mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Only unset _iterated when there is an else node
Same as #4249 Even though PHP does not complain, PHPStan does. > Cannot unset offset '_iterated' on array{ ... the context values ... } Since this is compiled code, we can easily produce a bit more valid code in the eyes of PHPStan.
This commit is contained in:
@@ -101,7 +101,10 @@ 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\']');
|
||||
$compiler->write('unset($context[\'_seq\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\']');
|
||||
if ($this->hasNode('else')) {
|
||||
$compiler->raw(', $context[\'_iterated\']');
|
||||
}
|
||||
if ($this->getAttribute('with_loop')) {
|
||||
$compiler->raw(', $context[\'loop\']');
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
unset(\$context['_seq'], \$context['key'], \$context['item'], \$context['_parent']);
|
||||
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
|
||||
EOF
|
||||
];
|
||||
@@ -104,7 +104,7 @@ foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
|
||||
}
|
||||
}
|
||||
\$_parent = \$context['_parent'];
|
||||
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
|
||||
unset(\$context['_seq'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
|
||||
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
|
||||
EOF
|
||||
];
|
||||
@@ -146,7 +146,7 @@ foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
|
||||
}
|
||||
}
|
||||
\$_parent = \$context['_parent'];
|
||||
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
|
||||
unset(\$context['_seq'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
|
||||
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
|
||||
EOF
|
||||
];
|
||||
@@ -193,7 +193,7 @@ if (!\$context['_iterated']) {
|
||||
yield {$this->getVariableGetter('foo')};
|
||||
}
|
||||
\$_parent = \$context['_parent'];
|
||||
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
|
||||
unset(\$context['_seq'], \$context['k'], \$context['v'], \$context['_parent'], \$context['_iterated'], \$context['loop']);
|
||||
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
|
||||
EOF
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user