Skip to content

Commit 0fbb507

Browse files
vranaondrejmirtes
authored andcommitted
MatchExpressionRule - ignore reportAlwaysTrueInLastCondition with enums
1 parent 490050c commit 0fbb507

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/Rules/Comparison/MatchExpressionRule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ public function processNode(Node $node, Scope $scope): array
105105
continue;
106106
}
107107

108-
if ($i === $armsCount - 1 && !$this->reportAlwaysTrueInLastCondition) {
108+
$reportAlwaysTrueInLastCondition = $this->reportAlwaysTrueInLastCondition && $matchConditionType->getEnumCases() === [];
109+
if ($i === $armsCount - 1 && !$reportAlwaysTrueInLastCondition) {
109110
continue;
110111
}
111112
$errorBuilder = RuleErrorBuilder::message(sprintf(
112113
'Match arm comparison between %s and %s is always true.',
113114
$armConditionScope->getType($matchCondition)->describe(VerbosityLevel::value()),
114115
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
115116
))->line($armLine);
116-
if ($i !== $armsCount - 1 && !$this->reportAlwaysTrueInLastCondition) {
117+
if ($i !== $armsCount - 1 && !$reportAlwaysTrueInLastCondition) {
117118
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
118119
}
119120

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,19 @@ public static function dataReportAlwaysTrueInLastCondition(): iterable
295295
],
296296
]];
297297
yield [true, [
298-
[
299-
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
300-
15,
301-
],
302298
[
303299
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
304300
23,
301+
'Remove remaining cases below this one and this error will disappear too.',
305302
],
306303
[
307304
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
308-
45,
305+
49,
306+
'Remove remaining cases below this one and this error will disappear too.',
309307
],
310308
[
311-
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
312-
49,
309+
'Match arm comparison between false and false is always true.',
310+
58,
313311
],
314312
]];
315313
}

tests/PHPStan/Rules/Comparison/data/match-always-true-last-arm.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ public function doMoreConditionsInLastArm(): void
5151
};
5252
}
5353

54+
public function doNonEnum(bool $a): void
55+
{
56+
match ($a) {
57+
true => 0,
58+
false => 1,
59+
};
60+
}
61+
5462
}

0 commit comments

Comments
 (0)