File tree Expand file tree Collapse file tree
tests/PHPStan/Rules/Methods Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4685,4 +4685,13 @@ public function testBug14819(): void
46854685 $ this ->analyse ([__DIR__ . '/data/bug-14819.php ' ], []);
46864686 }
46874687
4688+ #[RequiresPhp('>= 8.0.0 ' )]
4689+ public function testBug12978 (): void
4690+ {
4691+ $ this ->checkThisOnly = false ;
4692+ $ this ->checkNullables = true ;
4693+ $ this ->checkUnionTypes = true ;
4694+ $ this ->analyse ([__DIR__ . '/data/bug-12978.php ' ], []);
4695+ }
4696+
46884697}
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.0
2+
3+ namespace Bug12978 ;
4+
5+ /**
6+ * @template T
7+ */
8+ class Collection
9+ {
10+ /**
11+ * @param T[] $items
12+ */
13+ public function __construct (
14+ private array $ items = [],
15+ ) {}
16+
17+ /**
18+ * @param T $item
19+ */
20+ public function add ($ item ): void
21+ {
22+ $ this ->items [] = $ item ;
23+ }
24+
25+ /**
26+ * @return ?T
27+ */
28+ public function get (int $ index )
29+ {
30+ return $ this ->items [$ index ] ?? null ;
31+ }
32+ }
33+
34+ class Animal {}
35+
36+ class Dog extends Animal {}
37+
38+ /** @var Collection<Animal> */
39+ $ collectionWithExplicitType = new Collection ([new Animal ()]);
40+ \PHPStan \dumpType ($ collectionWithExplicitType );
41+ $ collectionWithExplicitType ->add (new Dog ());
42+
43+ $ collectionInferredFromConstructor = new Collection ([new Animal ()]);
44+ \PHPStan \dumpType ($ collectionInferredFromConstructor );
45+ $ collectionInferredFromConstructor ->add (new Dog ());
You can’t perform that action at this time.
0 commit comments