1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Merge pull request #9338 from weirdan/forbid-first-class-callables-in-new

This commit is contained in:
Bruce Weirdan 2023-02-19 04:34:56 -04:00 committed by GitHub
commit 538b4c80ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -29,6 +29,7 @@ use Psalm\Issue\InternalClass;
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 @@ class NewAnalyzer extends CallAnalyzer
$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();

View File

@ -1419,6 +1419,15 @@ class ClosureTest extends TestCase
'ignored_issues' => [],
'php_version' => '7.4',
],
'FirstClassCallable:WithNew' => [
'code' => <<<'PHP'
<?php
new stdClass(...);
PHP,
'error_message' => 'ParseError',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}