forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-14177.php
More file actions
224 lines (200 loc) · 5.87 KB
/
bug-14177.php
File metadata and controls
224 lines (200 loc) · 5.87 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php declare(strict_types = 1);
namespace Bug14177Nsrt;
use function PHPStan\Testing\assertType;
class HelloWorld
{
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
*/
public function testList(array $b): void
{
if (array_key_exists(3, $b)) {
assertType('array{string, string, string, string}', $b);
} else {
assertType('array{0: string, 1: string, 2?: string}', $b);
}
assertType('list{0: string, 1: string, 2?: string, 3?: string}', $b);
}
public function placeholderToEditor(string $html): void
{
$result = preg_replace_callback(
'~\[image\\sid="(\\d+)"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?\]~',
function (array $matches): string {
$id = (int) $matches[1];
assertType('list{0: non-falsy-string, 1: numeric-string, 2?: string, 3?: string}', $matches);
$replacement = sprintf(
'<img src="%s"%s/>',
$id,
array_key_exists(3, $matches) ? sprintf(' class="%s"', $matches[3]) : '',
);
assertType('list{0: non-falsy-string, 1: numeric-string, 2?: string, 3?: string}', $matches);
return array_key_exists(2, $matches) && $matches[2] !== ''
? sprintf('<a href="%s">%s</a>', $matches[2], $replacement)
: $replacement;
},
$html,
);
}
public function placeholderToEditor2(string $html): void
{
$result = preg_replace_callback(
'~\[image\\sid="(\\d+)?"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?\]~',
function (array $matches): string {
$id = (int) $matches[0];
assertType('list{0: non-falsy-string, 1?: \'\'|numeric-string, 2?: string, 3?: string}', $matches);
$replacement = sprintf(
'<img src="%s"%s/>',
$id,
array_key_exists(2, $matches) ? sprintf(' class="%s"', $matches[2]) : '',
);
assertType('list{0: non-falsy-string, 1?: \'\'|numeric-string, 2?: string, 3?: string}', $matches);
return array_key_exists(1, $matches) && $matches[1] !== ''
? sprintf('<a href="%s">%s</a>', $matches[1], $replacement)
: $replacement;
},
$html,
);
}
}
class HelloWorld2
{
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
*/
public function testUnset0OnList(array $b): void
{
assertType('true', array_is_list($b));
unset($b[0]);
assertType('false', array_is_list($b));
$b[] = 'foo';
assertType('false', array_is_list($b));
}
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
*/
public function testUnset1OnList(array $b): void
{
assertType('true', array_is_list($b));
unset($b[1]);
assertType('false', array_is_list($b));
$b[] = 'foo';
assertType('false', array_is_list($b));
}
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
*/
public function testUnset2OnList(array $b): void
{
assertType('true', array_is_list($b));
unset($b[2]);
assertType('bool', array_is_list($b));
$b[] = 'foo';
assertType('bool', array_is_list($b));
}
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
*/
public function testUnset3OnList(array $b): void
{
assertType('true', array_is_list($b));
unset($b[3]);
assertType('bool', array_is_list($b));
$b[] = 'foo';
assertType('bool', array_is_list($b));
}
/**
* @param array{0: string, 1?: string, 2: string, 3?: string} $b
*/
public function testUnset0OnArray(array $b): void
{
assertType('bool', array_is_list($b));
unset($b[0]);
assertType('false', array_is_list($b));
$b[] = 'foo';
assertType('false', array_is_list($b));
}
/**
* @param array{0: string, 1?: string, 2: string, 3?: string} $b
*/
public function testUnset1OnArray(array $b): void
{
assertType('bool', array_is_list($b));
unset($b[1]);
assertType('false', array_is_list($b));
$b[] = 'foo';
assertType('false', array_is_list($b));
}
/**
* @param array{0: string, 1?: string, 2: string, 3?: string} $b
*/
public function testUnset2OnArray(array $b): void
{
assertType('bool', array_is_list($b));
unset($b[2]);
assertType('false', array_is_list($b));
$b[] = 'foo';
assertType('false', array_is_list($b));
}
/**
* @param array{0: string, 1?: string, 2: string, 3?: string} $b
*/
public function testUnset3OnArray(array $b): void
{
assertType('bool', array_is_list($b));
unset($b[3]);
assertType('bool', array_is_list($b));
$b[] = 'foo';
assertType('bool', array_is_list($b));
}
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $a
* @param list{0: string, 1?: string, 2?: string, 3?: string} $b
* @param list{0: string, 1?: string, 2?: string, 3: string} $c
* @param 1|2 $int
*/
public function testUnsetNonConstant(array $a, array $b, array $c, int $int): void
{
assertType('true', array_is_list($a));
assertType('true', array_is_list($b));
assertType('true', array_is_list($c));
unset($a[$int]);
unset($b[$int]);
unset($c[$int]);
assertType('false', array_is_list($a));
assertType('bool', array_is_list($b));
assertType('bool', array_is_list($c));
}
/**
* @param list{0?: string, 1?: string, 2?: string, 3?: string} $a
* @param list{0: string, 1?: string, 2?: string, 3?: string} $b
*/
public function testUnsetInt(array $a, array $b, array $c, int $int): void
{
assertType('true', array_is_list($a));
assertType('true', array_is_list($b));
unset($a[$int]);
unset($b[$int]);
assertType('bool', array_is_list($a));
assertType('false', array_is_list($b));
}
/**
* @param list{0?: string, 1?: string, 2?: string} $l
*/
public function testFoo($l): void
{
if (array_key_exists(2, $l, true)) {
assertType('true', array_is_list($l));
assertType('list{0?: string, 1?: string, 2: string}', $l);
if (array_key_exists(1, $l, true)) {
assertType('true', array_is_list($l));
assertType('list{0?: string, 1: string, 2: string}', $l);
} else {
assertType('true', array_is_list($l));
assertType('*NEVER*', $l);
}
} else {
assertType('true', array_is_list($l));
assertType('list{0?: string, 1?: string}', $l);
}
}
}