diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php index c02cb9304a0..0087a5850c7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NewAnalyzer.php @@ -29,6 +29,7 @@ use Psalm\Issue\InternalMethod; use Psalm\Issue\InvalidStringClass; use Psalm\Issue\MixedMethodCall; +use Psalm\Issue\ParseError; use Psalm\Issue\TooManyArguments; use Psalm\Issue\UndefinedClass; use Psalm\Issue\UnsafeGenericInstantiation; @@ -84,6 +85,14 @@ public static function analyze( $from_static = false; + if ($stmt->isFirstClassCallable()) { + IssueBuffer::maybeAdd(new ParseError( + 'First-class callables cannot be used in new', + new CodeLocation($statements_analyzer->getSource(), $stmt), + )); + return false; + } + if ($stmt->class instanceof PhpParser\Node\Name) { if (!in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)) { $aliases = $statements_analyzer->getAliases(); diff --git a/tests/ClosureTest.php b/tests/ClosureTest.php index 5a381eeb364..43d441944fa 100644 --- a/tests/ClosureTest.php +++ b/tests/ClosureTest.php @@ -1419,6 +1419,15 @@ public function f(): int { 'ignored_issues' => [], 'php_version' => '7.4', ], + 'FirstClassCallable:WithNew' => [ + 'code' => <<<'PHP' + 'ParseError', + 'ignored_issues' => [], + 'php_version' => '8.1', + ], ]; } }