-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Twig 4] Introduce ForElseNode #4473
base: 4.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* (c) Armin Ronacher | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Twig\Node; | ||
|
||
use Twig\Attribute\YieldReady; | ||
use Twig\Compiler; | ||
use Twig\Node\Expression\AbstractExpression; | ||
use Twig\Node\Expression\Variable\AssignContextVariable; | ||
|
||
/** | ||
* Represents an else node in a for loop. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
#[YieldReady] | ||
class ForElseNode extends Node | ||
{ | ||
public function __construct(Node $body, int $lineno) | ||
{ | ||
parent::__construct(['body' => $body], [], $lineno); | ||
} | ||
|
||
public function compile(Compiler $compiler): void | ||
{ | ||
$compiler | ||
->addDebugInfo($this) | ||
->write("if (0 === \$iterator->getIndex0()) {\n") | ||
->indent() | ||
->subcompile($this->getNode('body')) | ||
->outdent() | ||
->write("}\n") | ||
; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
Check failure on line 1 in src/Node/ForNode.php GitHub Actions / PHPStan (8.4)
|
||
|
||
/* | ||
* This file is part of Twig. | ||
|
@@ -25,7 +25,7 @@ | |
#[YieldReady] | ||
class ForNode extends Node | ||
{ | ||
public function __construct(AssignContextVariable $keyTarget, AssignContextVariable $valueTarget, AbstractExpression $seq, ?AbstractExpression $ifexpr, Node $body, ?Node $else, int $lineno) | ||
public function __construct(AssignContextVariable $keyTarget, AssignContextVariable $valueTarget, AbstractExpression $seq, ?AbstractExpression $ifexpr, Node $body, ?ForElseNode $else, int $lineno) | ||
{ | ||
if ($ifexpr) { | ||
$body = new IfNode(new Nodes([$ifexpr, $body]), null, $lineno); | ||
|
@@ -72,13 +72,7 @@ | |
; | ||
|
||
if ($this->hasNode('else')) { | ||
$compiler | ||
->write("if (0 === \$iterator->getIndex0()) {\n") | ||
->indent() | ||
->subcompile($this->getNode('else')) | ||
->outdent() | ||
->write("}\n") | ||
; | ||
$compiler->subcompile($this->getNode('else')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, this is to get the What about adding |
||
} | ||
|
||
// remove some "private" loop variables (needed for nested loops) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restricting the type is a BC break. You should introduce the new class in 3.x with a deprecation instead (where passing something else than a
ForElseNode
ornull
would trigger a deprecation and wrap the node in a ForElseNode, using the line of that node as fallback line number)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good idea.
Do you think the direction of this PR is OK / makes sense? I can propose the V3 change once that is clear.