Skip to content

Commit e3d5526

Browse files
authored
Merge pull request #10838 from kkmuffme/undefined-parent-not-reported-in-callable
2 parents f61c7e1 + ff168a9 commit e3d5526

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Psalm\Issue\NamedArgumentNotAllowed;
3939
use Psalm\Issue\NoValue;
4040
use Psalm\Issue\NullArgument;
41+
use Psalm\Issue\ParentNotFound;
4142
use Psalm\Issue\PossiblyFalseArgument;
4243
use Psalm\Issue\PossiblyInvalidArgument;
4344
use Psalm\Issue\PossiblyNullArgument;
@@ -1297,6 +1298,16 @@ private static function verifyExplicitParam(
12971298

12981299
if ($callable_fq_class_name === 'parent') {
12991300
$container_class = $statements_analyzer->getParentFQCLN();
1301+
if ($container_class === null) {
1302+
IssueBuffer::accepts(
1303+
new ParentNotFound(
1304+
'Cannot call method on parent'
1305+
. ' as this class does not extend another',
1306+
$arg_location,
1307+
),
1308+
$statements_analyzer->getSuppressedIssues(),
1309+
);
1310+
}
13001311
}
13011312

13021313
if (!$container_class) {

tests/CallableTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,6 +2533,40 @@ function f(callable $c): void {
25332533
'ignored_issues' => [],
25342534
'php_version' => '8.0',
25352535
],
2536+
'parentCallableArrayWithoutParent' => [
2537+
'code' => '<?php
2538+
class A {
2539+
public function __construct() {
2540+
$this->run(["parent", "hello"]);
2541+
}
2542+
2543+
/**
2544+
* @param callable $callable
2545+
* @return void
2546+
*/
2547+
public function run($callable) {
2548+
call_user_func($callable);
2549+
}
2550+
}',
2551+
'error_message' => 'ParentNotFound',
2552+
],
2553+
'parentCallableWithoutParent' => [
2554+
'code' => '<?php
2555+
class A {
2556+
public function __construct() {
2557+
$this->run("parent::hello");
2558+
}
2559+
2560+
/**
2561+
* @param callable $callable
2562+
* @return void
2563+
*/
2564+
public function run($callable) {
2565+
call_user_func($callable);
2566+
}
2567+
}',
2568+
'error_message' => 'ParentNotFound',
2569+
],
25362570
];
25372571
}
25382572
}

0 commit comments

Comments
 (0)