File tree Expand file tree Collapse file tree
tests/PHPStan/Analyser/nsrt Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug5207 ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ abstract class HelloWorld
8+ {
9+
10+ abstract public function getChild (): ?HelloWorld ;
11+
12+ public function sayHello (): HelloWorld
13+ {
14+ $ foo = null !== $ this ->getChild ();
15+
16+ if ($ foo ) {
17+ assertType (HelloWorld::class, $ this ->getChild ());
18+ return $ this ->getChild ();
19+ }
20+
21+ throw new \Exception ();
22+ }
23+
24+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug9455 ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ class A {
8+ public function __construct (private int $ id ) {}
9+
10+ public function getId (): int
11+ {
12+ return $ this ->id ;
13+ }
14+ }
15+
16+ class B {
17+ public function __construct (private int $ id , private ?A $ a = null ) {}
18+
19+ public function getId (): int
20+ {
21+ return $ this ->id ;
22+ }
23+
24+ public function getA (): ?A
25+ {
26+ return $ this ->a ;
27+ }
28+ }
29+
30+ class HelloWorld
31+ {
32+
33+ public function testFails (B $ b ): void
34+ {
35+ $ hasA = $ b ->getA () !== null ;
36+
37+ if ($ hasA ) {
38+ assertType (A::class, $ b ->getA ());
39+ echo $ b ->getA ()->getId ();
40+ }
41+ }
42+
43+ public function testSucceeds (B $ b ): void
44+ {
45+ if ($ b ->getA () !== null ) {
46+ assertType (A::class, $ b ->getA ());
47+ echo $ b ->getA ()->getId ();
48+ }
49+ }
50+
51+ }
You can’t perform that action at this time.
0 commit comments