Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-5207b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php // lint >= 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('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());
}
}
}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Methods/NullsafeMethodCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
52 changes: 52 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-5207b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php // lint >= 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();
}
}
}

Loading