Move static attributes for Function|Filter|TestExpression to the constructor

This commit is contained in:
Fabien Potencier
2024-08-07 21:09:18 +02:00
parent 15854ca9bf
commit e8141bbd34
3 changed files with 8 additions and 11 deletions
+5 -3
View File
@@ -19,12 +19,16 @@ class FilterExpression extends CallExpression
{ {
public function __construct(Node $node, ConstantExpression $filterName, Node $arguments, int $lineno, ?string $tag = null) public function __construct(Node $node, ConstantExpression $filterName, Node $arguments, int $lineno, ?string $tag = null)
{ {
parent::__construct(['node' => $node, 'filter' => $filterName, 'arguments' => $arguments], [], $lineno, $tag); parent::__construct(['node' => $node, 'filter' => $filterName, 'arguments' => $arguments], ['name' => $filterName->getAttribute('value'), 'type' => 'filter'], $lineno, $tag);
} }
public function compile(Compiler $compiler): void public function compile(Compiler $compiler): void
{ {
$name = $this->getNode('filter')->getAttribute('value'); $name = $this->getNode('filter')->getAttribute('value');
if ($name !== $this->getAttribute('name')) {
trigger_deprecation('twig/twig', '3.11', 'Changing the value of a "filter" node in a NodeVisitor class is not supported anymore.');
$this->setAttribute('name', $name);
}
if ('raw' === $name) { if ('raw' === $name) {
trigger_deprecation('twig/twig', '3.11', 'Creating the "raw" filter via "FilterExpression" is deprecated; use "RawFilter" instead.'); trigger_deprecation('twig/twig', '3.11', 'Creating the "raw" filter via "FilterExpression" is deprecated; use "RawFilter" instead.');
@@ -34,8 +38,6 @@ class FilterExpression extends CallExpression
} }
$filter = $compiler->getEnvironment()->getFilter($name); $filter = $compiler->getEnvironment()->getFilter($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'filter');
$this->setAttribute('needs_charset', $filter->needsCharset()); $this->setAttribute('needs_charset', $filter->needsCharset());
$this->setAttribute('needs_environment', $filter->needsEnvironment()); $this->setAttribute('needs_environment', $filter->needsEnvironment());
$this->setAttribute('needs_context', $filter->needsContext()); $this->setAttribute('needs_context', $filter->needsContext());
+1 -3
View File
@@ -19,7 +19,7 @@ class FunctionExpression extends CallExpression
{ {
public function __construct(string $name, Node $arguments, int $lineno) public function __construct(string $name, Node $arguments, int $lineno)
{ {
parent::__construct(['arguments' => $arguments], ['name' => $name, 'is_defined_test' => false], $lineno); parent::__construct(['arguments' => $arguments], ['name' => $name, 'type' => 'function', 'is_defined_test' => false], $lineno);
} }
public function compile(Compiler $compiler) public function compile(Compiler $compiler)
@@ -27,8 +27,6 @@ class FunctionExpression extends CallExpression
$name = $this->getAttribute('name'); $name = $this->getAttribute('name');
$function = $compiler->getEnvironment()->getFunction($name); $function = $compiler->getEnvironment()->getFunction($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'function');
$this->setAttribute('needs_charset', $function->needsCharset()); $this->setAttribute('needs_charset', $function->needsCharset());
$this->setAttribute('needs_environment', $function->needsEnvironment()); $this->setAttribute('needs_environment', $function->needsEnvironment());
$this->setAttribute('needs_context', $function->needsContext()); $this->setAttribute('needs_context', $function->needsContext());
+2 -5
View File
@@ -23,16 +23,13 @@ class TestExpression extends CallExpression
$nodes['arguments'] = $arguments; $nodes['arguments'] = $arguments;
} }
parent::__construct($nodes, ['name' => $name], $lineno); parent::__construct($nodes, ['name' => $name, 'type' => 'test'], $lineno);
} }
public function compile(Compiler $compiler): void public function compile(Compiler $compiler): void
{ {
$name = $this->getAttribute('name'); $test = $compiler->getEnvironment()->getTest($this->getAttribute('name'));
$test = $compiler->getEnvironment()->getTest($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'test');
$this->setAttribute('arguments', $test->getArguments()); $this->setAttribute('arguments', $test->getArguments());
$this->setAttribute('callable', $test->getCallable()); $this->setAttribute('callable', $test->getCallable());
$this->setAttribute('is_variadic', $test->isVariadic()); $this->setAttribute('is_variadic', $test->isVariadic());