Skip to content

Commit d64af64

Browse files
committed
Fix inconsistent type inference for anonymous functions in class constants
1 parent 2a5eaba commit d64af64

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,19 @@ private function processStmtNode(
10621062
});
10631063

10641064
$this->processStmtNodesInternal($stmt, $classLikeStatements, $classScope, $storage, $classStatementsGatherer, $context);
1065+
foreach ($stmt->stmts as $classLikeStmt) {
1066+
if (!$classLikeStmt instanceof Node\Stmt\ClassConst || !$classLikeStmt->isPublic()) {
1067+
continue;
1068+
}
1069+
1070+
foreach ($classLikeStmt->consts as $const) {
1071+
$scope = $scope->assignExpression(
1072+
new Expr\ClassConstFetch(new Name\FullyQualified($classReflection->getName()), $const->name),
1073+
$classScope->getType($const->value),
1074+
$classScope->getNativeType($const->value),
1075+
);
1076+
}
1077+
}
10651078
$this->callNodeCallback($nodeCallback, new ClassPropertiesNode($stmt, $this->readWritePropertiesExtensionProvider, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls(), $classStatementsGatherer->getReturnStatementsNodes(), $classStatementsGatherer->getPropertyAssigns(), $classReflection), $classScope, $storage);
10661079
$this->callNodeCallback($nodeCallback, new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls(), $classReflection), $classScope, $storage);
10671080
$this->callNodeCallback($nodeCallback, new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches(), $classReflection), $classScope, $storage);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php // lint >= 8.2
2+
3+
namespace Bug14105;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
final readonly class Foo
8+
{
9+
const func = static function (int $num): array {
10+
return ['num' => $num];
11+
};
12+
}
13+
14+
const func = static function (int $num): array {
15+
return ['num' => $num];
16+
};
17+
18+
assertType('Closure(int): array{num: int}', Foo::func);
19+
assertType('Closure(int): array{num: int}', func);

0 commit comments

Comments
 (0)