diff --git a/src/Parser/ForeachTrait.php b/src/Parser/ForeachTrait.php index df1c990f..f51e5a0b 100644 --- a/src/Parser/ForeachTrait.php +++ b/src/Parser/ForeachTrait.php @@ -107,25 +107,6 @@ trait ForeachTrait return $this->getIndent() . ' ' . $valueVar . ' = ' . $valueExpr . ';' . PHP_EOL; } - protected function parseForeachArray(Foreach_ $node, string $iteratorVar): string - { - $tmpVar = $this->genTmpVarName(); - $code = "for (auto $tmpVar = $iteratorVar.begin(); $tmpVar != $iteratorVar.end(); ++$tmpVar) {" . PHP_EOL; - $this->indentLevel++; - $code .= $this->parseForeachKeyAssignment($node, $tmpVar . '.key()'); - $code .= $this->parseForeachValueAssignment($node, $tmpVar . '.value()', $tmpVar . '.valueRef()'); - - $body = $this->parseForeachBody($node); - $this->indentLevel--; - - $code .= $this->parseBeforeStmtLines() . PHP_EOL; - $code .= $body . PHP_EOL; - - $code .= $this->getIndent() . '}'; - - return $code; - } - protected function parseForeachIterable(Foreach_ $node, string $iterableVar): string { $iterator = $this->genTmpVarName(); @@ -158,7 +139,7 @@ trait ForeachTrait if ($this->hasVar($name)) { $type = $this->getVarType($name); if ($type === Type::ARRAY) { - return $this->parseForeachArray($node, $name); + return $this->parseForeachIterable($node, $name); } elseif ($type === Type::OBJECT) { if ($node->byRef) { $this->fatalError($node, 'Cannot use & with foreach'); diff --git a/tests/compiler/loop/foreach-unset-current.phpt b/tests/compiler/loop/foreach-unset-current.phpt new file mode 100644 index 00000000..b28f3fc6 --- /dev/null +++ b/tests/compiler/loop/foreach-unset-current.phpt @@ -0,0 +1,29 @@ +--TEST-- +foreach by value keeps a stable array snapshot when the current element is removed +--FILE-- + 1, 'b' => 2, 'c' => 3]; + $seen = []; + + foreach ($values as $key => $value) { + $seen[] = $key . ':' . $value; + unset($values[$key]); + } + + var_dump($seen); + var_dump($values); +} +?> +--EXPECT-- +array(3) { + [0]=> + string(3) "a:1" + [1]=> + string(3) "b:2" + [2]=> + string(3) "c:3" +} +array(0) { +}