Skip to content

Commit bfb99f5

Browse files
committed
Fix double processing of match with enum condition
1 parent 3a0da5b commit bfb99f5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,6 +4090,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
40904090
$hasDefaultCond = false;
40914091
$hasAlwaysTrueCond = false;
40924092
$arms = $expr->arms;
4093+
$armCondsToSkip = [];
40934094
if ($condType->isEnum()->yes()) {
40944095
// enum match analysis would work even without this if branch
40954096
// but would be much slower
@@ -4110,7 +4111,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
41104111
$condNodes = [];
41114112
$conditionCases = [];
41124113
$conditionExprs = [];
4113-
foreach ($arm->conds as $cond) {
4114+
foreach ($arm->conds as $j => $cond) {
41144115
if (!$cond instanceof Expr\ClassConstFetch) {
41154116
continue 2;
41164117
}
@@ -4170,6 +4171,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
41704171
$conditionExprs[] = $cond;
41714172

41724173
unset($unusedIndexedEnumCases[$loweredFetchedClassName][$caseName]);
4174+
$armCondsToSkip[$i][$j] = true;
41734175
}
41744176

41754177
$conditionCasesCount = count($conditionCases);
@@ -4246,7 +4248,10 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
42464248
$filteringExprs = [];
42474249
$armCondScope = $matchScope;
42484250
$condNodes = [];
4249-
foreach ($arm->conds as $armCond) {
4251+
foreach ($arm->conds as $j => $armCond) {
4252+
if (isset($armCondsToSkip[$i][$j])) {
4253+
continue;
4254+
}
42504255
$condNodes[] = new MatchExpressionArmCondition($armCond, $armCondScope, $armCond->getStartLine());
42514256
$armCondResult = $this->processExprNode($stmt, $armCond, $armCondScope, $storage, $nodeCallback, $deepContext);
42524257
$hasYield = $hasYield || $armCondResult->hasYield();

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ public function testEnums(): void
148148
'Match arm comparison between MatchEnums\Foo and MatchEnums\Foo::ONE is always false.',
149149
104,
150150
],
151-
[
152-
'Match arm comparison between *NEVER* and MatchEnums\Foo::ONE is always false.',
153-
113,
154-
],
155151
[
156152
'Match arm comparison between *NEVER* and MatchEnums\DifferentEnum::ONE is always false.',
157153
113,

0 commit comments

Comments
 (0)