forked from phpstan/phpstan-src
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpossibleInstanceOfRuleTest.php
546 lines (513 loc) · 17.1 KB
/
ImpossibleInstanceOfRuleTest.php
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Classes;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<ImpossibleInstanceOfRule>
*/
class ImpossibleInstanceOfRuleTest extends RuleTestCase
{
private bool $treatPhpDocTypesAsCertain;
private bool $reportAlwaysTrueInLastCondition = false;
protected function getRule(): Rule
{
return new ImpossibleInstanceOfRule(
$this->treatPhpDocTypesAsCertain,
$this->reportAlwaysTrueInLastCondition,
true,
);
}
protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return $this->treatPhpDocTypesAsCertain;
}
public function testInstanceof(): void
{
$this->treatPhpDocTypesAsCertain = true;
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse(
[__DIR__ . '/data/impossible-instanceof.php'],
[
[
'Instanceof between ImpossibleInstanceOf\Lorem and ImpossibleInstanceOf\Lorem will always evaluate to true.',
59,
],
[
'Instanceof between ImpossibleInstanceOf\Ipsum and ImpossibleInstanceOf\Lorem will always evaluate to true.',
65,
],
[
'Instanceof between ImpossibleInstanceOf\Ipsum and ImpossibleInstanceOf\Ipsum will always evaluate to true.',
68,
],
[
'Instanceof between ImpossibleInstanceOf\Dolor and ImpossibleInstanceOf\Lorem will always evaluate to false.',
71,
],
[
'Instanceof between ImpossibleInstanceOf\FooImpl and ImpossibleInstanceOf\Foo will always evaluate to true.',
74,
],
[
'Instanceof between ImpossibleInstanceOf\BarChild and ImpossibleInstanceOf\Bar will always evaluate to true.',
77,
],
[
'Instanceof between string and ImpossibleInstanceOf\Foo will always evaluate to false.',
94,
],
[
'Instanceof between string and \'str\' will always evaluate to false.',
98,
],
[
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
107,
],
[
'Instanceof between ImpossibleInstanceOf\Test|null and ImpossibleInstanceOf\Lorem will always evaluate to false.',
119,
],
[
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
124,
],
[
'Instanceof between ImpossibleInstanceOf\Test|null and ImpossibleInstanceOf\Lorem will always evaluate to false.',
137,
],
[
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
142,
],
[
'Instanceof between ImpossibleInstanceOf\Test|null and ImpossibleInstanceOf\Lorem will always evaluate to false.',
155,
],
[
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
160,
],
[
'Instanceof between callable and ImpossibleInstanceOf\FinalClassWithoutInvoke will always evaluate to false.',
204,
],
[
'Instanceof between ImpossibleInstanceOf\Dolor and ImpossibleInstanceOf\Dolor will always evaluate to true.',
226,
$tipText,
],
[
'Instanceof between *NEVER* and ImpossibleInstanceOf\Lorem will always evaluate to false.',
228,
],
[
'Instanceof between ImpossibleInstanceOf\Bar&ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Bar will always evaluate to true.',
232,
$tipText,
],
[
'Instanceof between ImpossibleInstanceOf\Bar&ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Foo will always evaluate to true.',
232,
$tipText,
],
[
'Instanceof between *NEVER* and ImpossibleInstanceOf\Foo will always evaluate to false.',
234,
],
[
'Instanceof between ImpossibleInstanceOf\Bar&ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Foo will always evaluate to true.',
238,
//$tipText,
],
[
'Instanceof between *NEVER* and ImpossibleInstanceOf\Bar will always evaluate to false.',
240,
//$tipText,
],
[
'Instanceof between object and Exception will always evaluate to false.',
303,
],
[
'Instanceof between object and InvalidArgumentException will always evaluate to false.',
307,
],
[
'Instanceof between ImpossibleInstanceOf\Bar and ImpossibleInstanceOf\BarChild will always evaluate to false.',
318,
],
[
'Instanceof between ImpossibleInstanceOf\Bar and ImpossibleInstanceOf\BarGrandChild will always evaluate to false.',
322,
],
/*[
'Instanceof between mixed and int results in an error.',
353,
],
[
'Instanceof between mixed and ImpossibleInstanceOf\InvalidTypeTest|int results in an error.',
362,
],*/
[
'Instanceof between ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Foo will always evaluate to true.',
388,
$tipText,
],
[
'Instanceof between class-string<DateTimeInterface> and DateTimeInterface will always evaluate to false.',
418,
$tipText,
],
[
'Instanceof between class-string<DateTimeInterface> and class-string<DateTimeInterface> will always evaluate to false.',
419,
$tipText,
],
[
'Instanceof between class-string<DateTimeInterface> and \'DateTimeInterface\' will always evaluate to false.',
432,
$tipText,
],
[
'Instanceof between DateTimeInterface and \'DateTimeInterface\' will always evaluate to true.',
433,
$tipText,
],
],
);
}
public function testDoNotReportTypesFromPhpDocs(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/impossible-instanceof-not-phpdoc.php'], [
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
12,
],
[
'Instanceof between stdClass and Exception will always evaluate to false.',
15,
],
[
'Instanceof between DateTimeInterface and DateTimeInterface will always evaluate to true.',
27,
],
[
'Instanceof between DateTimeInterface and ImpossibleInstanceofNotPhpDoc\SomeFinalClass will always evaluate to false.',
30,
],
]);
}
public function testReportTypesFromPhpDocs(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/impossible-instanceof-not-phpdoc.php'], [
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
12,
],
[
'Instanceof between stdClass and Exception will always evaluate to false.',
15,
],
[
'Instanceof between DateTimeImmutable and DateTimeInterface will always evaluate to true.',
27,
],
[
'Instanceof between DateTimeImmutable and ImpossibleInstanceofNotPhpDoc\SomeFinalClass will always evaluate to false.',
30,
],
[
'Instanceof between DateTimeImmutable and DateTimeImmutable will always evaluate to true.',
33,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Instanceof between DateTimeImmutable and DateTime will always evaluate to false.',
36,
//'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}
public function testBug3096(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-3096.php'], []);
}
public function testBug6213(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-6213.php'], []);
}
public function testBug5333(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/bug-5333.php'], []);
}
public function testBug8042(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('This test needs PHP 8.0');
}
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8042.php'], [
[
'Instanceof between Bug8042\B and Bug8042\B will always evaluate to true.',
18,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Instanceof between Bug8042\B and Bug8042\B will always evaluate to true.',
26,
'Remove remaining cases below this one and this error will disappear too.',
],
]);
}
public function testBug7721(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('This test needs PHP 8.1');
}
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-7721.php'], []);
}
public function testUnreachableIfBranches(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/../Comparison/data/unreachable-if-branches.php'], [
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
5,
],
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
13,
],
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
23,
],
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
37,
'Remove remaining cases below this one and this error will disappear too.',
],
]);
}
public function testIfBranchesDoNotReportPhpDoc(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/../Comparison/data/unreachable-if-branches-not-phpdoc.php'], [
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
16,
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
26,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
36,
'Remove remaining cases below this one and this error will disappear too.',
],
]);
}
public function testIfBranchesReportPhpDoc(): void
{
$this->treatPhpDocTypesAsCertain = true;
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse([__DIR__ . '/../Comparison/data/unreachable-if-branches-not-phpdoc.php'], [
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
16,
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
26,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
36,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
42,
$tipText,
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
52,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true.',
62,
'Remove remaining cases below this one and this error will disappear too.',
],
]);
}
public function testUnreachableTernaryElse(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/../Comparison/data/unreachable-ternary-else-branch.php'], [
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
6,
],
[
'Instanceof between stdClass and stdClass will always evaluate to true.',
9,
],
]);
}
public function testTernaryElseDoNotReportPhpDoc(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/../Comparison/data/unreachable-ternary-else-branch-not-phpdoc.php'], [
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
16,
],
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
17,
],
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
20,
],
]);
}
public function testTernaryElseReportPhpDoc(): void
{
$this->treatPhpDocTypesAsCertain = true;
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse([__DIR__ . '/../Comparison/data/unreachable-ternary-else-branch-not-phpdoc.php'], [
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
16,
],
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
17,
],
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
19,
$tipText,
],
[
'Instanceof between UnreachableTernaryElseBranchNotPhpDoc\Foo and UnreachableTernaryElseBranchNotPhpDoc\Foo will always evaluate to true.',
20,
],
]);
}
public function testBug4689(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/bug-4689.php'], []);
}
public function dataReportAlwaysTrueInLastCondition(): iterable
{
yield [false, [
[
'Instanceof between Exception and Exception will always evaluate to true.',
21,
'Remove remaining cases below this one and this error will disappear too.',
],
]];
yield [true, [
[
'Instanceof between Exception and Exception will always evaluate to true.',
12,
],
[
'Instanceof between Exception and Exception will always evaluate to true.',
21,
],
[
'Instanceof between DateTime and DateTime will always evaluate to true.',
34,
],
]];
}
/**
* @dataProvider dataReportAlwaysTrueInLastCondition
* @param list<array{0: string, 1: int, 2?: string}> $expectedErrors
*/
public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLastCondition, array $expectedErrors): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->reportAlwaysTrueInLastCondition = $reportAlwaysTrueInLastCondition;
$this->analyse([__DIR__ . '/data/impossible-instanceof-report-always-true-last-condition.php'], $expectedErrors);
}
public function testBug10201(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('This test needs PHP 8.1');
}
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10201.php'], [
[
'Instanceof between string and Bug10201\Hello will always evaluate to false.',
13,
],
]);
}
public function testBug3632(): void
{
$this->treatPhpDocTypesAsCertain = true;
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$this->analyse([__DIR__ . '/data/bug-3632.php'], [
[
'Instanceof between Bug3632\NiceClass and Bug3632\NiceClass will always evaluate to true.',
36,
$tipText,
],
]);
}
public function testNewIsAlwaysFinalClass(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('This test needs PHP 8.0.');
}
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/impossible-instanceof-new-is-always-final.php'], [
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
17,
],
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
33,
],
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
43,
],
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
53,
],
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
63,
],
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar|null and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
73,
],
[
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar|null and ImpossibleInstanceofNewIsAlwaysFinal\Baz will always evaluate to false.',
88,
],
]);
}
}