forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterCastableToNumberRuleTest.php
More file actions
306 lines (280 loc) · 9.31 KB
/
ParameterCastableToNumberRuleTest.php
File metadata and controls
306 lines (280 loc) · 9.31 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\ParameterCastableToStringCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use function array_map;
use function str_replace;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<ParameterCastableToNumberRule>
*/
class ParameterCastableToNumberRuleTest extends RuleTestCase
{
private bool $checkUnion = true;
private bool $checkExplicitMixed = true;
private bool $checkImplicitMixed = true;
protected function getRule(): Rule
{
$broker = self::createReflectionProvider();
return new ParameterCastableToNumberRule(
$broker,
new ParameterCastableToStringCheck(new RuleLevelHelper($broker, true, false, $this->checkUnion, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true)),
self::getContainer()->getByType(PhpVersion::class),
);
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/param-castable-to-number-functions.php'], $this->hackPhp74ErrorMessages([
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, array<int, int>> given.',
20,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, stdClass> given.',
21,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, string> given.',
22,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, resource|false> given.',
23,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, CurlHandle> given.',
24,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, ParamCastableToNumberFunctions\\ClassWithToString> given.',
25,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, array<int, int>> given.',
27,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, stdClass> given.',
28,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, string> given.',
29,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, resource|false> given.',
30,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, CurlHandle> given.',
31,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, ParamCastableToNumberFunctions\\ClassWithToString> given.',
32,
],
]));
}
#[RequiresPhp('>= 8.0')]
public function testNamedArguments(): void
{
$this->analyse([__DIR__ . '/data/param-castable-to-number-functions-named-args.php'], [
[
'Parameter $array of function array_sum expects an array of values castable to number, array<int, array<int, int>> given.',
7,
],
[
'Parameter $array of function array_product expects an array of values castable to number, array<int, array<int, int>> given.',
8,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testEnum(): void
{
$this->analyse([__DIR__ . '/data/param-castable-to-number-functions-enum.php'], [
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, ParamCastableToNumberFunctionsEnum\\FooEnum::A> given.',
12,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, ParamCastableToNumberFunctionsEnum\\FooEnum::A> given.',
13,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug11883(): void
{
$this->analyse([__DIR__ . '/data/bug-11883.php'], [
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, Bug11883\\SomeEnum::A|Bug11883\\SomeEnum::B> given.',
13,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, Bug11883\\SomeEnum::A|Bug11883\\SomeEnum::B> given.',
14,
],
]);
}
public function testBug13775(): void
{
$errors = [
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, string> given.',
13,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, string> given.',
19,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, string> given.',
22,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, string> given.',
25,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, array> given.',
28,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, stdClass> given.',
31,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, GMP|stdClass> given.',
34,
],
];
if (PHP_VERSION_ID < 80300) {
$errors[] = [
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, GMP> given.',
37,
];
}
$this->analyse([__DIR__ . '/data/bug-13775.php'], $this->hackPhp74ErrorMessages($errors));
}
public function testBug13775Bis(): void
{
$errors = [
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, string> given.',
13,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, string> given.',
19,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, string> given.',
22,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, string> given.',
25,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, array> given.',
28,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, stdClass> given.',
31,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, GMP|stdClass> given.',
34,
],
];
if (PHP_VERSION_ID < 80300) {
$errors[] = [
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, GMP> given.',
37,
];
}
$this->analyse([__DIR__ . '/data/bug-13775-bis.php'], $this->hackPhp74ErrorMessages($errors));
}
public function testBug13775TerWithoutMixed(): void
{
$this->checkExplicitMixed = false;
$this->checkImplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-13775-ter.php'], $this->hackPhp74ErrorMessages([
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, int|string> given.',
9,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, int|string> given.',
10,
],
]));
}
public function testBug13775TerWithoutUnion(): void
{
$this->checkUnion = false;
$this->analyse([__DIR__ . '/data/bug-13775-ter.php'], $this->hackPhp74ErrorMessages([
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int, mixed> given.',
5,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int, mixed> given.',
6,
],
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.',
14,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<string> given.',
15,
],
]));
}
public function testBug12146(): void
{
$this->analyse([__DIR__ . '/data/bug-12146.php'], $this->hackPhp74ErrorMessages([
[
'Parameter #1 $array of function array_sum expects an array of values castable to number, array<int|stdClass> given.',
16,
],
[
'Parameter #1 $array of function array_product expects an array of values castable to number, array<int|stdClass> given.',
22,
],
]));
}
/**
* @param list<array{0: string, 1: int, 2?: string|null}> $errors
* @return list<array{0: string, 1: int, 2?: string|null}>
*/
private function hackPhp74ErrorMessages(array $errors): array
{
if (PHP_VERSION_ID >= 80000) {
return $errors;
}
return array_map(static function (array $error): array {
$error[0] = str_replace(
[
'$array of function array_sum',
'$array of function array_product',
'array<int, CurlHandle>',
],
[
'$input of function array_sum',
'$input of function array_product',
'array<int, resource>',
],
$error[0],
);
return $error;
}, $errors);
}
}