-
Notifications
You must be signed in to change notification settings - Fork 569
Fix phpstan/phpstan#9455: Bug: False report Cannot call method getId() on A|null
#5447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 4 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-iy55e8w
Apr 18, 2026
+169
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2aae123
Fix phpstan/phpstan#9455: Type narrowing not propagated through store…
staabm 3940371
Add regression test for phpstan/phpstan#5207
phpstan-bot 656a527
Add impure method tests to verify type narrowing is not applied
phpstan-bot 62cf374
Add static method tests to bug-5207 regression test
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug5207; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| abstract class HelloWorld { | ||
| abstract public function getChild(): ?HelloWorld; | ||
|
|
||
| public function sayHello(): void { | ||
| $foo = null !== $this->getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\HelloWorld', $this->getChild()); | ||
| } | ||
| } | ||
|
|
||
| public function sayHelloInline(): void { | ||
| if (null !== $this->getChild()) { | ||
| assertType('Bug5207\HelloWorld', $this->getChild()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abstract class StaticWorld { | ||
| abstract public static function getChild(): ?StaticWorld; | ||
|
|
||
| public static function sayHello(): void { | ||
| $foo = null !== static::getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\StaticWorld', static::getChild()); | ||
| } | ||
| } | ||
|
|
||
| public static function sayHelloInline(): void { | ||
| if (null !== static::getChild()) { | ||
| assertType('Bug5207\StaticWorld', static::getChild()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abstract class ImpureStaticWorld { | ||
| /** | ||
| * @phpstan-impure | ||
| */ | ||
| abstract public static function getChild(): ?ImpureStaticWorld; | ||
|
|
||
| public static function sayHello(): void { | ||
| $foo = null !== static::getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\ImpureStaticWorld|null', static::getChild()); | ||
| } | ||
| } | ||
|
|
||
| public static function sayHelloInline(): void { | ||
| if (null !== static::getChild()) { | ||
| assertType('Bug5207\ImpureStaticWorld|null', static::getChild()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abstract class ImpureWorld { | ||
| /** | ||
| * @phpstan-impure | ||
| */ | ||
| abstract public function getChild(): ?ImpureWorld; | ||
|
|
||
| public function sayHello(): void { | ||
| $foo = null !== $this->getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\ImpureWorld|null', $this->getChild()); | ||
| } | ||
| } | ||
|
|
||
| public function sayHelloInline(): void { | ||
| if (null !== $this->getChild()) { | ||
| assertType('Bug5207\ImpureWorld|null', $this->getChild()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9455; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class A { | ||
| public function __construct(private int $id){} | ||
|
|
||
| public function getId(): int { | ||
| return $this->id; | ||
| } | ||
| } | ||
|
|
||
| class B { | ||
| public function __construct(private int $id, private ?A $a = null){} | ||
|
|
||
| public function getId(): int { | ||
| return $this->id; | ||
| } | ||
|
|
||
| public function getA(): ?A { | ||
| return $this->a; | ||
| } | ||
| } | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function testFails(): void | ||
| { | ||
| $a = new A(1); | ||
| $b = new B(1, $a); | ||
|
|
||
| $hasA = $b->getA() !== null; | ||
|
|
||
| if($hasA) { | ||
| assertType('Bug9455\A', $b->getA()); | ||
| } | ||
| } | ||
|
|
||
| public function testSucceeds(): void | ||
| { | ||
| $a = new A(1); | ||
| $b = new B(1, $a); | ||
|
|
||
| if($b->getA() !== null) { | ||
| assertType('Bug9455\A', $b->getA()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class C { | ||
| /** | ||
| * @phpstan-impure | ||
| */ | ||
| public function getA(): ?A { | ||
| return rand(0, 1) ? new A(1) : null; | ||
| } | ||
| } | ||
|
|
||
| class ImpureTest | ||
| { | ||
| public function testImpureMethodNotNarrowed(): void | ||
| { | ||
| $c = new C(); | ||
|
|
||
| $hasA = $c->getA() !== null; | ||
|
|
||
| if($hasA) { | ||
| assertType('Bug9455\A|null', $c->getA()); | ||
| } | ||
| } | ||
|
|
||
| public function testImpureMethodInline(): void | ||
| { | ||
| $c = new C(); | ||
|
|
||
| if($c->getA() !== null) { | ||
| assertType('Bug9455\A|null', $c->getA()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.