Skip to content

Commit 3f3deaa

Browse files
phpstan-botclaude
andcommitted
Simplify propagateRefTypeToArrayDimFetch to use single loop
Replace the two-pass approach (forward pass to collect intermediate types, backward pass to rebuild) with a single while loop that walks from leaf to root, getting each parent's type directly from scope. Also remove unused array_reverse import. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2a99875 commit 3f3deaa

1 file changed

Lines changed: 13 additions & 42 deletions

File tree

src/Analyser/MutatingScope.php

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@
109109
use function array_map;
110110
use function array_merge;
111111
use function array_pop;
112-
use function array_reverse;
113112
use function array_slice;
114113
use function array_unique;
115114
use function array_values;
@@ -2671,53 +2670,25 @@ private function propagateRefTypeToArrayDimFetch(
26712670
string $variableName,
26722671
): self
26732672
{
2674-
// Collect the chain of ArrayDimFetch from leaf to root
2675-
$dimStack = [];
2676-
$current = $targetExpr;
2677-
while ($current instanceof Expr\ArrayDimFetch) {
2678-
$dimStack[] = $current->dim;
2679-
$current = $current->var;
2680-
}
2681-
2682-
// Build intermediate types from root to leaf
2683-
$dimStack = array_reverse($dimStack);
2684-
$rootType = $scope->getType($current);
2685-
$rootNativeType = $scope->getNativeType($current);
2686-
2687-
$intermediateTypes = [$rootType];
2688-
$intermediateNativeTypes = [$rootNativeType];
2689-
$currentType = $rootType;
2690-
$currentNativeType = $rootNativeType;
2691-
for ($i = 0; $i < count($dimStack) - 1; $i++) {
2692-
$dim = $dimStack[$i];
2693-
if ($dim === null) {
2694-
return $scope->assignExpression($targetExpr, $newType, $newNativeType);
2695-
}
2696-
$dimType = $scope->getType($dim)->toArrayKey();
2697-
$currentType = $currentType->getOffsetValueType($dimType);
2698-
$currentNativeType = $currentNativeType->getOffsetValueType($dimType);
2699-
$intermediateTypes[] = $currentType;
2700-
$intermediateNativeTypes[] = $currentNativeType;
2701-
}
2673+
$expr = $targetExpr;
2674+
$type = $newType;
2675+
$nativeType = $newNativeType;
27022676

2703-
// Build up from the leaf using setOffsetValueType
2704-
$builtType = $newType;
2705-
$builtNativeType = $newNativeType;
2706-
for ($i = count($dimStack) - 1; $i >= 0; $i--) {
2707-
$dim = $dimStack[$i];
2708-
if ($dim === null) {
2677+
while ($expr instanceof Expr\ArrayDimFetch) {
2678+
if ($expr->dim === null) {
27092679
return $scope->assignExpression($targetExpr, $newType, $newNativeType);
27102680
}
2711-
$dimType = $scope->getType($dim)->toArrayKey();
2712-
$builtType = $intermediateTypes[$i]->setOffsetValueType($dimType, $builtType);
2713-
$builtNativeType = $intermediateNativeTypes[$i]->setOffsetValueType($dimType, $builtNativeType);
2681+
$dimType = $scope->getType($expr->dim)->toArrayKey();
2682+
$type = $scope->getType($expr->var)->setOffsetValueType($dimType, $type);
2683+
$nativeType = $scope->getNativeType($expr->var)->setOffsetValueType($dimType, $nativeType);
2684+
$expr = $expr->var;
27142685
}
27152686

2716-
if ($current instanceof Variable && is_string($current->name)) {
2687+
if ($expr instanceof Variable && is_string($expr->name)) {
27172688
return $scope->assignVariable(
2718-
$current->name,
2719-
$builtType,
2720-
$builtNativeType,
2689+
$expr->name,
2690+
$type,
2691+
$nativeType,
27212692
TrinaryLogic::createYes(),
27222693
array_merge($intertwinedPropagatedFrom, [$variableName]),
27232694
);

0 commit comments

Comments
 (0)