Skip to content

Commit d5e44c7

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents d98c69e + 3cb3109 commit d5e44c7

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug5207b;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
abstract class HelloWorld {
10+
abstract public function getChild(): ?HelloWorld;
11+
12+
public function sayHello(): void {
13+
$foo = null !== $this->getChild()->getChild();
14+
if ($foo) {
15+
assertType('Bug5207b\HelloWorld|null', $this->getChild()); // could be Bug5207b\HelloWorld
16+
assertType('Bug5207b\HelloWorld', $this->getChild()->getChild());
17+
assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild());
18+
}
19+
}
20+
21+
public function sayFoo(): void {
22+
$foo = null !== $this?->getChild()?->getChild();
23+
if ($foo) {
24+
assertType('Bug5207b\HelloWorld', $this->getChild());
25+
assertType('Bug5207b\HelloWorld', $this->getChild()->getChild());
26+
assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild());
27+
}
28+
}
29+
30+
public function sayBar(): void {
31+
$foo = null !== $this?->getChild()->getChild();
32+
if ($foo) {
33+
assertType('Bug5207b\HelloWorld', $this->getChild());
34+
assertType('Bug5207b\HelloWorld', $this->getChild()->getChild());
35+
assertType('Bug5207b\HelloWorld|null', $this->getChild()->getChild()->getChild());
36+
}
37+
}
38+
}

tests/PHPStan/Rules/Methods/NullsafeMethodCallRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public function testBug8523c(): void
7474
$this->analyse([__DIR__ . '/data/bug-8523c.php'], []);
7575
}
7676

77+
#[RequiresPhp('>= 8.0.0')]
78+
public function testBug5207b(): void
79+
{
80+
$this->analyse([__DIR__ . '/data/bug-5207b.php'], [
81+
[
82+
'Using nullsafe method call on non-nullable type Bug5207b\OrderCustomerEntity. Use -> instead.',
83+
47,
84+
],
85+
]);
86+
}
87+
7788
#[RequiresPhp('>= 8.1.0')]
7889
public function testBug12222(): void
7990
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug5207b;
6+
7+
class OrderEntity
8+
{
9+
public function getOrderCustomer(): ?OrderCustomerEntity
10+
{
11+
return new OrderCustomerEntity();
12+
}
13+
}
14+
15+
class OrderCustomerEntity
16+
{
17+
public function getCustomer(): ?CustomerEntity
18+
{
19+
return null;
20+
}
21+
22+
public function getEmail(): string
23+
{
24+
return '';
25+
}
26+
}
27+
28+
class CustomerEntity
29+
{
30+
public function getGuest(): bool
31+
{
32+
return true;
33+
}
34+
}
35+
36+
class GuestAuthenticator
37+
{
38+
public function validate(OrderEntity $order, string $s): void
39+
{
40+
$isOrderByGuest = $order->getOrderCustomer()?->getCustomer()?->getGuest();
41+
42+
if (!$isOrderByGuest) {
43+
throw new \Exception();
44+
}
45+
46+
47+
if (mb_strtolower($s) !== mb_strtolower($order->getOrderCustomer()?->getEmail() ?: '')) {
48+
throw new \Exception();
49+
}
50+
}
51+
}
52+

0 commit comments

Comments
 (0)