Skip to content

Commit 3e442f2

Browse files
phpstan-botclaude
andcommitted
Add non-regression tests for phpstan/phpstan#4608
Ensure that union method names ('abc'|'not_abc') on anonymous classes correctly report errors for dynamic method calls, call_user_func, and array callable invocations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 33171cd commit 3e442f2

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ public function testPipeOperator(): void
364364
]);
365365
}
366366

367+
public function testBug4608(): void
368+
{
369+
$this->analyse([__DIR__ . '/data/bug-4608-callables.php'], [
370+
[
371+
"Trying to invoke array{class@anonymous/tests/PHPStan/Rules/Functions/data/bug-4608-callables.php:5, 'abc'|'not_abc'} but it might not be a callable.",
372+
11,
373+
],
374+
]);
375+
}
376+
367377
public function testMaybeNotCallable(): void
368378
{
369379
$errors = [];

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,4 +2817,14 @@ public function testBug14312b(): void
28172817
$this->analyse([__DIR__ . '/data/bug-14312b.php'], []);
28182818
}
28192819

2820+
public function testBug4608(): void
2821+
{
2822+
$this->analyse([__DIR__ . '/data/bug-4608-call-user-func.php'], [
2823+
[
2824+
"Parameter #1 \$callback of function call_user_func expects callable(): mixed, array{class@anonymous/tests/PHPStan/Rules/Functions/data/bug-4608-call-user-func.php:5, 'abc'|'not_abc'} given.",
2825+
11,
2826+
],
2827+
]);
2828+
}
2829+
28202830
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug4608CallUserFunc;
4+
5+
$c = new class {
6+
public function abc(): void {}
7+
};
8+
9+
$s = rand(0, 1) ? 'abc' : 'not_abc';
10+
11+
call_user_func([$c, $s]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug4608Callables;
4+
5+
$c = new class {
6+
public function abc(): void {}
7+
};
8+
9+
$s = rand(0, 1) ? 'abc' : 'not_abc';
10+
11+
[$c, $s]();

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,4 +3962,17 @@ public function testBug11463(): void
39623962
]);
39633963
}
39643964

3965+
public function testBug4608(): void
3966+
{
3967+
$this->checkThisOnly = false;
3968+
$this->checkNullables = true;
3969+
$this->checkUnionTypes = true;
3970+
$this->analyse([__DIR__ . '/data/bug-4608.php'], [
3971+
[
3972+
'Call to an undefined method class@anonymous/tests/PHPStan/Rules/Methods/data/bug-4608.php:5::not_abc().',
3973+
11,
3974+
],
3975+
]);
3976+
}
3977+
39653978
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug4608;
4+
5+
$c = new class {
6+
public function abc(): void {}
7+
};
8+
9+
$s = rand(0, 1) ? 'abc' : 'not_abc';
10+
11+
$c->{$s}();

0 commit comments

Comments
 (0)