Skip to content

Commit b6d9dd6

Browse files
committed
Fix inheritance logic to be able to use instanceof ContextVariable instead of NameExpression
1 parent 72c91f6 commit b6d9dd6

15 files changed

+33
-22
lines changed

src/ExpressionParser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ public function parseArguments()
655655

656656
$name = null;
657657
if ($namedArguments && (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || (!$definition && $token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':')))) {
658-
if (!$value instanceof NameExpression) {
658+
if (!$value instanceof ContextVariable) {
659659
throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext());
660660
}
661661
$name = $value->getAttribute('name');
@@ -743,7 +743,7 @@ private function parseTestExpression(Node $node): TestExpression
743743
$arguments = new Nodes([0 => $this->getPrimary()]);
744744
}
745745

746-
if ('defined' === $test->getName() && $node instanceof NameExpression && null !== $alias = $this->parser->getImportedSymbol('function', $node->getAttribute('name'))) {
746+
if ('defined' === $test->getName() && $node instanceof ContextVariable && null !== $alias = $this->parser->getImportedSymbol('function', $node->getAttribute('name'))) {
747747
$node = new MacroReferenceExpression($alias['node']->getNode('var'), $alias['name'], new ArrayExpression([], $node->getTemplateLine()), $node->getTemplateLine());
748748
}
749749

@@ -898,7 +898,7 @@ public function parseOnlyArguments()
898898

899899
$name = null;
900900
if (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || ($token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':'))) {
901-
if (!$value instanceof NameExpression) {
901+
if (!$value instanceof ContextVariable) {
902902
throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext());
903903
}
904904
$name = $value->getAttribute('name');
@@ -946,7 +946,7 @@ private function parseSubscriptExpressionDot(Node $node): AbstractExpression
946946
}
947947

948948
if (
949-
$node instanceof NameExpression
949+
$node instanceof ContextVariable
950950
&& (
951951
null !== $this->parser->getImportedSymbol('template', $node->getAttribute('name'))
952952
|| '_self' === $node->getAttribute('name') && $attribute instanceof ConstantExpression

src/Node/Expression/ArrayExpression.php

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

1414
use Twig\Compiler;
1515
use Twig\Node\Expression\Unary\StringCastUnary;
16+
use Twig\Node\Expression\Variable\ContextVariable;
1617

1718
class ArrayExpression extends AbstractExpression
1819
{
@@ -99,7 +100,7 @@ public function compile(Compiler $compiler): void
99100
++$nextIndex;
100101
} else {
101102
$key = null;
102-
if ($pair['key'] instanceof NameExpression) {
103+
if ($pair['key'] instanceof ContextVariable) {
103104
$pair['key'] = new StringCastUnary($pair['key'], $pair['key']->getTemplateLine());
104105
}
105106
if ($pair['key'] instanceof TempNameExpression) {

src/Node/Expression/AssignNameExpression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
use Twig\Compiler;
1616
use Twig\Error\SyntaxError;
1717
use Twig\Node\Expression\Variable\AssignContextVariable;
18+
use Twig\Node\Expression\Variable\ContextVariable;
1819

19-
class AssignNameExpression extends NameExpression
20+
class AssignNameExpression extends ContextVariable
2021
{
2122
public function __construct(string $name, int $lineno)
2223
{

src/Node/Expression/Binary/NullCoalesceBinary.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use Twig\Node\EmptyNode;
1616
use Twig\Node\Expression\AbstractExpression;
1717
use Twig\Node\Expression\BlockReferenceExpression;
18-
use Twig\Node\Expression\NameExpression;
1918
use Twig\Node\Expression\OperatorEscapeInterface;
2019
use Twig\Node\Expression\Test\DefinedTest;
2120
use Twig\Node\Expression\Test\NullTest;
2221
use Twig\Node\Expression\Unary\NotUnary;
22+
use Twig\Node\Expression\Variable\ContextVariable;
2323
use Twig\TwigTest;
2424

2525
final class NullCoalesceBinary extends AbstractBinary implements OperatorEscapeInterface
@@ -28,7 +28,7 @@ public function __construct(AbstractExpression $left, AbstractExpression $right,
2828
{
2929
parent::__construct($left, $right, $lineno);
3030

31-
if (!$left instanceof NameExpression) {
31+
if (!$left instanceof ContextVariable) {
3232
$test = new DefinedTest(clone $left, new TwigTest('defined'), new EmptyNode(), $left->getTemplateLine());
3333
// for "block()", we don't need the null test as the return value is always a string
3434
if (!$left instanceof BlockReferenceExpression) {

src/Node/Expression/Filter/DefaultFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
use Twig\Node\Expression\ConstantExpression;
2020
use Twig\Node\Expression\FilterExpression;
2121
use Twig\Node\Expression\GetAttrExpression;
22-
use Twig\Node\Expression\NameExpression;
2322
use Twig\Node\Expression\Ternary\ConditionalTernary;
2423
use Twig\Node\Expression\Test\DefinedTest;
24+
use Twig\Node\Expression\Variable\ContextVariable;
2525
use Twig\Node\Node;
2626
use Twig\TwigFilter;
2727
use Twig\TwigTest;
@@ -53,7 +53,7 @@ public function __construct(Node $node, TwigFilter|ConstantExpression $filter, N
5353
$default = new FilterExpression($node, new TwigFilter('default', [CoreExtension::class, 'default']), $arguments, $node->getTemplateLine());
5454
}
5555

56-
if ('default' === $name && ($node instanceof NameExpression || $node instanceof GetAttrExpression)) {
56+
if ('default' === $name && ($node instanceof ContextVariable || $node instanceof GetAttrExpression)) {
5757
$test = new DefinedTest(clone $node, new TwigTest('defined'), new EmptyNode(), $node->getTemplateLine());
5858
$false = \count($arguments) ? $arguments->getNode('0') : new ConstantExpression('', $node->getTemplateLine());
5959

src/Node/Expression/GetAttrExpression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Twig\Compiler;
1616
use Twig\Extension\SandboxExtension;
17+
use Twig\Node\Expression\Variable\ContextVariable;
1718
use Twig\Template;
1819

1920
class GetAttrExpression extends AbstractExpression
@@ -28,7 +29,7 @@ public function __construct(AbstractExpression $node, AbstractExpression $attrib
2829
$nodes['arguments'] = $arguments;
2930
}
3031

31-
if ($arguments && !$arguments instanceof ArrayExpression && !$arguments instanceof NameExpression) {
32+
if ($arguments && !$arguments instanceof ArrayExpression && !$arguments instanceof ContextVariable) {
3233
trigger_deprecation('twig/twig', '3.15', \sprintf('Not passing a "%s" instance as the "arguments" argument of the "%s" constructor is deprecated ("%s" given).', ArrayExpression::class, static::class, $arguments::class));
3334
}
3435

src/Node/Expression/MethodCallExpression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Twig\Node\Expression;
1313

1414
use Twig\Compiler;
15+
use Twig\Node\Expression\Variable\ContextVariable;
1516

1617
class MethodCallExpression extends AbstractExpression
1718
{
@@ -21,7 +22,7 @@ public function __construct(AbstractExpression $node, string $method, ArrayExpre
2122

2223
parent::__construct(['node' => $node, 'arguments' => $arguments], ['method' => $method, 'safe' => false, 'is_defined_test' => false], $lineno);
2324

24-
if ($node instanceof NameExpression) {
25+
if ($node instanceof ContextVariable) {
2526
$node->setAttribute('always_defined', true);
2627
}
2728
}

src/Node/Expression/NullCoalesceExpression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Twig\Node\Expression\Test\DefinedTest;
1919
use Twig\Node\Expression\Test\NullTest;
2020
use Twig\Node\Expression\Unary\NotUnary;
21+
use Twig\Node\Expression\Variable\ContextVariable;
2122
use Twig\Node\Node;
2223
use Twig\TwigTest;
2324

@@ -60,7 +61,7 @@ public function compile(Compiler $compiler): void
6061
* cases might be implemented as an optimizer node visitor, but has not been done
6162
* as benefits are probably not worth the added complexity.
6263
*/
63-
if ($this->getNode('expr2') instanceof NameExpression) {
64+
if ($this->getNode('expr2') instanceof ContextVariable) {
6465
$this->getNode('expr2')->setAttribute('always_defined', true);
6566
$compiler
6667
->raw('((')

src/Node/Expression/Test/DefinedTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Twig\Node\Expression\MethodCallExpression;
2525
use Twig\Node\Expression\NameExpression;
2626
use Twig\Node\Expression\TestExpression;
27+
use Twig\Node\Expression\Variable\ContextVariable;
2728
use Twig\Node\Node;
2829
use Twig\TwigTest;
2930

@@ -49,7 +50,7 @@ public function __construct(Node $node, TwigTest|string $name, ?Node $arguments,
4950
trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance to the "node" argument of "%s" is deprecated ("%s" given).', AbstractExpression::class, static::class, \get_class($node));
5051
}
5152

52-
if ($node instanceof NameExpression) {
53+
if ($node instanceof ContextVariable) {
5354
$node->setAttribute('is_defined_test', true);
5455
} elseif ($node instanceof GetAttrExpression) {
5556
$node->setAttribute('is_defined_test', true);

src/Node/Expression/Variable/ContextVariable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313

1414
use Twig\Node\Expression\NameExpression;
1515

16-
final class ContextVariable extends NameExpression
16+
class ContextVariable extends NameExpression
1717
{
1818
}

0 commit comments

Comments
 (0)