Skip to content

Commit 7380ed0

Browse files
committed
MatchExpressionRule - use ConstantConditionRuleHelper to remove some duplicate errors
1 parent 6e6037d commit 7380ed0

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/Rules/Comparison/MatchExpressionRule.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MatchExpressionRule implements Rule
2626
{
2727

2828
public function __construct(
29+
private ConstantConditionRuleHelper $constantConditionRuleHelper,
2930
private bool $checkAlwaysTrueStrictComparison,
3031
private bool $disableUnreachable,
3132
private bool $reportAlwaysTrueInLastCondition,
@@ -41,6 +42,7 @@ public function getNodeType(): string
4142
public function processNode(Node $node, Scope $scope): array
4243
{
4344
$matchCondition = $node->getCondition();
45+
$matchConditionType = $scope->getType($matchCondition);
4446
$nextArmIsDead = false;
4547
$errors = [];
4648
$armsCount = count($node->getArms());
@@ -67,6 +69,17 @@ public function processNode(Node $node, Scope $scope): array
6769
continue;
6870
}
6971

72+
if ($armConditionResult->getValue()) {
73+
$nextArmIsDead = true;
74+
}
75+
76+
if ($matchConditionType instanceof ConstantBooleanType) {
77+
$armConditionStandaloneResult = $this->constantConditionRuleHelper->getBooleanType($armConditionScope, $armCondition->getCondition());
78+
if (!$armConditionStandaloneResult instanceof ConstantBooleanType) {
79+
continue;
80+
}
81+
}
82+
7083
$armLine = $armCondition->getLine();
7184
if (!$armConditionResult->getValue()) {
7285
$errors[] = RuleErrorBuilder::message(sprintf(
@@ -75,7 +88,6 @@ public function processNode(Node $node, Scope $scope): array
7588
$armConditionScope->getType($armCondition->getCondition())->describe(VerbosityLevel::value()),
7689
))->line($armLine)->build();
7790
} else {
78-
$nextArmIsDead = true;
7991
if ($this->checkAlwaysTrueStrictComparison) {
8092
if ($i === $armsCount - 1 && !$this->reportAlwaysTrueInLastCondition) {
8193
continue;

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@ class MatchExpressionRuleTest extends RuleTestCase
2020

2121
protected function getRule(): Rule
2222
{
23-
return new MatchExpressionRule(true, $this->disableUnreachable, $this->reportAlwaysTrueInLastCondition);
23+
return new MatchExpressionRule(
24+
new ConstantConditionRuleHelper(
25+
new ImpossibleCheckTypeHelper(
26+
$this->createReflectionProvider(),
27+
$this->getTypeSpecifier(),
28+
[],
29+
$this->treatPhpDocTypesAsCertain,
30+
true,
31+
),
32+
$this->treatPhpDocTypesAsCertain,
33+
true,
34+
),
35+
true,
36+
$this->disableUnreachable,
37+
$this->reportAlwaysTrueInLastCondition,
38+
);
2439
}
2540

2641
protected function shouldTreatPhpDocTypesAsCertain(): bool
@@ -92,14 +107,6 @@ public function testRule(): void
92107
'Match expression does not handle remaining values: 1|2|3',
93108
78,
94109
],
95-
[
96-
'Match arm comparison between true and false is always false.',
97-
86,
98-
],
99-
[
100-
'Match arm comparison between true and false is always false.',
101-
92,
102-
],
103110
[
104111
'Match expression does not handle remaining value: true',
105112
90,

tests/PHPStan/Rules/Comparison/data/match-expr.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ public function doFoo(int $i): void
8383
public function doBar(\Exception $e): void
8484
{
8585
match (true) {
86-
$e instanceof \InvalidArgumentException, $e instanceof \InvalidArgumentException => true,
86+
$e instanceof \InvalidArgumentException, $e instanceof \InvalidArgumentException => true, // reported by ImpossibleInstanceOfRule
8787
default => null,
8888
};
8989

9090
match (true) {
9191
$e instanceof \InvalidArgumentException => true,
92-
$e instanceof \InvalidArgumentException => true,
92+
$e instanceof \InvalidArgumentException => true, // reported by ImpossibleInstanceOfRule
9393
};
9494
}
9595

0 commit comments

Comments
 (0)