Skip to content

Commit 6b4cb63

Browse files
committed
1 parent d17593f commit 6b4cb63

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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());

0 commit comments

Comments
 (0)