@@ -761,12 +761,16 @@ private function processStmtNode(
761
761
} elseif ($ stmt instanceof Foreach_) {
762
762
$ condResult = $ this ->processExprNode ($ stmt ->expr , $ scope , $ nodeCallback , ExpressionContext::createDeep ());
763
763
$ scope = $ condResult ->getScope ();
764
- $ bodyScope = $ this ->enterForeach ($ scope , $ stmt );
764
+ $ arrayComparisonExpr = new BinaryOp \NotIdentical (
765
+ $ stmt ->expr ,
766
+ new Array_ ([])
767
+ );
768
+ $ bodyScope = $ this ->enterForeach ($ scope ->filterByTruthyValue ($ arrayComparisonExpr ), $ stmt );
765
769
$ hasYield = false ;
766
770
$ count = 0 ;
767
771
do {
768
772
$ prevScope = $ bodyScope ;
769
- $ bodyScope = $ bodyScope ->mergeWith ($ scope );
773
+ $ bodyScope = $ bodyScope ->mergeWith ($ scope-> filterByTruthyValue ( $ arrayComparisonExpr ) );
770
774
$ bodyScope = $ this ->enterForeach ($ bodyScope , $ stmt );
771
775
$ bodyScopeResult = $ this ->processStmtNodes ($ stmt , $ stmt ->stmts , $ bodyScope , static function (): void {
772
776
})->filterOutLoopExitPoints ();
@@ -785,7 +789,7 @@ private function processStmtNode(
785
789
$ count ++;
786
790
} while (!$ alwaysTerminating && $ count < self ::LOOP_SCOPE_ITERATIONS );
787
791
788
- $ bodyScope = $ bodyScope ->mergeWith ($ scope );
792
+ $ bodyScope = $ bodyScope ->mergeWith ($ scope-> filterByTruthyValue ( $ arrayComparisonExpr ) );
789
793
$ bodyScope = $ this ->enterForeach ($ bodyScope , $ stmt );
790
794
$ finalScopeResult = $ this ->processStmtNodes ($ stmt , $ stmt ->stmts , $ bodyScope , $ nodeCallback )->filterOutLoopExitPoints ();
791
795
$ finalScope = $ finalScopeResult ->getScope ();
@@ -801,11 +805,7 @@ private function processStmtNode(
801
805
$ finalScope = $ scope ;
802
806
} elseif ($ isIterableAtLeastOnce ->maybe ()) {
803
807
if ($ this ->polluteScopeWithAlwaysIterableForeach ) {
804
- $ arrayComparisonExpr = new BinaryOp \NotIdentical (
805
- $ stmt ->expr ,
806
- new Array_ ([])
807
- );
808
- $ finalScope = $ finalScope ->filterByTruthyValue ($ arrayComparisonExpr )->mergeWith ($ scope ->filterByFalseyValue ($ arrayComparisonExpr ));
808
+ $ finalScope = $ finalScope ->mergeWith ($ scope ->filterByFalseyValue ($ arrayComparisonExpr ));
809
809
} else {
810
810
$ finalScope = $ finalScope ->mergeWith ($ scope );
811
811
}
0 commit comments