forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-11311.php
More file actions
226 lines (193 loc) · 8.55 KB
/
bug-11311.php
File metadata and controls
226 lines (193 loc) · 8.55 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
225
226
<?php
namespace Bug11311;
use function PHPStan\Testing\assertType;
use InvalidArgumentException;
function doFoo(string $s) {
if (1 === preg_match('/(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: non-falsy-string, major: numeric-string, 1: numeric-string, minor: numeric-string, 2: numeric-string, patch: numeric-string|null, 3: numeric-string|null}', $matches);
}
}
function doUnmatchedAsNull(string $s): void {
if (preg_match('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType("array{string, 'foo'|null, 'bar'|null, 'baz'|null}", $matches);
}
assertType("array{}|array{string, 'foo'|null, 'bar'|null, 'baz'|null}", $matches);
}
// see https://3v4l.org/VeDob
function unmatchedAsNullWithOptionalGroup(string $s): void {
if (preg_match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
// with PREG_UNMATCHED_AS_NULL the offset 1 will always exist. It is correct that it's nullable because it's optional though
assertType("array{non-falsy-string, '£'|'€'|null}", $matches);
} else {
assertType('array{}', $matches);
}
assertType("array{}|array{non-falsy-string, '£'|'€'|null}", $matches);
}
function bug11331a(string $url):void {
// group a is actually optional as the entire (?:...) around it is optional
if (preg_match('{^
(?:
(?<a>.+)
)?
(?<b>.+)}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: non-empty-string, a: non-empty-string|null, 1: non-empty-string|null, b: non-empty-string, 2: non-empty-string}', $matches);
}
}
function bug11331b(string $url):void {
if (preg_match('{^
(?:
(?<a>.+)
)?
(?<b>.+)?}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, a: non-empty-string|null, 1: non-empty-string|null, b: non-empty-string|null, 2: non-empty-string|null}', $matches);
}
}
function bug11331c(string $url):void {
if (preg_match('{^
(?:
(?:https?|git)://([^/]+)/ (?# group 1 here can be null if group 2 matches)
| (?# the alternation making it so that only either should match)
git@([^:]+):/? (?# group 2 here can be null if group 1 matches)
)
([^/]+)
/
([^/]+?)
(?:\.git|/)?
$}x', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{non-falsy-string, non-empty-string|null, non-empty-string|null, non-empty-string, non-empty-string}', $matches);
}
}
class UnmatchedAsNullWithTopLevelAlternation {
function doFoo(string $s): void {
if (preg_match('/Price: (?:(£)|(€))\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType("array{non-falsy-string, '£'|null, '€'|null}", $matches); // could be tagged union
}
}
function doBar(string $s): void {
if (preg_match('/Price: (?:(£)|(€))?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType("array{non-falsy-string, '£'|null, '€'|null}", $matches); // could be tagged union
}
}
}
function (string $size): void {
if (preg_match('/ab(\d){2,4}xx([0-9])?e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, numeric-string, numeric-string|null}', $matches);
};
function (string $size): void {
if (preg_match('/a(\dAB){2}b(\d){2,4}([1-5])([1-5a-z])e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-falsy-string, numeric-string, numeric-string, non-empty-string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(ab(\d)){2,4}xx([0-9][a-c])?e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-falsy-string, numeric-string, non-falsy-string|null}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\d+)e(\d?)/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType("array{non-falsy-string, numeric-string, ''|numeric-string}", $matches);
};
function (string $size): void {
if (preg_match('/ab(?P<num>\d+)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{0: non-falsy-string, num: numeric-string, 1: numeric-string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\d\d)/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-falsy-string&numeric-string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\d+\s)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-falsy-string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\s)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-empty-string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\S)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-empty-string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\S?)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, string}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\S)?e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, non-empty-string|null}', $matches);
};
function (string $size): void {
if (preg_match('/ab(\d+\d?)e?/', $size, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new InvalidArgumentException(sprintf('Invalid size "%s"', $size));
}
assertType('array{non-falsy-string, numeric-string}', $matches);
};
function (string $s): void {
if (preg_match('/Price: ([2-5])/i', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{non-falsy-string, numeric-string}', $matches);
}
};
function (string $s): void {
if (preg_match('/Price: ([2-5A-Z])/i', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{non-falsy-string, non-empty-string}', $matches);
}
};
function (string $s): void {
if (preg_match('/^%([0-9]*\$)?[0-9]*\.?[0-9]*([sbdeEfFgGhHouxX])$/', $s, $matches, PREG_UNMATCHED_AS_NULL) === 1) {
assertType("array{non-falsy-string, non-falsy-string|null, 'b'|'d'|'E'|'e'|'F'|'f'|'G'|'g'|'H'|'h'|'o'|'s'|'u'|'X'|'x'}", $matches);
}
};
function (string $s): void {
if (preg_match('/(?<whitespace>\s*)(?<value>.*)/', $s, $matches, PREG_UNMATCHED_AS_NULL) === 1) {
assertType('array{0: string, whitespace: string, 1: string, value: string, 2: string}', $matches);
}
};
function (string $s): void {
preg_match('/%a(\d*)/', $s, $matches, PREG_UNMATCHED_AS_NULL);
assertType("array{}|array{non-falsy-string, ''|numeric-string}", $matches); // could be array{0?: string, 1?: ''|numeric-string}
};
function (string $s): void {
preg_match('/%a(\d*)?/', $s, $matches, PREG_UNMATCHED_AS_NULL);
assertType("array{}|array{non-falsy-string, ''|numeric-string|null}", $matches); // could be array{0?: string, 1?: ''|numeric-string}
};
function (string $s): void {
if (preg_match('~a|(\d)|(\s)~', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType("array{non-empty-string, numeric-string|null, non-empty-string|null}", $matches);
} else {
assertType("array{}", $matches);
}
assertType("array{}|array{non-empty-string, numeric-string|null, non-empty-string|null}", $matches);
};
function (string $s): void {
if (preg_match('~a|(\d)|(\s)~', $s, $matches, PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE) === 1) {
assertType("array{array{non-empty-string|null, int<-1, max>}, array{numeric-string|null, int<-1, max>}, array{non-empty-string|null, int<-1, max>}}", $matches);
}
};
function (string $s): void {
if (preg_match('~a|((u)x)|((v)y)~', $s, $matches, PREG_UNMATCHED_AS_NULL) === 1) {
assertType("array{non-empty-string, 'ux'|null, 'u'|null, 'vy'|null, 'v'|null}", $matches);
}
};
function (string $s): void {
preg_match('~a|(\d)|(\s)~', $s, $matches, PREG_UNMATCHED_AS_NULL);
assertType("array{}|array{non-empty-string, numeric-string|null, non-empty-string|null}", $matches);
};