Only unset loop when with_loop

Even though PHP does not complain, PHPStan does.

Since this is compiled code, we can easily produce a bit more valid code in the eyes of PHPStan.
This commit is contained in:
Ruud Kamphuis
2024-08-28 15:47:07 +02:00
parent d379eef2aa
commit 42245310ee
2 changed files with 6 additions and 2 deletions
+5 -1
View File
@@ -101,7 +101,11 @@ class ForNode extends Node
$compiler->write("\$_parent = \$context['_parent'];\n");
// remove some "private" loop variables (needed for nested loops)
$compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
$compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\']');
if ($this->getAttribute('with_loop')) {
$compiler->raw(', $context[\'loop\']');
}
$compiler->raw(");\n");
// keep the values set in the inner context for variables defined in the outer context
$compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
+1 -1
View File
@@ -62,7 +62,7 @@ foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) {
yield {$this->getVariableGetter('foo')};
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']);
unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent']);
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
EOF
];