Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/Node/SetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ public function __construct(bool $capture, Node $names, Node $values, int $linen
$safe = false;
if ($capture) {
$safe = true;
if ($values instanceof TextNode) {
if (Node::class === get_class($values) && !count($values)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this makes me think we should really investigate having a dedicated node class for representing a statement list in 4.x (as suggested in #4292 (comment)), as we have this special case of Node::class === get_class($values) in more and more places

$values = new ConstantExpression('', $values->getTemplateLine());
$capture = false;
} elseif ($values instanceof TextNode) {
$values = new ConstantExpression($values->getAttribute('data'), $values->getTemplateLine());
$capture = false;
} elseif ($values instanceof PrintNode && $values->getNode('expr') instanceof ConstantExpression) {
$values = $values->getNode('expr');
$capture = false;
} else {
$values = new CaptureNode($values, $values->getTemplateLine());
}
Expand Down Expand Up @@ -78,11 +84,23 @@ public function compile(Compiler $compiler): void
$compiler->raw(']');
} else {
if ($this->getAttribute('safe')) {
$compiler
->raw("('' === \$tmp = ")
->subcompile($this->getNode('values'))
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
;
if ($this->getNode('values') instanceof ConstantExpression) {
if ('' === $this->getNode('values')->getAttribute('value')) {
$compiler->raw('""');
} else {
$compiler
->raw('new Markup(')
->subcompile($this->getNode('values'))
->raw(', $this->env->getCharset())')
;
}
} else {
$compiler
->raw("('' === \$tmp = ")
->subcompile($this->getNode('values'))
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
;
}
} else {
$compiler->subcompile($this->getNode('values'));
}
Expand Down
20 changes: 19 additions & 1 deletion tests/Node/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,25 @@ public static function provideTests(): iterable
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = ('' === \$tmp = "foo") ? '' : new Markup(\$tmp, \$this->env->getCharset());
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
EOF
];

$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new TextNode('', 1);
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = "";
EOF
];

$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new PrintNode(new ConstantExpression('foo', 1), 1);
$node = new SetNode(true, $names, $values, 1);
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
EOF
];

Expand Down