Skip to content

Commit f40624c

Browse files
committed
Optimize compiled code for "set" tag
1 parent d3a0717 commit f40624c

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/Node/SetNode.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ public function __construct(bool $capture, Node $names, Node $values, int $linen
3333
$safe = false;
3434
if ($capture) {
3535
$safe = true;
36-
if ($values instanceof TextNode) {
36+
if (Node::class === get_class($values) && !count($values)) {
37+
$values = new ConstantExpression('', $values->getTemplateLine());
38+
$capture = false;
39+
} elseif ($values instanceof TextNode) {
3740
$values = new ConstantExpression($values->getAttribute('data'), $values->getTemplateLine());
3841
$capture = false;
42+
} elseif ($values instanceof PrintNode && $values->getNode('expr') instanceof ConstantExpression) {
43+
$values = new ConstantExpression($values->getNode('expr')->getAttribute('value'), $values->getTemplateLine());
44+
$capture = false;
3945
} else {
4046
$values = new CaptureNode($values, $values->getTemplateLine());
4147
}
@@ -78,11 +84,23 @@ public function compile(Compiler $compiler): void
7884
$compiler->raw(']');
7985
} else {
8086
if ($this->getAttribute('safe')) {
81-
$compiler
82-
->raw("('' === \$tmp = ")
83-
->subcompile($this->getNode('values'))
84-
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
85-
;
87+
if ($this->getNode('values') instanceof ConstantExpression) {
88+
if ('' === $this->getNode('values')->getAttribute('value')) {
89+
$compiler->raw('""');
90+
} else {
91+
$compiler
92+
->raw('new Markup(')
93+
->subcompile($this->getNode('values'))
94+
->raw(', $this->env->getCharset())')
95+
;
96+
}
97+
} else {
98+
$compiler
99+
->raw("('' === \$tmp = ")
100+
->subcompile($this->getNode('values'))
101+
->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
102+
;
103+
}
86104
} else {
87105
$compiler->subcompile($this->getNode('values'));
88106
}

tests/Node/SetTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,25 @@ public static function provideTests(): iterable
7777
$node = new SetNode(true, $names, $values, 1);
7878
$tests[] = [$node, <<<EOF
7979
// line 1
80-
\$context["foo"] = ('' === \$tmp = "foo") ? '' : new Markup(\$tmp, \$this->env->getCharset());
80+
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
81+
EOF
82+
];
83+
84+
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
85+
$values = new TextNode('', 1);
86+
$node = new SetNode(true, $names, $values, 1);
87+
$tests[] = [$node, <<<EOF
88+
// line 1
89+
\$context["foo"] = "";
90+
EOF
91+
];
92+
93+
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
94+
$values = new PrintNode(new ConstantExpression('foo', 1), 1);
95+
$node = new SetNode(true, $names, $values, 1);
96+
$tests[] = [$node, <<<EOF
97+
// line 1
98+
\$context["foo"] = new Markup("foo", \$this->env->getCharset());
8199
EOF
82100
];
83101

0 commit comments

Comments
 (0)