Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Analyser/Generator/ExprHandler/CastObjectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser\Generator\ExprHandler;

use Generator;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
use PHPStan\Analyser\Generator\ExprAnalysisResult;
use PHPStan\Analyser\Generator\ExprHandler;
use PHPStan\Analyser\Generator\GeneratorScope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\InitializerExprTypeResolver;

/**
* @implements ExprHandler<Expr\Cast\Object_>
*/
#[AutowiredService]
final class CastObjectHandler implements ExprHandler
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
{
}

public function supports(Expr $expr): bool
{
return $expr instanceof Expr\Cast\Object_;
}

public function analyseExpr(
Stmt $stmt,
Expr $expr,
GeneratorScope $scope,
ExpressionContext $context,
?callable $alternativeNodeCallback,
): Generator
{
$exprResult = yield new ExprAnalysisRequest($stmt, $expr->expr, $scope, $context->enterDeep(), $alternativeNodeCallback);

return new ExprAnalysisResult(
$this->initializerExprTypeResolver->getCastObjectType($exprResult->type),
$this->initializerExprTypeResolver->getCastObjectType($exprResult->nativeType),
$scope,
hasYield: false,
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
specifiedTruthyTypes: new SpecifiedTypes(),
specifiedFalseyTypes: new SpecifiedTypes(),
specifiedNullTypes: new SpecifiedTypes(),
);
}

}
12 changes: 8 additions & 4 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,14 @@ public function getCastType(Expr\Cast $expr, callable $getTypeCallback): Type
return $getTypeCallback($expr->expr)->toArray();
}
if ($expr instanceof Object_) {
return $this->getCastObjectType($getTypeCallback($expr->expr));
}

return new MixedType();
}

public function getCastObjectType(Type $exprType): Type
{
$castToObject = static function (Type $type): Type {
$constantArrays = $type->getConstantArrays();
if (count($constantArrays) > 0) {
Expand Down Expand Up @@ -730,17 +738,13 @@ public function getCastType(Expr\Cast $expr, callable $getTypeCallback): Type
return new ObjectType('stdClass');
};

$exprType = $getTypeCallback($expr->expr);
if ($exprType instanceof UnionType) {
return TypeCombinator::union(...array_map($castToObject, $exprType->getTypes()));
}

return $castToObject($exprType);
}

return new MixedType();
}

/**
* @param Name|Identifier|ComplexType|null $type
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/Generator/data/gnsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function doCast() {

assertType('1', (int) $a);
assertType("array{'1'}", (array) $a);
// assertType("array{'1'}", (object) $a);
assertType('stdClass', (object) $a);
assertType('1.0', (double) $a);
assertType('true', (bool) $a);
assertType("'1'", (string) $a);
Expand Down
Loading