Skip to content

Commit

Permalink
Fix processCalledMethod when anonymous classes are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 4, 2023
1 parent 9b52e62 commit 3f52779
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -4422,11 +4422,19 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Metho
$parserNodes = $this->parser->parseFile($fileName);

$returnStatement = null;
$this->processNodesForCalledMethod($parserNodes, $fileName, $methodReflection, static function (Node $node) use (&$returnStatement): void {
$this->processNodesForCalledMethod($parserNodes, $fileName, $methodReflection, static function (Node $node, Scope $scope) use ($methodReflection, &$returnStatement): void {
if (!$node instanceof MethodReturnStatementsNode) {
return;
}

if (!$scope->isInClass()) {
return;
}

if ($scope->getClassReflection()->getName() !== $methodReflection->getDeclaringClass()->getName()) {
return;
}

if ($returnStatement !== null) {
return;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Properties/data/uninitialized-property.php
Expand Up @@ -304,3 +304,31 @@ public function doSomething()
}

}

class ConfuseNodeScopeResolverWithAnonymousClass
{

private int $foo;

public function __construct()
{
$this->setFoo();
$this->doSomething();
}

private function setFoo()
{
$c = new class () {
public function setFoo()
{
}
};
$this->foo = 1;
}

public function doSomething()
{
echo $this->foo;
}

}

0 comments on commit 3f52779

Please sign in to comment.