@@ -16,9 +16,11 @@ class ImpossibleInstanceOfRuleTest extends RuleTestCase
16
16
17
17
private bool $ treatPhpDocTypesAsCertain ;
18
18
19
+ private bool $ reportAlwaysTrueInLastCondition = false ;
20
+
19
21
protected function getRule (): Rule
20
22
{
21
- return new ImpossibleInstanceOfRule ($ this ->checkAlwaysTrueInstanceOf , $ this ->treatPhpDocTypesAsCertain );
23
+ return new ImpossibleInstanceOfRule ($ this ->checkAlwaysTrueInstanceOf , $ this ->treatPhpDocTypesAsCertain , $ this -> reportAlwaysTrueInLastCondition );
22
24
}
23
25
24
26
protected function shouldTreatPhpDocTypesAsCertain (): bool
@@ -378,10 +380,12 @@ public function testBug8042(): void
378
380
[
379
381
'Instanceof between Bug8042\B and Bug8042\B will always evaluate to true. ' ,
380
382
18 ,
383
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
381
384
],
382
385
[
383
386
'Instanceof between Bug8042\B and Bug8042\B will always evaluate to true. ' ,
384
387
26 ,
388
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
385
389
],
386
390
]);
387
391
}
@@ -417,6 +421,7 @@ public function testUnreachableIfBranches(): void
417
421
[
418
422
'Instanceof between stdClass and stdClass will always evaluate to true. ' ,
419
423
37 ,
424
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
420
425
],
421
426
]);
422
427
}
@@ -433,10 +438,12 @@ public function testIfBranchesDoNotReportPhpDoc(): void
433
438
[
434
439
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
435
440
26 ,
441
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
436
442
],
437
443
[
438
444
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
439
445
36 ,
446
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
440
447
],
441
448
]);
442
449
}
@@ -454,10 +461,12 @@ public function testIfBranchesReportPhpDoc(): void
454
461
[
455
462
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
456
463
26 ,
464
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
457
465
],
458
466
[
459
467
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
460
468
36 ,
469
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
461
470
],
462
471
[
463
472
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
@@ -467,10 +476,12 @@ public function testIfBranchesReportPhpDoc(): void
467
476
[
468
477
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
469
478
52 ,
479
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
470
480
],
471
481
[
472
482
'Instanceof between UnreachableIfBranchesNotPhpDoc\Foo and UnreachableIfBranchesNotPhpDoc\Foo will always evaluate to true. ' ,
473
483
62 ,
484
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
474
485
],
475
486
]);
476
487
}
@@ -541,4 +552,37 @@ public function testBug4689(): void
541
552
$ this ->analyse ([__DIR__ . '/data/bug-4689.php ' ], []);
542
553
}
543
554
555
+ public function dataReportAlwaysTrueInLastCondition (): iterable
556
+ {
557
+ yield [false , [
558
+ [
559
+ 'Instanceof between Exception and Exception will always evaluate to true. ' ,
560
+ 21 ,
561
+ 'Remove remaining cases below this one and this error will disappear too. ' ,
562
+ ],
563
+ ]];
564
+ yield [true , [
565
+ [
566
+ 'Instanceof between Exception and Exception will always evaluate to true. ' ,
567
+ 12 ,
568
+ ],
569
+ [
570
+ 'Instanceof between Exception and Exception will always evaluate to true. ' ,
571
+ 21 ,
572
+ ],
573
+ ]];
574
+ }
575
+
576
+ /**
577
+ * @dataProvider dataReportAlwaysTrueInLastCondition
578
+ * @param list<array{0: string, 1: int, 2?: string}> $expectedErrors
579
+ */
580
+ public function testReportAlwaysTrueInLastCondition (bool $ reportAlwaysTrueInLastCondition , array $ expectedErrors ): void
581
+ {
582
+ $ this ->checkAlwaysTrueInstanceOf = true ;
583
+ $ this ->treatPhpDocTypesAsCertain = true ;
584
+ $ this ->reportAlwaysTrueInLastCondition = $ reportAlwaysTrueInLastCondition ;
585
+ $ this ->analyse ([__DIR__ . '/data/impossible-instanceof-report-always-true-last-condition.php ' ], $ expectedErrors );
586
+ }
587
+
544
588
}
0 commit comments