Skip to content

Commit 3bfdb5d

Browse files
committed
Remove Twig deprecations
1 parent 553ea2b commit 3bfdb5d

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

Diff for: Twig/Visitor/AbstractNodeVisitor.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Twig\Visitor;
13+
14+
use Twig\Environment;
15+
use Twig\Node\Node;
16+
use Twig\NodeVisitor\NodeVisitorInterface;
17+
18+
abstract class AbstractNodeVisitor implements NodeVisitorInterface
19+
{
20+
21+
abstract protected function doEnterNode(Node $node, Environment $env): Node;
22+
23+
abstract protected function doLeaveNode(Node $node, Environment $env): Node;
24+
25+
public function enterNode(Node $node, Environment $env): Node
26+
{
27+
return $this->doEnterNode($node, $env);
28+
}
29+
30+
public function leaveNode(Node $node, Environment $env): ?Node
31+
{
32+
return $this->doLeaveNode($node, $env);
33+
}
34+
35+
protected function getValueFromNode(Node $node): ?string
36+
{
37+
if (Environment::VERSION_ID >= 31200) {
38+
return $node->getAttribute('twig_callable')->getName();
39+
} else {
40+
return $node->getNode('filter')->getAttribute('value');
41+
}
42+
}
43+
}

Diff for: Twig/Visitor/DefaultApplyingNodeVisitor.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Twig\Node\Expression\Ternary\ConditionalTernary;
2222
use Twig\Node\Node;
2323
use Twig\Node\Nodes;
24-
use Twig\NodeVisitor\AbstractNodeVisitor;
2524
use Twig\TwigFilter;
2625

2726
/**
@@ -50,14 +49,14 @@ public function doEnterNode(Node $node, Environment $env): Node
5049
return $node;
5150
}
5251

53-
if (!($node instanceof FilterExpression && 'desc' === $node->getNode('filter')->getAttribute('value'))) {
52+
if (!($node instanceof FilterExpression && 'desc' === $this->getValueFromNode($node))) {
5453
return $node;
5554
}
5655

5756
$transNode = $node->getNode('node');
5857
while ($transNode instanceof FilterExpression
59-
&& 'trans' !== $transNode->getNode('filter')->getAttribute('value')
60-
&& 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
58+
&& 'trans' !== $this->getValueFromNode($transNode)
59+
&& 'transchoice' !== $this->getValueFromNode($transNode)) {
6160
$transNode = $transNode->getNode('node');
6261
}
6362

@@ -72,7 +71,7 @@ public function doEnterNode(Node $node, Environment $env): Node
7271
// if the |transchoice filter is used, delegate the call to the TranslationExtension
7372
// so that we can catch a possible exception when the default translation has not yet
7473
// been extracted
75-
if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) {
74+
if ('transchoice' === $this->getValueFromNode($transNode)) {
7675
$transchoiceArguments = new ArrayExpression([], $transNode->getTemplateLine());
7776
$transchoiceArguments->addElement($wrappingNode->getNode('node'));
7877
$transchoiceArguments->addElement($defaultNode);
@@ -150,4 +149,14 @@ public function getPriority(): int
150149
{
151150
return -2;
152151
}
152+
153+
public function enterNode(Node $node, Environment $env): Node
154+
{
155+
return $this->doEnterNode($node, $env);
156+
}
157+
158+
public function leaveNode(Node $node, Environment $env): ?Node
159+
{
160+
return $this->doLeaveNode($node, $env);
161+
}
153162
}

Diff for: Twig/Visitor/NormalizingNodeVisitor.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Twig\Node\Expression\Binary\ConcatBinary;
1616
use Twig\Node\Expression\ConstantExpression;
1717
use Twig\Node\Node;
18-
use Twig\NodeVisitor\AbstractNodeVisitor;
1918

2019
/**
2120
* Performs equivalence transformations on the AST to ensure that

Diff for: Twig/Visitor/RemovingNodeVisitor.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Twig\Environment;
1515
use Twig\Node\Expression\FilterExpression;
1616
use Twig\Node\Node;
17-
use Twig\NodeVisitor\AbstractNodeVisitor;
1817

1918
/**
2019
* Removes translation metadata filters from the AST.
@@ -36,7 +35,7 @@ public function setEnabled(bool $bool): void
3635
protected function doEnterNode(Node $node, Environment $env): Node
3736
{
3837
if ($this->enabled && $node instanceof FilterExpression) {
39-
$name = $node->getNode('filter')->getAttribute('value');
38+
$name = $this->getValueFromNode($node);
4039

4140
if ('desc' === $name || 'meaning' === $name) {
4241
return $this->enterNode($node->getNode('node'), $env);

0 commit comments

Comments
 (0)