-
Notifications
You must be signed in to change notification settings - Fork 571
Expand file tree
/
Copy patharray-union-keep-separate.php
More file actions
118 lines (102 loc) · 2.67 KB
/
array-union-keep-separate.php
File metadata and controls
118 lines (102 loc) · 2.67 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php declare(strict_types=1);
namespace ArrayUnionKeepSeparate;
use function PHPStan\Testing\assertType;
class KeepSeparate
{
/** @param array<int>|array<string> $arr */
public function plainArray(array $arr): void
{
assertType('array<int>|array<string>', $arr);
}
/** @param list<int>|list<string> $list */
public function listUnion(array $list): void
{
assertType('list<int>|list<string>', $list);
}
/** @param non-empty-array<int, int>|non-empty-array<string, string> $arr */
public function distinctKeyAndValue(array $arr): void
{
assertType('non-empty-array<int, int>|non-empty-array<string, string>', $arr);
}
/**
* Subsumption: array<int> is a subtype of array<int|string>, so the
* union must collapse to the wider member.
*
* @param array<int>|array<int|string> $arr
*/
public function subsumesWider(array $arr): void
{
assertType('array<int|string>', $arr);
}
/**
* Subsumption across list/array: list<int> is a subtype of array<int>.
*
* @param list<int>|array<int> $arr
*/
public function subsumesListIntoArray(array $arr): void
{
assertType('array<int>', $arr);
}
/**
* Identical members dedupe.
*
* @param array<int>|array<int> $arr
*/
public function identicalMembers(array $arr): void
{
assertType('array<int>', $arr);
}
/**
* Narrowing the value via offset-access propagates back to the array.
*
* @param list<int>|list<string> $list
*/
public function narrowByOffset(array $list): void
{
if (count($list) === 0) {
return;
}
if (is_string($list[0])) {
assertType('non-empty-list<string>&hasOffsetValue(0, string)', $list);
} else {
assertType('non-empty-list<int>&hasOffsetValue(0, int)', $list);
}
}
/**
* A mixed array is not a subtype of a union of homogeneous arrays.
*
* @param array<int>|array<string> $arr
*/
public function acceptsTaker(array $arr): void
{
}
public function callerWithMixedArray(): void
{
/** @var array<int|string> $mixed */
$mixed = [];
// phpstan-should-error: passing array<int|string> does not satisfy array<int>|array<string>
$this->acceptsTaker($mixed);
}
/**
* Constant array stays separate from a general array (no folding into
* array<int|string, ...>).
*
* @param array{foo: int}|array<string, string> $arr
*/
public function constantAndGeneral(array $arr): void
{
assertType("array{foo: int}|array<string, string>", $arr);
}
/**
* Iterating a union preserves the element union (existing UnionType
* behavior; regression guard for the keep-separate change).
*
* @param list<int>|list<string> $list
*/
public function iteration(array $list): void
{
foreach ($list as $value) {
assertType('int|string', $value);
}
}
}