Skip to content

Commit 6097675

Browse files
committed
Add ForElseNode
1 parent 17b35c8 commit 6097675

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# 3.19.0 (2025-XX-XX)
22

3+
* Add `ForElseNode`
34
* Deprecate `Twig\ExpressionParser::parseOnlyArguments()` and
45
`Twig\ExpressionParser::parseArguments()` (use
56
`Twig\ExpressionParser::parseNamedArguments()` instead)
6-
77
* Fix `constant()` behavior when used with `??`
88
* Add the `invoke` filter
99
* Make `{}` optional for the `types` tag

src/Node/ForElseNode.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Twig.
5+
*
6+
* (c) Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Twig\Node;
13+
14+
use Twig\Attribute\YieldReady;
15+
use Twig\Compiler;
16+
17+
/**
18+
* Represents an else node in a for loop.
19+
*
20+
* @author Fabien Potencier <fabien@symfony.com>
21+
*/
22+
#[YieldReady]
23+
class ForElseNode extends Node
24+
{
25+
public function __construct(Node $body, int $lineno)
26+
{
27+
parent::__construct(['body' => $body], [], $lineno);
28+
}
29+
30+
public function compile(Compiler $compiler): void
31+
{
32+
$compiler
33+
->addDebugInfo($this)
34+
->write("if (!\$context['_iterated']) {\n")
35+
->indent()
36+
->subcompile($this->getNode('body'))
37+
->outdent()
38+
->write("}\n")
39+
;
40+
}
41+
}

src/Node/ForNode.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public function __construct(AssignContextVariable $keyTarget, AssignContextVaria
3535
trigger_deprecation('twig/twig', '3.19', \sprintf('Passing not-null to the "ifexpr" argument of the "%s" constructor is deprecated.', static::class));
3636
}
3737

38+
if (null !== $else && !$else instanceof ForElseNode) {
39+
trigger_deprecation('twig/twig', '3.19', \sprintf('Not passing an instance of "%s" to the "else" argument of the "%s" constructor is deprecated.', ForElseNode::class, static::class));
40+
41+
$else = new ForElseNode($else, $else->getTemplateLine());
42+
}
43+
3844
$nodes = ['key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body];
3945
if (null !== $else) {
4046
$nodes['else'] = $else;
@@ -93,13 +99,7 @@ public function compile(Compiler $compiler): void
9399
;
94100

95101
if ($this->hasNode('else')) {
96-
$compiler
97-
->write("if (!\$context['_iterated']) {\n")
98-
->indent()
99-
->subcompile($this->getNode('else'))
100-
->outdent()
101-
->write("}\n")
102-
;
102+
$compiler->subcompile($this->getNode('else'));
103103
}
104104

105105
$compiler->write("\$_parent = \$context['_parent'];\n");

src/TokenParser/ForTokenParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Twig\TokenParser;
1414

1515
use Twig\Node\Expression\Variable\AssignContextVariable;
16+
use Twig\Node\ForElseNode;
1617
use Twig\Node\ForNode;
1718
use Twig\Node\Node;
1819
use Twig\Token;
@@ -42,7 +43,7 @@ public function parse(Token $token): Node
4243
$body = $this->parser->subparse([$this, 'decideForFork']);
4344
if ('else' == $stream->next()->getValue()) {
4445
$stream->expect(Token::BLOCK_END_TYPE);
45-
$else = $this->parser->subparse([$this, 'decideForEnd'], true);
46+
$else = new ForElseNode($this->parser->subparse([$this, 'decideForEnd'], true), $stream->getCurrent()->getLine());
4647
} else {
4748
$else = null;
4849
}

tests/Node/ForTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Twig\Node\Expression\Variable\AssignContextVariable;
1515
use Twig\Node\Expression\Variable\ContextVariable;
16+
use Twig\Node\ForElseNode;
1617
use Twig\Node\ForNode;
1718
use Twig\Node\Nodes;
1819
use Twig\Node\PrintNode;
@@ -36,7 +37,7 @@ public function testConstructor()
3637
$this->assertEquals($body, $node->getNode('body')->getNode('0'));
3738
$this->assertFalse($node->hasNode('else'));
3839

39-
$else = new PrintNode(new ContextVariable('foo', 1), 1);
40+
$else = new ForElseNode(new PrintNode(new ContextVariable('foo', 1), 1), 5);
4041
$node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1);
4142
$node->setAttribute('with_loop', false);
4243
$this->assertEquals($else, $node->getNode('else'));
@@ -159,7 +160,7 @@ public static function provideTests(): iterable
159160
$valueTarget = new AssignContextVariable('v', 1);
160161
$seq = new ContextVariable('values', 1);
161162
$body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1);
162-
$else = new PrintNode(new ContextVariable('foo', 1), 1);
163+
$else = new ForElseNode(new PrintNode(new ContextVariable('foo', 6), 6), 5);
163164
$node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1);
164165
$node->setAttribute('with_loop', true);
165166

@@ -193,7 +194,9 @@ public static function provideTests(): iterable
193194
\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
194195
}
195196
}
197+
// line 5
196198
if (!\$context['_iterated']) {
199+
// line 6
197200
yield $fooGetter;
198201
}
199202
\$_parent = \$context['_parent'];

0 commit comments

Comments
 (0)