Skip to content

Commit

Permalink
Merge pull request #9338 from weirdan/forbid-first-class-callables-in…
Browse files Browse the repository at this point in the history
…-new
  • Loading branch information
weirdan committed Feb 19, 2023
2 parents 075e97a + 73bad89 commit 538b4c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions tests/ClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,15 @@ public function f(): int {
'ignored_issues' => [],
'php_version' => '7.4',
],
'FirstClassCallable:WithNew' => [
'code' => <<<'PHP'
<?php
new stdClass(...);
PHP,
'error_message' => 'ParseError',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}

0 comments on commit 538b4c8

Please sign in to comment.