diff --git a/src/Node/ForNode.php b/src/Node/ForNode.php index 5283fe55a..63681f754 100644 --- a/src/Node/ForNode.php +++ b/src/Node/ForNode.php @@ -65,7 +65,7 @@ class ForNode extends Node if ($this->hasNode('else')) { $compiler - ->write("if (!\${$loopName}->iterated()) {\n") + ->write("if (0 === \${$loopName}->getIndex0()) {\n") ->indent() ->subcompile($this->getNode('else')) ->outdent() diff --git a/src/Runtime/LoopContext.php b/src/Runtime/LoopContext.php index 328adfe7d..716169c3a 100644 --- a/src/Runtime/LoopContext.php +++ b/src/Runtime/LoopContext.php @@ -22,8 +22,10 @@ final class LoopContext { private mixed $lastChanged; - public function __construct(private LoopIterator $loop, private $parent) - { + public function __construct( + private LoopIterator $loop, + private $parent, + ) { } public function getParent(): mixed @@ -63,7 +65,7 @@ final class LoopContext public function isLast(): bool { - return $this->loop->isLast(); + return !$this->loop->getNext()['valid']; } public function hasChanged(mixed $value): bool diff --git a/src/Runtime/LoopIterator.php b/src/Runtime/LoopIterator.php index 023e9c616..275545b70 100644 --- a/src/Runtime/LoopIterator.php +++ b/src/Runtime/LoopIterator.php @@ -90,11 +90,6 @@ final class LoopIterator implements \Iterator return $this->current['valid']; } - public function iterated(): bool - { - return 0 !== $this->index0; - } - public function getIndex0(): int { return $this->index0; @@ -113,11 +108,6 @@ final class LoopIterator implements \Iterator return $this->length = count($this->seq); } - public function isLast(): bool - { - return !$this->getNext()['valid']; - } - /** * @return array{valid: bool, key: mixed, value: mixed} */ diff --git a/tests/Node/ForTest.php b/tests/Node/ForTest.php index 5ce276670..1916dbc6c 100644 --- a/tests/Node/ForTest.php +++ b/tests/Node/ForTest.php @@ -127,7 +127,7 @@ EOF foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) { yield {$this->getVariableGetter('foo')}; } -if (!\$__internal_compile_0->iterated()) { +if (0 === \$__internal_compile_0->getIndex0()) { yield {$this->getVariableGetter('foo')}; } \$_parent = \$context['_parent'];