Skip to content

Commit 96c0249

Browse files
phpstan-botclaude
andcommitted
Merge two foreach loops over conditionalExpressions into a single loop
Addresses review feedback: the exact-match and isSuperTypeOf-fallback passes now happen in a single iteration over conditionalExpressions, collecting exactMatches and superTypeMatches separately, then deciding which to use after the loop. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0e31ce7 commit 96c0249

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/Analyser/MutatingScope.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,39 +3230,38 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
32303230
continue;
32313231
}
32323232

3233-
// Pass 1: exact matches
3233+
$exactMatches = [];
3234+
$superTypeMatches = [];
32343235
foreach ($conditionalExpressions as $conditionalExpression) {
32353236
$allExact = true;
3237+
$allSuperType = true;
32363238
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
3237-
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
3239+
if (!array_key_exists($holderExprString, $specifiedExpressions)) {
32383240
$allExact = false;
3241+
$allSuperType = false;
32393242
break;
32403243
}
3244+
if (!$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
3245+
$allExact = false;
3246+
if (!$this->conditionalExpressionHolderMatches($specifiedExpressions[$holderExprString], $conditionalTypeHolder)) {
3247+
$allSuperType = false;
3248+
break;
3249+
}
3250+
}
32413251
}
32423252
if ($allExact) {
3243-
$conditions[$conditionalExprString][] = $conditionalExpression;
3244-
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
3253+
$exactMatches[] = $conditionalExpression;
3254+
} elseif ($allSuperType) {
3255+
$superTypeMatches[] = $conditionalExpression;
32453256
}
32463257
}
32473258

3248-
if (array_key_exists($conditionalExprString, $conditions)) {
3249-
continue;
3250-
}
3251-
3252-
// Pass 2: isSuperTypeOf fallback when no exact match exists
3253-
$superTypeMatches = [];
3254-
$totalConditionalCount = count($conditionalExpressions);
3255-
foreach ($conditionalExpressions as $conditionalExpression) {
3256-
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
3257-
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$this->conditionalExpressionHolderMatches($specifiedExpressions[$holderExprString], $conditionalTypeHolder)) {
3258-
continue 2;
3259-
}
3259+
if (count($exactMatches) > 0) {
3260+
foreach ($exactMatches as $exactMatch) {
3261+
$conditions[$conditionalExprString][] = $exactMatch;
3262+
$specifiedExpressions[$conditionalExprString] = $exactMatch->getTypeHolder();
32603263
}
3261-
3262-
$superTypeMatches[] = $conditionalExpression;
3263-
}
3264-
3265-
if (count($superTypeMatches) === 1 && $totalConditionalCount >= 2) {
3264+
} elseif (count($superTypeMatches) === 1 && count($conditionalExpressions) >= 2) {
32663265
$conditions[$conditionalExprString][] = $superTypeMatches[0];
32673266
}
32683267
}

0 commit comments

Comments
 (0)