From 45e5b4a1f2362628021c0bed75871e7411e38614 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 18 Apr 2026 13:47:25 +0200 Subject: [PATCH 1/3] Added regression test --- tests/PHPStan/Analyser/nsrt/bug-5207b.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-5207b.php diff --git a/tests/PHPStan/Analyser/nsrt/bug-5207b.php b/tests/PHPStan/Analyser/nsrt/bug-5207b.php new file mode 100644 index 0000000000..13f3856ee1 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-5207b.php @@ -0,0 +1,20 @@ += 8.0 + +declare(strict_types = 1); + +namespace Bug5207b; + +use function PHPStan\Testing\assertType; + +abstract class HelloWorld { + abstract public function getChild(): ?HelloWorld; + + public function sayHello(): void { + $foo = null !== $this->getChild()->getChild(); + if ($foo) { + assertType('Bug5207\HelloWorld', $this->getChild()); + assertType('Bug5207\HelloWorld', $this->getChild()->getChild()); + assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild()); + } + } +} From 465301f06c43b9852db4adca781a9d294bb8a033 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 18 Apr 2026 13:49:51 +0200 Subject: [PATCH 2/3] Update bug-5207b.php --- tests/PHPStan/Analyser/nsrt/bug-5207b.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Analyser/nsrt/bug-5207b.php b/tests/PHPStan/Analyser/nsrt/bug-5207b.php index 13f3856ee1..76d3fec935 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-5207b.php +++ b/tests/PHPStan/Analyser/nsrt/bug-5207b.php @@ -12,8 +12,26 @@ abstract public function getChild(): ?HelloWorld; public function sayHello(): void { $foo = null !== $this->getChild()->getChild(); if ($foo) { - assertType('Bug5207\HelloWorld', $this->getChild()); - assertType('Bug5207\HelloWorld', $this->getChild()->getChild()); + assertType('Bug5207b\HelloWorld|null', $this->getChild()); // could be Bug5207b\HelloWorld + assertType('Bug5207b\HelloWorld', $this->getChild()->getChild()); + assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild()); + } + } + + public function sayFoo(): void { + $foo = null !== $this?->getChild()?->getChild(); + if ($foo) { + assertType('Bug5207b\HelloWorld', $this->getChild()); + assertType('Bug5207b\HelloWorld', $this->getChild()->getChild()); + assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild()); + } + } + + public function sayBar(): void { + $foo = null !== $this?->getChild()->getChild(); + if ($foo) { + assertType('Bug5207b\HelloWorld', $this->getChild()); + assertType('Bug5207b\HelloWorld', $this->getChild()->getChild()); assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild()); } } From 3ad693e626e7175903b4788bda2b2fb007a4f2a2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 19 Apr 2026 11:18:36 +0200 Subject: [PATCH 3/3] added another test --- .../Methods/NullsafeMethodCallRuleTest.php | 11 ++++ .../PHPStan/Rules/Methods/data/bug-5207b.php | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-5207b.php diff --git a/tests/PHPStan/Rules/Methods/NullsafeMethodCallRuleTest.php b/tests/PHPStan/Rules/Methods/NullsafeMethodCallRuleTest.php index 0d1bc3e1b6..4b0b8db7e2 100644 --- a/tests/PHPStan/Rules/Methods/NullsafeMethodCallRuleTest.php +++ b/tests/PHPStan/Rules/Methods/NullsafeMethodCallRuleTest.php @@ -74,6 +74,17 @@ public function testBug8523c(): void $this->analyse([__DIR__ . '/data/bug-8523c.php'], []); } + #[RequiresPhp('>= 8.0.0')] + public function testBug5207b(): void + { + $this->analyse([__DIR__ . '/data/bug-5207b.php'], [ + [ + 'Using nullsafe method call on non-nullable type Bug5207b\OrderCustomerEntity. Use -> instead.', + 47, + ], + ]); + } + #[RequiresPhp('>= 8.1.0')] public function testBug12222(): void { diff --git a/tests/PHPStan/Rules/Methods/data/bug-5207b.php b/tests/PHPStan/Rules/Methods/data/bug-5207b.php new file mode 100644 index 0000000000..010dae12c9 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-5207b.php @@ -0,0 +1,52 @@ += 8.0 + +declare(strict_types = 1); + +namespace Bug5207b; + +class OrderEntity +{ + public function getOrderCustomer(): ?OrderCustomerEntity + { + return new OrderCustomerEntity(); + } +} + +class OrderCustomerEntity +{ + public function getCustomer(): ?CustomerEntity + { + return null; + } + + public function getEmail(): string + { + return ''; + } +} + +class CustomerEntity +{ + public function getGuest(): bool + { + return true; + } +} + +class GuestAuthenticator +{ + public function validate(OrderEntity $order, string $s): void + { + $isOrderByGuest = $order->getOrderCustomer()?->getCustomer()?->getGuest(); + + if (!$isOrderByGuest) { + throw new \Exception(); + } + + + if (mb_strtolower($s) !== mb_strtolower($order->getOrderCustomer()?->getEmail() ?: '')) { + throw new \Exception(); + } + } +} +