Skip to content

Commit 9850ea7

Browse files
committed
StrictComparisonOfDifferentTypesRule - do not report "use match instead" when already in match
1 parent 497835e commit 9850ea7

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/Parser/LastConditionVisitor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class LastConditionVisitor extends NodeVisitorAbstract
1010
{
1111

1212
public const ATTRIBUTE_NAME = 'isLastCondition';
13+
public const ATTRIBUTE_IS_MATCH_NAME = 'isMatch';
1314

1415
public function enterNode(Node $node): ?Node
1516
{
@@ -31,7 +32,9 @@ public function enterNode(Node $node): ?Node
3132
}
3233

3334
$isLast = $i === $lastArm;
34-
$arm->conds[count($arm->conds) - 1]->setAttribute(self::ATTRIBUTE_NAME, $isLast);
35+
$index = count($arm->conds) - 1;
36+
$arm->conds[$index]->setAttribute(self::ATTRIBUTE_NAME, $isLast);
37+
$arm->conds[$index]->setAttribute(self::ATTRIBUTE_IS_MATCH_NAME, true);
3538
}
3639
}
3740

src/Rules/Comparison/StrictComparisonOfDifferentTypesRule.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ public function processNode(Node $node, Scope $scope): array
8282
$errorBuilder->addTip('Remove remaining cases below this one and this error will disappear too.');
8383
}
8484

85-
if ($leftType->isEnum()->yes() && $rightType->isEnum()->yes()) {
85+
if (
86+
$leftType->isEnum()->yes()
87+
&& $rightType->isEnum()->yes()
88+
&& $node->getAttribute(LastConditionVisitor::ATTRIBUTE_IS_MATCH_NAME, false) !== true
89+
) {
8690
$errorBuilder->addTip('Use match expression instead. PHPStan will report unhandled enum cases.');
8791
}
8892

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ public function testEnumTips(): void
955955
29,
956956
'Use match expression instead. PHPStan will report unhandled enum cases.',
957957
],
958+
[
959+
'Strict comparison using === between StrictComparisonEnumTips\SomeEnum::Two and StrictComparisonEnumTips\SomeEnum::Two will always evaluate to true.',
960+
50,
961+
'Remove remaining cases below this one and this error will disappear too.',
962+
],
958963
]);
959964
}
960965

tests/PHPStan/Rules/Comparison/data/strict-comparison-enum-tips.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,23 @@ public function exhaustiveWithSafetyCheck2(): int
3333
throw new \LogicException('New case added, handling missing');
3434
}
3535

36+
public function exhaustiveWithSafetyCheckInMatchAlready(): int
37+
{
38+
// not reported by this rule at all
39+
return match ($this) {
40+
self::One => -1,
41+
self::Two => 0,
42+
default => throw new \LogicException('New case added, handling missing'),
43+
};
44+
}
45+
46+
public function exhaustiveWithSafetyCheckInMatchAlready2(self $self): int
47+
{
48+
return match (true) {
49+
$self === self::One => -1,
50+
$self === self::Two => 0,
51+
default => throw new \LogicException('New case added, handling missing'),
52+
};
53+
}
54+
3655
}

0 commit comments

Comments
 (0)