Rename Node classes related to variables

This commit is contained in:
Fabien Potencier
2024-10-16 08:45:38 +02:00
parent af4090757b
commit fc15e7ccbc
35 changed files with 277 additions and 149 deletions
+14 -13
View File
@@ -18,7 +18,6 @@ use Twig\Node\EmptyNode;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ArrowFunctionExpression;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Node\Expression\Binary\AbstractBinary;
use Twig\Node\Expression\Binary\ConcatBinary;
use Twig\Node\Expression\ConditionalExpression;
@@ -26,13 +25,15 @@ use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\MacroReferenceExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\TempNameExpression;
use Twig\Node\Expression\TestExpression;
use Twig\Node\Expression\Unary\AbstractUnary;
use Twig\Node\Expression\Unary\NegUnary;
use Twig\Node\Expression\Unary\NotUnary;
use Twig\Node\Expression\Unary\PosUnary;
use Twig\Node\Expression\Unary\SpreadUnary;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Expression\Variable\LocalVariable;
use Twig\Node\Expression\Variable\TemplateVariable;
use Twig\Node\Node;
use Twig\Node\Nodes;
@@ -184,7 +185,7 @@ class ExpressionParser
if ($stream->look(1)->test(Token::ARROW_TYPE)) {
$line = $stream->getCurrent()->getLine();
$token = $stream->expect(Token::NAME_TYPE);
$names = [new AssignNameExpression($token->getValue(), $token->getLine())];
$names = [new AssignContextVariable($token->getValue(), $token->getLine())];
$stream->expect(Token::ARROW_TYPE);
return new ArrowFunctionExpression($this->parseExpression(), new Nodes($names), $line);
@@ -219,7 +220,7 @@ class ExpressionParser
$names = [];
while (true) {
$token = $stream->expect(Token::NAME_TYPE);
$names[] = new AssignNameExpression($token->getValue(), $token->getLine());
$names[] = new AssignContextVariable($token->getValue(), $token->getLine());
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break;
@@ -327,7 +328,7 @@ class ExpressionParser
if ('(' === $this->parser->getCurrentToken()->getValue()) {
$node = $this->getFunctionNode($token->getValue(), $token->getLine());
} else {
$node = new NameExpression($token->getValue(), $token->getLine());
$node = new ContextVariable($token->getValue(), $token->getLine());
}
}
break;
@@ -354,7 +355,7 @@ class ExpressionParser
if (preg_match(Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
// in this context, string operators are variable names
$this->parser->getStream()->next();
$node = new NameExpression($token->getValue(), $token->getLine());
$node = new ContextVariable($token->getValue(), $token->getLine());
break;
}
@@ -485,7 +486,7 @@ class ExpressionParser
// {a} is a shortcut for {a:a}
if ($stream->test(Token::PUNCTUATION_TYPE, [',', '}'])) {
$value = new NameExpression($key->getAttribute('value'), $key->getTemplateLine());
$value = new ContextVariable($key->getAttribute('value'), $key->getTemplateLine());
$node->addElement($value, $key);
continue;
}
@@ -532,7 +533,7 @@ class ExpressionParser
public function getFunctionNode($name, $line)
{
if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
return new MacroReferenceExpression(new TemplateVariable($alias['node'], $line), $alias['name'], $this->createArguments($line), $line);
return new MacroReferenceExpression(new TemplateVariable($alias['node']->getAttribute('name'), $line), $alias['name'], $this->createArguments($line), $line);
}
$args = $this->parseOnlyArguments();
@@ -646,7 +647,7 @@ class ExpressionParser
if ($definition) {
$token = $stream->expect(Token::NAME_TYPE, null, 'An argument must be a name');
$value = new NameExpression($token->getValue(), $this->parser->getCurrentToken()->getLine());
$value = new ContextVariable($token->getValue(), $this->parser->getCurrentToken()->getLine());
} else {
if ($stream->nextIf(Token::SPREAD_TYPE)) {
$hasSpread = true;
@@ -708,7 +709,7 @@ class ExpressionParser
} else {
$stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
}
$targets[] = new AssignNameExpression($token->getValue(), $token->getLine());
$targets[] = new AssignContextVariable($token->getValue(), $token->getLine());
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break;
@@ -749,7 +750,7 @@ class ExpressionParser
}
if ('defined' === $test->getName() && $node instanceof NameExpression && null !== $alias = $this->parser->getImportedSymbol('function', $node->getAttribute('name'))) {
$node = new MacroReferenceExpression(new TemplateVariable($alias['node'], $node->getTemplateLine()), $alias['name'], new ArrayExpression([], $node->getTemplateLine()), $node->getTemplateLine());
$node = new MacroReferenceExpression(new TemplateVariable($alias['node']->getAttribute('name'), $node->getTemplateLine()), $alias['name'], new ArrayExpression([], $node->getTemplateLine()), $node->getTemplateLine());
}
$ready = $test instanceof TwigTest;
@@ -870,7 +871,7 @@ class ExpressionParser
{
$arguments = new ArrayExpression([], $line);
foreach ($this->parseOnlyArguments() as $k => $n) {
$arguments->addElement($n, new TempNameExpression($k, $line));
$arguments->addElement($n, new LocalVariable($k, $line));
}
return $arguments;
@@ -956,7 +957,7 @@ class ExpressionParser
$node instanceof NameExpression
&&
(
null !== $this->parser->getImportedSymbol('template', $node->getAttribute('name'))
null !== $this->parser->getImportedSymbol('template', (new TemplateVariable($node->getAttribute('name'), $node->getTemplateLine()))->getAttribute('name'))
||
'_self' === $node->getAttribute('name') && $attribute instanceof ConstantExpression
)