Skip to content

Commit 7877d19

Browse files
phpstan-botclaude
andcommitted
Add impure method tests to verify type narrowing is not applied
Addresses review feedback: adds test cases with @phpstan-impure methods to both bug-9455 and bug-5207 regression tests, asserting that impure method results are correctly NOT narrowed when stored in boolean variables. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bc1c8a0 commit 7877d19

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tests/PHPStan/Analyser/nsrt/bug-5207.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,23 @@ public function sayHelloInline(): void {
2222
}
2323
}
2424
}
25+
26+
abstract class ImpureWorld {
27+
/**
28+
* @phpstan-impure
29+
*/
30+
abstract public function getChild(): ?ImpureWorld;
31+
32+
public function sayHello(): void {
33+
$foo = null !== $this->getChild();
34+
if ($foo) {
35+
assertType('Bug5207\ImpureWorld|null', $this->getChild());
36+
}
37+
}
38+
39+
public function sayHelloInline(): void {
40+
if (null !== $this->getChild()) {
41+
assertType('Bug5207\ImpureWorld|null', $this->getChild());
42+
}
43+
}
44+
}

tests/PHPStan/Analyser/nsrt/bug-9455.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,35 @@ public function testSucceeds(): void
5050
}
5151
}
5252
}
53+
54+
class C {
55+
/**
56+
* @phpstan-impure
57+
*/
58+
public function getA(): ?A {
59+
return rand(0, 1) ? new A(1) : null;
60+
}
61+
}
62+
63+
class ImpureTest
64+
{
65+
public function testImpureMethodNotNarrowed(): void
66+
{
67+
$c = new C();
68+
69+
$hasA = $c->getA() !== null;
70+
71+
if($hasA) {
72+
assertType('Bug9455\A|null', $c->getA());
73+
}
74+
}
75+
76+
public function testImpureMethodInline(): void
77+
{
78+
$c = new C();
79+
80+
if($c->getA() !== null) {
81+
assertType('Bug9455\A|null', $c->getA());
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)