forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-14279.php
More file actions
78 lines (65 loc) · 1.66 KB
/
bug-14279.php
File metadata and controls
78 lines (65 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php declare(strict_types=1);
namespace Bug14279;
use function PHPStan\Testing\assertType;
/**
* @template TElement
* @implements \IteratorAggregate<array-key, TElement>
*/
abstract class Collection implements \IteratorAggregate, \Countable
{
/** @var array<array-key, TElement> */
protected array $elements = [];
/** @param iterable<TElement> $elements */
public function __construct(iterable $elements = [])
{
}
/**
* @param array-key $key
* @return TElement|null
*/
public function get($key)
{
return $this->elements[$key] ?? null;
}
/** @phpstan-impure */
public function count(): int
{
return \count($this->elements);
}
/** @return \Traversable<TElement> */
public function getIterator(): \Traversable
{
yield from $this->elements;
}
public function assignRecursive(array $options): static
{
return $this;
}
}
/**
* @template TElement
* @extends Collection<TElement>
*/
class TestCollection extends Collection
{
}
function test(): void
{
$data = [
null,
0,
'some-string',
];
$collection = (new TestCollection())->assignRecursive($data);
// assertSame($data[0], $collection->get(0)) narrows $data[0] via Identical
// $collection->get(0) returns null (TElement=*NEVER*)
// intersect(null, null) = null - no problem here
assert($data[0] === $collection->get(0));
assertType('null', $data[0]);
assertType("'some-string'", $data[2]);
// assertSame($data[1], $collection->get(1)) narrows $data[1] via Identical
// $collection->get(1) returns null, $data[1] is 0
// intersect(0, null) = *NEVER* - this must not poison the parent array $data
assert($data[1] === $collection->get(1));
assertType("'some-string'", $data[2]);
}