From 97b5685f558a77d91de5d068abe3da4673b29d22 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Mon, 21 Feb 2022 18:37:20 -0600 Subject: [PATCH] Fix first-class callable in loop --- .../PhpVisitor/AssignmentMapVisitor.php | 10 +++--- tests/ClosureTest.php | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/PhpVisitor/AssignmentMapVisitor.php b/src/Psalm/Internal/PhpVisitor/AssignmentMapVisitor.php index 89119466d78..a3fd78e28b8 100644 --- a/src/Psalm/Internal/PhpVisitor/AssignmentMapVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/AssignmentMapVisitor.php @@ -77,11 +77,13 @@ public function enterNode(PhpParser\Node $node): ?int || $node instanceof PhpParser\Node\Expr\MethodCall || $node instanceof PhpParser\Node\Expr\StaticCall ) { - foreach ($node->getArgs() as $arg) { - $arg_var_id = ExpressionIdentifier::getRootVarId($arg->value, $this->this_class_name); + if (!$node->isFirstClassCallable()) { + foreach ($node->getArgs() as $arg) { + $arg_var_id = ExpressionIdentifier::getRootVarId($arg->value, $this->this_class_name); - if ($arg_var_id) { - $this->assignment_map[$arg_var_id][$arg_var_id] = true; + if ($arg_var_id) { + $this->assignment_map[$arg_var_id][$arg_var_id] = true; + } } } diff --git a/tests/ClosureTest.php b/tests/ClosureTest.php index e5f6c9551b1..def99f9955c 100644 --- a/tests/ClosureTest.php +++ b/tests/ClosureTest.php @@ -773,6 +773,40 @@ public static function __callStatic(string $name, array $args): mixed { [], '8.1', ], + 'FirstClassCallable:AssignmentVisitorMap' => [ + ' */ + public array $handlers = []; + + public function register(): void { + foreach ([1, 2, 3] as $index) { + $this->push($this->handler(...)); + } + } + + /** + * @param Closure():void $closure + * @return void + */ + private function push(\Closure $closure): void { + $this->handlers[] = $closure; + } + + private function handler(): void { + } + } + + $test = new Test(); + $test->register(); + $handlers = $test->handlers; + ', + 'assertions' => [ + '$handlers' => 'list', + ], + 'ignored_issues' => [], + 'php_version' => '8.1', + ], 'arrowFunctionReturnsNeverImplictly' => [ '