Skip to content

Commit a327965

Browse files
committed
StrictComparisonOfDifferentTypesRule - tip for always true comparison between enums
1 parent e06c529 commit a327965

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/Rules/Comparison/StrictComparisonOfDifferentTypesRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ public function processNode(Node $node, Scope $scope): array
7979
$rightType->describe(VerbosityLevel::value()),
8080
)));
8181
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
82-
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
82+
$errorBuilder->addTip('Remove remaining cases below this one and this error will disappear too.');
83+
}
84+
85+
if ($leftType->isEnum()->yes() && $rightType->isEnum()->yes()) {
86+
$errorBuilder->addTip('Use match expression instead. PHPStan will report unhandled enum cases.');
8387
}
8488

8589
return [

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ public function testBug8485(): void
637637
[
638638
'Strict comparison using === between Bug8485\E::c and Bug8485\E::c will always evaluate to true.',
639639
19,
640+
'Use match expression instead. PHPStan will report unhandled enum cases.',
640641
],
641642
[
642643
'Strict comparison using === between Bug8485\F::c and Bug8485\E::c will always evaluate to false.',
@@ -657,12 +658,12 @@ public function testBug8485(): void
657658
[
658659
'Strict comparison using === between Bug8485\FooEnum::C and Bug8485\FooEnum::C will always evaluate to true.',
659660
67,
660-
'Remove remaining cases below this one and this error will disappear too.',
661+
"Remove remaining cases below this one and this error will disappear too.\n• Use match expression instead. PHPStan will report unhandled enum cases.",
661662
],
662663
[
663664
'Strict comparison using === between Bug8485\FooEnum::C and Bug8485\FooEnum::C will always evaluate to true.',
664665
74,
665-
'Remove remaining cases below this one and this error will disappear too.',
666+
"Remove remaining cases below this one and this error will disappear too.\n• Use match expression instead. PHPStan will report unhandled enum cases.",
666667
],
667668
]);
668669
}
@@ -936,4 +937,25 @@ public function testBug9104(): void
936937
]);
937938
}
938939

940+
public function testEnumTips(): void
941+
{
942+
if (PHP_VERSION_ID < 80100) {
943+
$this->markTestSkipped('Test requires PHP 8.1.');
944+
}
945+
946+
$this->checkAlwaysTrueStrictComparison = true;
947+
$this->analyse([__DIR__ . '/data/strict-comparison-enum-tips.php'], [
948+
[
949+
'Strict comparison using === between $this(StrictComparisonEnumTips\SomeEnum)&StrictComparisonEnumTips\SomeEnum::Two and StrictComparisonEnumTips\SomeEnum::Two will always evaluate to true.',
950+
15,
951+
"• Remove remaining cases below this one and this error will disappear too.\n• Use match expression instead. PHPStan will report unhandled enum cases.",
952+
],
953+
[
954+
'Strict comparison using === between $this(StrictComparisonEnumTips\SomeEnum)&StrictComparisonEnumTips\SomeEnum::Two and StrictComparisonEnumTips\SomeEnum::Two will always evaluate to true.',
955+
29,
956+
'Use match expression instead. PHPStan will report unhandled enum cases.',
957+
],
958+
]);
959+
}
960+
939961
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php // lint >= 8.1
2+
3+
namespace StrictComparisonEnumTips;
4+
5+
enum SomeEnum
6+
{
7+
8+
case One;
9+
case Two;
10+
11+
public function exhaustiveWithSafetyCheck(): int
12+
{
13+
if ($this === self::One) {
14+
return -1;
15+
} elseif ($this === self::Two) {
16+
return 0;
17+
} else {
18+
throw new \LogicException('New case added, handling missing');
19+
}
20+
}
21+
22+
23+
public function exhaustiveWithSafetyCheck2(): int
24+
{
25+
if ($this === self::One) {
26+
return -1;
27+
}
28+
29+
if ($this === self::Two) {
30+
return 0;
31+
}
32+
33+
throw new \LogicException('New case added, handling missing');
34+
}
35+
36+
}

0 commit comments

Comments
 (0)