Skip to content

Commit 099a0ba

Browse files
committed
resolveType in ClassConstFetchHandler
1 parent 456275f commit 099a0ba

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/Analyser/ExprHandler/ClassConstFetchHandler.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node\Expr;
66
use PhpParser\Node\Expr\ClassConstFetch;
7+
use PhpParser\Node\Identifier;
78
use PhpParser\Node\Stmt;
89
use PHPStan\Analyser\ExpressionContext;
910
use PHPStan\Analyser\ExpressionResult;
@@ -12,6 +13,9 @@
1213
use PHPStan\Analyser\MutatingScope;
1314
use PHPStan\Analyser\NodeScopeResolver;
1415
use PHPStan\DependencyInjection\AutowiredService;
16+
use PHPStan\Reflection\InitializerExprTypeResolver;
17+
use PHPStan\Type\MixedType;
18+
use PHPStan\Type\Type;
1519
use function array_merge;
1620

1721
/**
@@ -21,11 +25,34 @@
2125
final class ClassConstFetchHandler implements ExprHandler
2226
{
2327

28+
public function __construct(
29+
private InitializerExprTypeResolver $initializerExprTypeResolver,
30+
)
31+
{
32+
}
33+
2434
public function supports(Expr $expr): bool
2535
{
2636
return $expr instanceof ClassConstFetch;
2737
}
2838

39+
/**
40+
* @param ClassConstFetch $expr
41+
*/
42+
public function resolveType(MutatingScope $scope, Expr $expr): Type
43+
{
44+
if (!$expr->name instanceof Identifier) {
45+
return new MixedType();
46+
}
47+
48+
return $this->initializerExprTypeResolver->getClassConstFetchTypeByReflection(
49+
$expr->class,
50+
$expr->name->name,
51+
$scope->isInClass() ? $scope->getClassReflection() : null,
52+
static fn (Expr $e): Type => $scope->getType($e),
53+
);
54+
}
55+
2956
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
3057
{
3158
$hasYield = false;

src/Analyser/MutatingScope.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,16 +1154,6 @@ private function resolveType(string $exprString, Expr $node): Type
11541154
}
11551155

11561156
return new ErrorType();
1157-
} elseif ($node instanceof Node\Expr\ClassConstFetch && $node->name instanceof Node\Identifier) {
1158-
if ($this->hasExpressionType($node)->yes()) {
1159-
return $this->expressionTypes[$exprString]->getType();
1160-
}
1161-
return $this->initializerExprTypeResolver->getClassConstFetchTypeByReflection(
1162-
$node->class,
1163-
$node->name->name,
1164-
$this->isInClass() ? $this->getClassReflection() : null,
1165-
fn (Expr $expr): Type => $this->getType($expr),
1166-
);
11671157
}
11681158

11691159
if ($node instanceof MethodCall) {

0 commit comments

Comments
 (0)