1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Merge pull request #10838 from kkmuffme/undefined-parent-not-reported-in-callable

This commit is contained in:
Bruce Weirdan 2024-03-20 01:08:45 +01:00 committed by GitHub
commit e3d55268f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -38,6 +38,7 @@ use Psalm\Issue\MixedArgumentTypeCoercion;
use Psalm\Issue\NamedArgumentNotAllowed; use Psalm\Issue\NamedArgumentNotAllowed;
use Psalm\Issue\NoValue; use Psalm\Issue\NoValue;
use Psalm\Issue\NullArgument; use Psalm\Issue\NullArgument;
use Psalm\Issue\ParentNotFound;
use Psalm\Issue\PossiblyFalseArgument; use Psalm\Issue\PossiblyFalseArgument;
use Psalm\Issue\PossiblyInvalidArgument; use Psalm\Issue\PossiblyInvalidArgument;
use Psalm\Issue\PossiblyNullArgument; use Psalm\Issue\PossiblyNullArgument;
@ -1297,6 +1298,16 @@ final class ArgumentAnalyzer
if ($callable_fq_class_name === 'parent') { if ($callable_fq_class_name === 'parent') {
$container_class = $statements_analyzer->getParentFQCLN(); $container_class = $statements_analyzer->getParentFQCLN();
if ($container_class === null) {
IssueBuffer::accepts(
new ParentNotFound(
'Cannot call method on parent'
. ' as this class does not extend another',
$arg_location,
),
$statements_analyzer->getSuppressedIssues(),
);
}
} }
if (!$container_class) { if (!$container_class) {

View File

@ -2533,6 +2533,40 @@ class CallableTest extends TestCase
'ignored_issues' => [], 'ignored_issues' => [],
'php_version' => '8.0', 'php_version' => '8.0',
], ],
'parentCallableArrayWithoutParent' => [
'code' => '<?php
class A {
public function __construct() {
$this->run(["parent", "hello"]);
}
/**
* @param callable $callable
* @return void
*/
public function run($callable) {
call_user_func($callable);
}
}',
'error_message' => 'ParentNotFound',
],
'parentCallableWithoutParent' => [
'code' => '<?php
class A {
public function __construct() {
$this->run("parent::hello");
}
/**
* @param callable $callable
* @return void
*/
public function run($callable) {
call_user_func($callable);
}
}',
'error_message' => 'ParentNotFound',
],
]; ];
} }
} }