forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstant-array-optional-set.php
More file actions
114 lines (97 loc) · 2.57 KB
/
constant-array-optional-set.php
File metadata and controls
114 lines (97 loc) · 2.57 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
<?php
namespace ConstantArrayOptionalSet;
use function PHPStan\Testing\assertType;
class Foo
{
public function doFoo()
{
$a = [1];
if (rand(0, 1)) {
$a[] = 2;
}
assertType('array{0: 1, 1?: 2}', $a);
if (rand(0, 1)) {
$a[] = 3;
}
assertType('list{0: 1, 1?: 2|3, 2?: 3}', $a);
if (rand(0, 1)) {
$a[] = 4;
}
assertType('list{0: 1, 1?: 2|3|4, 2?: 3|4, 3?: 4}', $a);
if (rand(0, 1)) {
$a[] = 5;
}
assertType('list{0: 1, 1?: 2|3|4|5, 2?: 3|4|5, 3?: 4|5, 4?: 5}', $a);
}
public function doBar()
{
$a = [1];
if (rand(0, 1)) {
$a[] = 2;
}
assertType('array{0: 1, 1?: 2}', $a);
if (rand(0, 1)) {
$a[3] = 3;
}
assertType('array{0: 1, 1?: 2, 3?: 3}', $a);
if (rand(0, 1)) {
$a[] = 4;
}
assertType('array{0: 1, 1?: 2|4, 3?: 3, 4?: 4}', $a);
if (rand(0, 1)) {
$a[] = 5;
}
assertType('array{0: 1, 1?: 2|4|5, 3?: 3, 4?: 4|5, 5?: 5}', $a);
}
}
class Bar
{
/**
* @param non-empty-list<int>|int $nextAutoIndexes
* @return void
*/
public function doFoo($nextAutoIndexes)
{
assertType('int|non-empty-list<int>', $nextAutoIndexes);
if (is_int($nextAutoIndexes)) {
assertType('int', $nextAutoIndexes);
} else {
assertType('non-empty-list<int>', $nextAutoIndexes);
}
assertType('int|non-empty-list<int>', $nextAutoIndexes);
}
/**
* @param non-empty-list<int>|int $nextAutoIndexes
* @return void
*/
public function doBar($nextAutoIndexes)
{
assertType('int|non-empty-list<int>', $nextAutoIndexes);
if (is_int($nextAutoIndexes)) {
$nextAutoIndexes = [$nextAutoIndexes];
assertType('array{int}', $nextAutoIndexes);
} else {
assertType('non-empty-list<int>', $nextAutoIndexes);
}
assertType('non-empty-list<int>', $nextAutoIndexes);
}
}
class Baz
{
public function doFoo()
{
$conditionalArray = [1, 1, 1];
if (doFoo()) {
$conditionalArray[] = 2;
$conditionalArray[] = 3;
}
assertType('array{1, 1, 1, 2, 3}|array{1, 1, 1}', $conditionalArray);
$unshiftedConditionalArray = $conditionalArray;
array_unshift($unshiftedConditionalArray, 'lorem', new \stdClass());
assertType("array{'lorem', stdClass, 1, 1, 1, 2, 3}|array{'lorem', stdClass, 1, 1, 1}", $unshiftedConditionalArray);
assertType('array{1, 1, 1, 1, 1, 2, 3}|array{1, 1, 1, 1, 1}|array{1, 1, 1, 2, 3, 2, 3}|array{1, 1, 1, 2, 3}', $conditionalArray + $unshiftedConditionalArray);
assertType("array{'lorem', stdClass, 1, 1, 1, 2, 3}|array{'lorem', stdClass, 1, 1, 1}", $unshiftedConditionalArray + $conditionalArray);
$conditionalArray[] = 4;
assertType('array{1, 1, 1, 2, 3, 4}|array{1, 1, 1, 4}', $conditionalArray);
}
}