Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Rules/Comparison/MatchExpressionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

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

Expand Down
12 changes: 5 additions & 7 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,19 @@ public static function dataReportAlwaysTrueInLastCondition(): iterable
],
]];
yield [true, [
[
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
15,
],
[
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
23,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
45,
49,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Match arm comparison between $this(MatchAlwaysTrueLastArm\Foo)&MatchAlwaysTrueLastArm\Foo::BAR and MatchAlwaysTrueLastArm\Foo::BAR is always true.',
49,
'Match arm comparison between false and false is always true.',
58,
],
]];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public function doMoreConditionsInLastArm(): void
};
}

public function doNonEnum(bool $a): void
{
match ($a) {
true => 0,
false => 1,
};
}

}
Loading