Skip to content

Commit

Permalink
Only unset loop when with_loop
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ruudk committed Aug 28, 2024
1 parent d379eef commit 4224531
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Node/ForNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ public function compile(Compiler $compiler): void
$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");
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/ForTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getTests()
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
];
Expand Down

0 comments on commit 4224531

Please sign in to comment.