File tree Expand file tree Collapse file tree 3 files changed +109
-0
lines changed Expand file tree Collapse file tree 3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug12376 ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ /**
8+ * workaround https://github.com/phpstan/phpstan-src/pull/3853
9+ *
10+ * @template T of object
11+ * @param class-string<T> $class
12+ * @return T
13+ */
14+ function newNonFinalInstance (string $ class ): object
15+ {
16+ return new $ class ();
17+ }
18+
19+ class Base {}
20+
21+ class A extends Base
22+ {
23+ /**
24+ * @template T of object
25+ * @param T $object
26+ * @return (T is static ? T : static)
27+ *
28+ * @phpstan-assert static $object
29+ */
30+ public static function assertInstanceOf (object $ object )
31+ {
32+ if (!$ object instanceof static) {
33+ throw new \Exception ();
34+ }
35+
36+ return $ object ;
37+ }
38+ }
39+
40+ class B extends A {}
41+ class C extends Base {}
42+
43+ $ o = newNonFinalInstance (\DateTime::class);
44+ $ r = A::assertInstanceOf ($ o );
45+ assertType ('*NEVER* ' , $ o );
46+ assertType ('A ' , $ r );
47+
48+ $ o = newNonFinalInstance (A::class);
49+ $ r = A::assertInstanceOf ($ o );
50+ assertType ('A ' , $ o );
51+ assertType ('A ' , $ r );
52+
53+ $ o = newNonFinalInstance (B::class);
54+ $ r = A::assertInstanceOf ($ o );
55+ assertType ('B ' , $ o );
56+ assertType ('B ' , $ r );
57+
58+ $ o = newNonFinalInstance (C::class);
59+ $ r = A::assertInstanceOf ($ o );
60+ assertType ('*NEVER* ' , $ o );
61+ assertType ('A ' , $ r );
62+
63+ $ o = newNonFinalInstance (A::class);
64+ $ r = B::assertInstanceOf ($ o );
65+ assertType ('B ' , $ o );
66+ assertType ('B ' , $ r );
Original file line number Diff line number Diff line change @@ -1165,6 +1165,14 @@ public function testBug12645(): void
11651165 ]);
11661166 }
11671167
1168+ public function testBug11289 (): void
1169+ {
1170+ $ this ->checkThisOnly = false ;
1171+ $ this ->checkUnionTypes = true ;
1172+ $ this ->checkDynamicProperties = false ;
1173+ $ this ->analyse ([__DIR__ . '/data/bug-11289.php ' ], []);
1174+ }
1175+
11681176 public function testBug8668 (): void
11691177 {
11701178 $ this ->checkThisOnly = false ;
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug11289 ;
4+
5+ abstract class SomeAbstractClass
6+ {
7+ private bool $ someValue = true ;
8+
9+ /**
10+ * @phpstan-assert-if-true =static $other
11+ */
12+ public function equals (?self $ other ): bool
13+ {
14+ return $ other instanceof static
15+ && $ this ->someValue === $ other ->someValue ;
16+ }
17+ }
18+
19+ class SomeConcreteClass extends SomeAbstractClass
20+ {
21+ public function __construct (
22+ private bool $ someOtherValue ,
23+ ) {}
24+
25+ public function equals (?SomeAbstractClass $ other ): bool
26+ {
27+ return parent ::equals ($ other )
28+ && $ this ->someOtherValue === $ other ->someOtherValue ;
29+ }
30+ }
31+
32+ $ a = new SomeConcreteClass (true );
33+ $ b = new SomeConcreteClass (false );
34+
35+ var_dump ($ a ->equals ($ b ), $ b ->equals ($ b ));
You can’t perform that action at this time.
0 commit comments