Don't unset loop when it's not defined

This commit is contained in:
Fabien Potencier
2024-07-27 09:22:35 +02:00
parent 1b9baa764b
commit 036c9943db
2 changed files with 2 additions and 2 deletions
+1 -1
View File
@@ -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");
+1 -1
View File
@@ -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;
};