Skip to content

Commit 2377075

Browse files
phpstan-botclaude
andcommitted
Simplify by-ref constant array handling via MutatingScope::enterForeach
Remove the isConstantArray()->no() guard from the with-key IntertwinedVariableByReferenceWithExpr setup in enterForeach. The ArrayDimFetch-based intertwined expression correctly targets individual elements via the key variable, so it works for constant arrays (unlike the SetExistingOffsetValueTypeExpr path which updates all values at once and must remain guarded). This lets the intertwined mechanism automatically propagate by-ref type changes to the dim fetch for constant arrays, removing the need for the elseif ($stmt->byRef) fallback branches in the post-loop rewrite in NodeScopeResolver. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2576141 commit 2377075

2 files changed

Lines changed: 1 addition & 5 deletions

File tree

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ public function enterForeach(self $originalScope, Expr $iteratee, string $valueN
23732373
if ($keyName !== null) {
23742374
$scope = $scope->enterForeachKey($originalScope, $iteratee, $keyName);
23752375

2376-
if ($valueByRef && $iterateeType->isArray()->yes() && $iterateeType->isConstantArray()->no()) {
2376+
if ($valueByRef && $iterateeType->isArray()->yes()) {
23772377
$scope = $scope->assignExpression(
23782378
new IntertwinedVariableByReferenceWithExpr($valueName, new Expr\ArrayDimFetch($iteratee, new Variable($keyName)), new Variable($valueName)),
23792379
$valueType,

src/Analyser/NodeScopeResolver.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,8 +1377,6 @@ public function processStmtNode(
13771377
if ($dimFetchType->isSuperTypeOf($valueVarType)->yes()) {
13781378
$dimFetchType = $valueVarType;
13791379
}
1380-
} elseif ($stmt->byRef && $originalValueExpr !== null) {
1381-
$dimFetchType = $scopeWithIterableValueType->getType($stmt->valueVar);
13821380
}
13831381
$arrayDimFetchLoopTypes[] = $dimFetchType;
13841382
$keyLoopTypes[] = $scopeWithIterableValueType->getType($stmt->keyVar);
@@ -1396,8 +1394,6 @@ public function processStmtNode(
13961394
if ($dimFetchNativeType->isSuperTypeOf($valueVarNativeType)->yes()) {
13971395
$dimFetchNativeType = $valueVarNativeType;
13981396
}
1399-
} elseif ($stmt->byRef && $originalValueExpr !== null) {
1400-
$dimFetchNativeType = $scopeWithIterableValueType->getNativeType($stmt->valueVar);
14011397
}
14021398
$arrayDimFetchLoopNativeTypes[] = $dimFetchNativeType;
14031399
$keyLoopNativeTypes[] = $scopeWithIterableValueType->getType($stmt->keyVar);

0 commit comments

Comments
 (0)