Make some small internal tweaks to loops

This commit is contained in:
Fabien Potencier
2024-07-18 08:23:22 +02:00
parent 98e67eac9a
commit 7f0f313f1a
4 changed files with 7 additions and 15 deletions
+1 -1
View File
@@ -65,7 +65,7 @@ class ForNode extends Node
if ($this->hasNode('else')) { if ($this->hasNode('else')) {
$compiler $compiler
->write("if (!\${$loopName}->iterated()) {\n") ->write("if (0 === \${$loopName}->getIndex0()) {\n")
->indent() ->indent()
->subcompile($this->getNode('else')) ->subcompile($this->getNode('else'))
->outdent() ->outdent()
+5 -3
View File
@@ -22,8 +22,10 @@ final class LoopContext
{ {
private mixed $lastChanged; private mixed $lastChanged;
public function __construct(private LoopIterator $loop, private $parent) public function __construct(
{ private LoopIterator $loop,
private $parent,
) {
} }
public function getParent(): mixed public function getParent(): mixed
@@ -63,7 +65,7 @@ final class LoopContext
public function isLast(): bool public function isLast(): bool
{ {
return $this->loop->isLast(); return !$this->loop->getNext()['valid'];
} }
public function hasChanged(mixed $value): bool public function hasChanged(mixed $value): bool
-10
View File
@@ -90,11 +90,6 @@ final class LoopIterator implements \Iterator
return $this->current['valid']; return $this->current['valid'];
} }
public function iterated(): bool
{
return 0 !== $this->index0;
}
public function getIndex0(): int public function getIndex0(): int
{ {
return $this->index0; return $this->index0;
@@ -113,11 +108,6 @@ final class LoopIterator implements \Iterator
return $this->length = count($this->seq); return $this->length = count($this->seq);
} }
public function isLast(): bool
{
return !$this->getNext()['valid'];
}
/** /**
* @return array{valid: bool, key: mixed, value: mixed} * @return array{valid: bool, key: mixed, value: mixed}
*/ */
+1 -1
View File
@@ -127,7 +127,7 @@ EOF
foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) { foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) {
yield {$this->getVariableGetter('foo')}; yield {$this->getVariableGetter('foo')};
} }
if (!\$__internal_compile_0->iterated()) { if (0 === \$__internal_compile_0->getIndex0()) {
yield {$this->getVariableGetter('foo')}; yield {$this->getVariableGetter('foo')};
} }
\$_parent = \$context['_parent']; \$_parent = \$context['_parent'];