@@ -1491,11 +1491,26 @@ public function processStmtNode(
14911491 $ finalScope = $ finalScopeResult ->getScope ();
14921492 $ scopesWithIterableValueType = [];
14931493
1494+ $ keyVarExpr = null ;
14941495 $ originalKeyVarExpr = null ;
1495- $ continueExitPointHasUnoriginalKeyType = false ;
14961496 if ($ stmt ->keyVar instanceof Variable && is_string ($ stmt ->keyVar ->name )) {
1497+ $ keyVarExpr = $ stmt ->keyVar ;
14971498 $ originalKeyVarExpr = new OriginalForeachKeyExpr ($ stmt ->keyVar ->name );
1498- if ($ finalScope ->hasExpressionType ($ originalKeyVarExpr )->yes ()) {
1499+ }
1500+ $ originalValueExpr = null ;
1501+ if ($ stmt ->valueVar instanceof Variable && is_string ($ stmt ->valueVar ->name )) {
1502+ $ originalValueExpr = new OriginalForeachValueExpr ($ stmt ->valueVar ->name );
1503+ }
1504+
1505+ // With a key variable, each iteration is tracked through the original key
1506+ // expression and the narrowed element is projected onto the array dim fetch.
1507+ // Without one (`foreach ($a as $v)`) we instead track the original value
1508+ // expression and rewrite the array value type directly from the value var.
1509+ $ trackingExpr = $ originalKeyVarExpr ?? $ originalValueExpr ;
1510+
1511+ $ continueExitPointHasUnoriginalKeyType = false ;
1512+ if ($ trackingExpr !== null ) {
1513+ if ($ finalScope ->hasExpressionType ($ trackingExpr )->yes ()) {
14991514 $ scopesWithIterableValueType [] = $ finalScope ;
15001515 } else {
15011516 $ continueExitPointHasUnoriginalKeyType = true ;
@@ -1505,7 +1520,7 @@ public function processStmtNode(
15051520 foreach ($ finalScopeResult ->getExitPointsByType (Continue_::class) as $ continueExitPoint ) {
15061521 $ continueScope = $ continueExitPoint ->getScope ();
15071522 $ finalScope = $ continueScope ->mergeWith ($ finalScope );
1508- if ($ originalKeyVarExpr === null || !$ continueScope ->hasExpressionType ($ originalKeyVarExpr )->yes ()) {
1523+ if ($ trackingExpr === null || !$ continueScope ->hasExpressionType ($ trackingExpr )->yes ()) {
15091524 $ continueExitPointHasUnoriginalKeyType = true ;
15101525 continue ;
15111526 }
@@ -1526,58 +1541,59 @@ public function processStmtNode(
15261541 count ($ breakExitPoints ) === 0
15271542 && count ($ scopesWithIterableValueType ) > 0
15281543 && !$ continueExitPointHasUnoriginalKeyType
1529- && $ stmt -> keyVar !== null
1544+ && ( $ keyVarExpr !== null || $ originalValueExpr !== null )
15301545 && (!$ hasExpr ->no () || !$ stmt ->expr instanceof Variable)
15311546 && $ exprType ->isArray ()->yes ()
15321547 && $ exprType ->isConstantArray ()->no ()
15331548 ) {
1534- $ arrayExprDimFetch = new ArrayDimFetch ($ stmt ->expr , $ stmt ->keyVar );
1535- $ originalValueExpr = null ;
1536- if ($ stmt ->valueVar instanceof Variable && is_string ($ stmt ->valueVar ->name )) {
1537- $ originalValueExpr = new OriginalForeachValueExpr ($ stmt ->valueVar ->name );
1538- }
15391549 $ arrayDimFetchLoopTypes = [];
1540- $ keyLoopTypes = [];
1541- foreach ($ scopesWithIterableValueType as $ scopeWithIterableValueType ) {
1542- $ dimFetchType = $ scopeWithIterableValueType ->getType ($ arrayExprDimFetch );
1543- // Condition-based narrowings like `is_string($type)` apply to the value
1544- // variable but not automatically to the array dim fetch, even though the
1545- // two describe the same element for a given iteration. If the value var
1546- // hasn't been reassigned (OriginalForeachValueExpr still tracked) we use
1547- // the narrowed value-var type in place of the broader dim fetch type so
1548- // the loop's final array rewrite below picks up the sharper element type.
1549- if ($ originalValueExpr !== null && $ scopeWithIterableValueType ->hasExpressionType ($ originalValueExpr )->yes ()) {
1550- $ valueVarType = $ scopeWithIterableValueType ->getType ($ stmt ->valueVar );
1551- if ($ dimFetchType ->isSuperTypeOf ($ valueVarType )->yes ()) {
1552- $ dimFetchType = $ valueVarType ;
1553- }
1554- }
1555- $ arrayDimFetchLoopTypes [] = $ dimFetchType ;
1556- $ keyLoopTypes [] = $ scopeWithIterableValueType ->getType ($ stmt ->keyVar );
1557- }
1558-
1559- $ arrayDimFetchLoopType = TypeCombinator ::union (...$ arrayDimFetchLoopTypes );
1560- $ keyLoopType = TypeCombinator ::union (...$ keyLoopTypes );
1561-
15621550 $ arrayDimFetchLoopNativeTypes = [];
1551+ $ keyLoopTypes = [];
15631552 $ keyLoopNativeTypes = [];
15641553 foreach ($ scopesWithIterableValueType as $ scopeWithIterableValueType ) {
1565- $ dimFetchNativeType = $ scopeWithIterableValueType ->getNativeType ($ arrayExprDimFetch );
1566- if ($ originalValueExpr !== null && $ scopeWithIterableValueType ->hasExpressionType ($ originalValueExpr )->yes ()) {
1567- $ valueVarNativeType = $ scopeWithIterableValueType ->getNativeType ($ stmt ->valueVar );
1568- if ($ dimFetchNativeType ->isSuperTypeOf ($ valueVarNativeType )->yes ()) {
1569- $ dimFetchNativeType = $ valueVarNativeType ;
1554+ if ($ keyVarExpr !== null ) {
1555+ $ arrayExprDimFetch = new ArrayDimFetch ($ stmt ->expr , $ keyVarExpr );
1556+ $ dimFetchType = $ scopeWithIterableValueType ->getType ($ arrayExprDimFetch );
1557+ $ dimFetchNativeType = $ scopeWithIterableValueType ->getNativeType ($ arrayExprDimFetch );
1558+ // Condition-based narrowings like `is_string($type)` apply to the value
1559+ // variable but not automatically to the array dim fetch, even though the
1560+ // two describe the same element for a given iteration. If the value var
1561+ // hasn't been reassigned (OriginalForeachValueExpr still tracked) we use
1562+ // the narrowed value-var type in place of the broader dim fetch type so
1563+ // the loop's final array rewrite below picks up the sharper element type.
1564+ if ($ originalValueExpr !== null && $ scopeWithIterableValueType ->hasExpressionType ($ originalValueExpr )->yes ()) {
1565+ $ valueVarType = $ scopeWithIterableValueType ->getType ($ stmt ->valueVar );
1566+ if ($ dimFetchType ->isSuperTypeOf ($ valueVarType )->yes ()) {
1567+ $ dimFetchType = $ valueVarType ;
1568+ }
1569+ $ valueVarNativeType = $ scopeWithIterableValueType ->getNativeType ($ stmt ->valueVar );
1570+ if ($ dimFetchNativeType ->isSuperTypeOf ($ valueVarNativeType )->yes ()) {
1571+ $ dimFetchNativeType = $ valueVarNativeType ;
1572+ }
15701573 }
1574+ $ keyLoopTypes [] = $ scopeWithIterableValueType ->getType ($ keyVarExpr );
1575+ $ keyLoopNativeTypes [] = $ scopeWithIterableValueType ->getType ($ keyVarExpr );
1576+ } else {
1577+ // No key variable: the narrowed value var is the array element type directly.
1578+ $ dimFetchType = $ scopeWithIterableValueType ->getType ($ stmt ->valueVar );
1579+ $ dimFetchNativeType = $ scopeWithIterableValueType ->getNativeType ($ stmt ->valueVar );
15711580 }
1581+ $ arrayDimFetchLoopTypes [] = $ dimFetchType ;
15721582 $ arrayDimFetchLoopNativeTypes [] = $ dimFetchNativeType ;
1573- $ keyLoopNativeTypes [] = $ scopeWithIterableValueType ->getType ($ stmt ->keyVar );
15741583 }
15751584
1585+ $ arrayDimFetchLoopType = TypeCombinator ::union (...$ arrayDimFetchLoopTypes );
15761586 $ arrayDimFetchLoopNativeType = TypeCombinator ::union (...$ arrayDimFetchLoopNativeTypes );
1577- $ keyLoopNativeType = TypeCombinator ::union (...$ keyLoopNativeTypes );
15781587
15791588 $ valueTypeChanged = !$ arrayDimFetchLoopType ->equals ($ exprType ->getIterableValueType ());
1580- $ keyTypeChanged = !$ keyLoopType ->equals ($ exprType ->getIterableKeyType ());
1589+ $ keyTypeChanged = false ;
1590+ $ keyLoopType = $ exprType ->getIterableKeyType ();
1591+ $ keyLoopNativeType = $ scope ->getNativeType ($ stmt ->expr )->getIterableKeyType ();
1592+ if ($ keyVarExpr !== null ) {
1593+ $ keyLoopType = TypeCombinator ::union (...$ keyLoopTypes );
1594+ $ keyLoopNativeType = TypeCombinator ::union (...$ keyLoopNativeTypes );
1595+ $ keyTypeChanged = !$ keyLoopType ->equals ($ exprType ->getIterableKeyType ());
1596+ }
15811597
15821598 if ($ valueTypeChanged || $ keyTypeChanged ) {
15831599 $ newExprType = $ exprType ;
0 commit comments