Skip to content

Commit c2b9570

Browse files
phpstan-botclaude
andcommitted
Add rule test for undefined method calls on union template return types
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent fd75876 commit c2b9570

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,23 @@ public function testBug14720(): void
987987
$this->analyse([__DIR__ . '/data/bug-14720.php'], []);
988988
}
989989

990+
public function testBug2579(): void
991+
{
992+
$this->checkThisOnly = false;
993+
$this->checkNullables = true;
994+
$this->checkUnionTypes = true;
995+
$this->analyse([__DIR__ . '/data/bug-2579.php'], [
996+
[
997+
'Call to an undefined method Bug2579Methods\A1::bar().',
998+
30,
999+
],
1000+
[
1001+
'Call to an undefined method Bug2579Methods\B1::foo().',
1002+
31,
1003+
],
1004+
]);
1005+
}
1006+
9901007
public function testClosureBind(): void
9911008
{
9921009
$this->checkThisOnly = false;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug2579Methods;
4+
5+
class A {}
6+
class B {}
7+
class A1 extends A {
8+
public function foo(): void {}
9+
}
10+
class B1 extends B {
11+
public function bar(): void {}
12+
}
13+
14+
/**
15+
* @template T1 of A
16+
* @template T2 of B
17+
* @param T1|T2 $type
18+
* @return T1|T2
19+
*/
20+
function f(object $type)
21+
{
22+
return new $type();
23+
}
24+
25+
function test(): void
26+
{
27+
(f(new A1()))->foo(); // should pass
28+
(f(new B1()))->bar(); // should pass
29+
30+
(f(new A1()))->bar(); // should fail
31+
(f(new B1()))->foo(); // should fail
32+
}

0 commit comments

Comments
 (0)