@@ -835,20 +835,21 @@ private function processStmtNode(
835
835
$ stmt ->expr ,
836
836
new Array_ ([]),
837
837
);
838
- $ inForeachScope = $ scope ;
839
838
if ($ stmt ->expr instanceof Variable && is_string ($ stmt ->expr ->name )) {
840
- $ inForeachScope = $ this ->processVarAnnotation ($ scope , [$ stmt ->expr ->name ], $ stmt );
839
+ $ scope = $ this ->processVarAnnotation ($ scope , [$ stmt ->expr ->name ], $ stmt );
841
840
}
842
- $ nodeCallback (new InForeachNode ($ stmt ), $ inForeachScope );
841
+ $ nodeCallback (new InForeachNode ($ stmt ), $ scope );
842
+ $ originalScope = $ scope ;
843
843
$ bodyScope = $ scope ;
844
844
845
845
if ($ context ->isTopLevel ()) {
846
- $ bodyScope = $ this ->polluteScopeWithAlwaysIterableForeach ? $ this ->enterForeach ($ scope ->filterByTruthyValue ($ arrayComparisonExpr ), $ stmt ) : $ this ->enterForeach ($ scope , $ stmt );
846
+ $ originalScope = $ this ->polluteScopeWithAlwaysIterableForeach ? $ scope ->filterByTruthyValue ($ arrayComparisonExpr ) : $ scope ;
847
+ $ bodyScope = $ this ->enterForeach ($ originalScope , $ originalScope , $ stmt );
847
848
$ count = 0 ;
848
849
do {
849
850
$ prevScope = $ bodyScope ;
850
851
$ bodyScope = $ bodyScope ->mergeWith ($ this ->polluteScopeWithAlwaysIterableForeach ? $ scope ->filterByTruthyValue ($ arrayComparisonExpr ) : $ scope );
851
- $ bodyScope = $ this ->enterForeach ($ bodyScope , $ stmt );
852
+ $ bodyScope = $ this ->enterForeach ($ bodyScope , $ originalScope , $ stmt );
852
853
$ bodyScopeResult = $ this ->processStmtNodes ($ stmt , $ stmt ->stmts , $ bodyScope , static function (): void {
853
854
}, $ context ->enterDeep ())->filterOutLoopExitPoints ();
854
855
$ bodyScope = $ bodyScopeResult ->getScope ();
@@ -867,7 +868,7 @@ private function processStmtNode(
867
868
}
868
869
869
870
$ bodyScope = $ bodyScope ->mergeWith ($ this ->polluteScopeWithAlwaysIterableForeach ? $ scope ->filterByTruthyValue ($ arrayComparisonExpr ) : $ scope );
870
- $ bodyScope = $ this ->enterForeach ($ bodyScope , $ stmt );
871
+ $ bodyScope = $ this ->enterForeach ($ bodyScope , $ originalScope , $ stmt );
871
872
$ finalScopeResult = $ this ->processStmtNodes ($ stmt , $ stmt ->stmts , $ bodyScope , $ nodeCallback , $ context )->filterOutLoopExitPoints ();
872
873
$ finalScope = $ finalScopeResult ->getScope ();
873
874
foreach ($ finalScopeResult ->getExitPointsByType (Continue_::class) as $ continueExitPoint ) {
@@ -4142,12 +4143,12 @@ private function processVarAnnotation(MutatingScope $scope, array $variableNames
4142
4143
return $ scope ;
4143
4144
}
4144
4145
4145
- private function enterForeach (MutatingScope $ scope , Foreach_ $ stmt ): MutatingScope
4146
+ private function enterForeach (MutatingScope $ scope , MutatingScope $ originalScope , Foreach_ $ stmt ): MutatingScope
4146
4147
{
4147
4148
if ($ stmt ->expr instanceof Variable && is_string ($ stmt ->expr ->name )) {
4148
4149
$ scope = $ this ->processVarAnnotation ($ scope , [$ stmt ->expr ->name ], $ stmt );
4149
4150
}
4150
- $ iterateeType = $ scope ->getType ($ stmt ->expr );
4151
+ $ iterateeType = $ originalScope ->getType ($ stmt ->expr );
4151
4152
if (
4152
4153
($ stmt ->valueVar instanceof Variable && is_string ($ stmt ->valueVar ->name ))
4153
4154
&& ($ stmt ->keyVar === null || ($ stmt ->keyVar instanceof Variable && is_string ($ stmt ->keyVar ->name )))
@@ -4157,6 +4158,7 @@ private function enterForeach(MutatingScope $scope, Foreach_ $stmt): MutatingSco
4157
4158
$ keyVarName = $ stmt ->keyVar ->name ;
4158
4159
}
4159
4160
$ scope = $ scope ->enterForeach (
4161
+ $ originalScope ,
4160
4162
$ stmt ->expr ,
4161
4163
$ stmt ->valueVar ->name ,
4162
4164
$ keyVarName ,
@@ -4180,7 +4182,7 @@ static function (): void {
4180
4182
if (
4181
4183
$ stmt ->keyVar instanceof Variable && is_string ($ stmt ->keyVar ->name )
4182
4184
) {
4183
- $ scope = $ scope ->enterForeachKey ($ stmt ->expr , $ stmt ->keyVar ->name );
4185
+ $ scope = $ scope ->enterForeachKey ($ originalScope , $ stmt ->expr , $ stmt ->keyVar ->name );
4184
4186
$ vars [] = $ stmt ->keyVar ->name ;
4185
4187
} elseif ($ stmt ->keyVar !== null ) {
4186
4188
$ scope = $ this ->processAssignVar (
0 commit comments