From 036c9943db2a38e4f63e7dc96aec22e6f7133ce9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 27 Jul 2024 09:22:35 +0200 Subject: [PATCH] Don't unset loop when it's not defined --- src/Node/ForNode.php | 2 +- tests/Node/ForTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Node/ForNode.php b/src/Node/ForNode.php index 1c980d49c..af524e7b1 100644 --- a/src/Node/ForNode.php +++ b/src/Node/ForNode.php @@ -79,7 +79,7 @@ class ForNode extends Node } // remove some "private" loop variables (needed for nested loops) - $compiler->write('unset($context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'loop\']);'."\n"); + $compiler->write('unset($context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\']'.($this->getAttribute('with_loop') ? ', $context[\'loop\']' : '').");\n"); // keep the values set in the inner context for variables defined in the outer context $compiler->write("\$context = array_intersect_key(\$context, \$$parentVar) + \$$parentVar;\n"); diff --git a/tests/Node/ForTest.php b/tests/Node/ForTest.php index 110200b58..50d6a65cb 100644 --- a/tests/Node/ForTest.php +++ b/tests/Node/ForTest.php @@ -63,7 +63,7 @@ class ForTest extends NodeTestCase foreach (\$__internal_compile_0 as \$context["key"] => \$context["item"]) { yield {$this->getVariableGetter('foo')}; } - unset(\$context['key'], \$context['item'], \$context['loop']); + unset(\$context['key'], \$context['item']); \$context = array_intersect_key(\$context, \$__internal_compile_2) + \$__internal_compile_2; return; yield; };