From 73649889348953e1362af53fd2db94c808bfe7fc Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 2 Dec 2022 23:28:22 -0400 Subject: [PATCH] Fix crash when using phantom methods as first-class callable Fixes vimeo/psalm#8377 --- .../Call/Method/AtomicMethodCallAnalyzer.php | 26 +++++++++++-------- tests/ClosureTest.php | 15 +++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php index ffd12120872..29f510c6359 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php @@ -685,18 +685,22 @@ private static function handleInvalidClass( } } - if (ArgumentsAnalyzer::analyze( - $statements_analyzer, - $stmt->getArgs(), - null, - null, - true, - $context - ) === false) { - return; - } + if ($stmt->isFirstClassCallable()) { + $result->return_type = Type::getClosure(); + } else { + if (ArgumentsAnalyzer::analyze( + $statements_analyzer, + $stmt->getArgs(), + null, + null, + true, + $context + ) === false) { + return; + } - $result->return_type = Type::getMixed(); + $result->return_type = Type::getMixed(); + } return; default: diff --git a/tests/ClosureTest.php b/tests/ClosureTest.php index 0043d17e31b..8ec439bdf8d 100644 --- a/tests/ClosureTest.php +++ b/tests/ClosureTest.php @@ -872,6 +872,21 @@ private function handler(): void { 'ignored_issues' => [], 'php_version' => '8.1', ], + 'FirstClassCallable:Method:Asserted' => [ + 'code' => '$m(...); + } + ', + 'assertions' => [ + '$r===' => 'Closure|false', + ], + 'ignored_issues' => [], + 'php_version' => '8.1', + ], 'arrowFunctionReturnsNeverImplictly' => [ 'code' => '