Skip to content

Commit

Permalink
Fix #17 - Replace Twig\Node\Expression\NameExpression and `Twig\Nod…
Browse files Browse the repository at this point in the history
…e\Expression\TempNameExpression` are deprecated
  • Loading branch information
williamdes committed Jan 7, 2025
1 parent ddbf44a commit cd2a03a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
ignoreErrors:
-
message: "#^Only booleans are allowed in an if condition, array\\<int, Twig\\\\Node\\\\Expression\\\\NameExpression\\> given\\.$#"
message: "#^Only booleans are allowed in an if condition, array\\<int, Twig\\\\Node\\\\Expression\\\\Variable\\\\ContextVariable\\> given\\.$#"
count: 1
path: src/Node/TransNode.php

Expand All @@ -11,7 +11,7 @@ parameters:
path: src/Node/TransNode.php

-
message: "#^Parameter \\#1 \\$name of class Twig\\\\Node\\\\Expression\\\\NameExpression constructor expects string, mixed given\\.$#"
message: "#^Parameter \\#1 \\$name of class Twig\\\\Node\\\\Expression\\\\Variable\\\\ContextVariable constructor expects string, mixed given\\.$#"
count: 1
path: src/Node/TransNode.php

Expand Down
14 changes: 5 additions & 9 deletions src/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\TempNameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Expression\Variable\LocalVariable;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
Expand Down Expand Up @@ -214,15 +214,11 @@ public function compile(Compiler $compiler)
/**
* Keep this method protected instead of private some implementations may use it
*
* @psalm-return array{Node, list<NameExpression>}
* @psalm-return array{Node, list<ContextVariable>}
*/
protected function compileString(Node $body): array
{
if (
$body instanceof NameExpression
|| $body instanceof ConstantExpression
|| $body instanceof TempNameExpression
) {
if ($body instanceof ContextVariable || $body instanceof ConstantExpression || $body instanceof LocalVariable) {
return [$body, []];
}

Expand All @@ -243,7 +239,7 @@ protected function compileString(Node $body): array

$attributeName = $n->getAttribute('name');
$msg .= sprintf('%%%s%%', $attributeName);
$vars[] = new NameExpression($attributeName, $n->getTemplateLine());
$vars[] = new ContextVariable($attributeName, $n->getTemplateLine());
} else {
/** @phpstan-var TextNode $node */
$msg .= $node->getAttribute('data');
Expand Down
4 changes: 2 additions & 2 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
Expand Down Expand Up @@ -133,7 +133,7 @@ protected function checkTransString(Node $body, int $lineno): void
if (
$node instanceof TextNode
||
($node instanceof PrintNode && $node->getNode('expr') instanceof NameExpression)
($node instanceof PrintNode && $node->getNode('expr') instanceof ContextVariable)
) {
continue;
}
Expand Down
34 changes: 17 additions & 17 deletions test/Node/MoTranslatorTransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Nodes;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
Expand Down Expand Up @@ -55,9 +55,9 @@ public function testFullConstructor(): void
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, $context, $notes, $domain, 0);
Expand All @@ -75,7 +75,7 @@ public function getTests(): array
{
$tests = [];

$body = new NameExpression('foo', 0);
$body = new ContextVariable('foo', 0);
$domain = new Nodes([
new TextNode('coredomain', 0),
]);
Expand All @@ -85,7 +85,7 @@ public function getTests(): array
sprintf('yield _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
$body = new ContextVariable('foo', 0);
$domain = new Nodes([
new TextNode('coredomain', 0),
]);
Expand All @@ -103,7 +103,7 @@ public function getTests(): array

$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new PrintNode(new ContextVariable('foo', 0), 0),
new TextNode(' pommes', 0),
]);
$node = new TransNode($body, null, null, null, null, null, 0);
Expand All @@ -118,14 +118,14 @@ public function getTests(): array
$count = new ConstantExpression(12, 0);
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have one apple', 0),
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, null, null, null, 0);
Expand All @@ -142,7 +142,7 @@ public function getTests(): array

$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new PrintNode(new ContextVariable('foo', 0), 0),
new TextNode(' pommes', 0),
]);
$context = new Nodes([
Expand All @@ -160,17 +160,17 @@ public function getTests(): array
$count = new ConstantExpression(12, 0);
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have one apple', 0),
]);
$context = new Nodes([
new TextNode('The context', 0),
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, $context, null, null, 0);
Expand All @@ -187,7 +187,7 @@ public function getTests(): array

$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new PrintNode(new ContextVariable('foo', 0), 0),
new TextNode(' pommes', 0),
]);
$context = new Nodes([
Expand All @@ -208,7 +208,7 @@ public function getTests(): array
$count = new ConstantExpression(12, 0);
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have one apple', 0),
]);
$context = new Nodes([
Expand All @@ -219,9 +219,9 @@ public function getTests(): array
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, $context, null, $domain, 0);
Expand Down
30 changes: 15 additions & 15 deletions test/Node/TransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Nodes;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
Expand All @@ -35,9 +35,9 @@ public function testConstructor(): void
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, null, null, null, 0);
Expand All @@ -58,9 +58,9 @@ public function testConstructorWithDomain(): void
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, null, null, $domain, 0);
Expand All @@ -77,7 +77,7 @@ public function testEnableDebugNotEnabled(): void
$body = new TextNode('There is 1 pending task', 0);
$plural = new Nodes([
new TextNode('There are ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' pending tasks', 0),
]);
$notes = new TextNode('Notes for translators', 0);
Expand Down Expand Up @@ -105,7 +105,7 @@ public function testEnableDebugEnabled(): void
$body = new TextNode('There is 1 pending task', 0);
$plural = new Nodes([
new TextNode('There are ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' pending tasks', 0),
]);
$notes = new TextNode('Notes for translators', 0);
Expand Down Expand Up @@ -133,7 +133,7 @@ public function getTests(): array
{
$tests = [];

$body = new NameExpression('foo', 0);
$body = new ContextVariable('foo', 0);
$domain = new Nodes([
new TextNode('coredomain', 0),
]);
Expand All @@ -143,7 +143,7 @@ public function getTests(): array
sprintf('yield dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
$body = new ContextVariable('foo', 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, sprintf('yield gettext(%s);', $this->getVariableGetter('foo'))];

Expand All @@ -159,7 +159,7 @@ public function getTests(): array

$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new PrintNode(new ContextVariable('foo', 0), 0),
new TextNode(' pommes', 0),
]);
$node = new TransNode($body, null, null, null, null, null, 0);
Expand All @@ -174,14 +174,14 @@ public function getTests(): array
$count = new ConstantExpression(12, 0);
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have one apple', 0),
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new PrintNode(new ContextVariable('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' apples', 0),
]);
$node = new TransNode($body, $plural, $count, null, null, null, 0);
Expand All @@ -200,7 +200,7 @@ public function getTests(): array
$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(
new FilterExpression(new NameExpression('foo', 0), new ConstantExpression('escape', 0), new Nodes(), 0),
new FilterExpression(new ContextVariable('foo', 0), new ConstantExpression('escape', 0), new Nodes(), 0),
0,
),
new TextNode(' pommes', 0),
Expand Down Expand Up @@ -234,7 +234,7 @@ public function getTests(): array
$body = new TextNode('There is 1 pending task', 0);
$plural = new Nodes([
new TextNode('There are ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new PrintNode(new ContextVariable('count', 0), 0),
new TextNode(' pending tasks', 0),
]);
$notes = new TextNode('Notes for translators', 0);
Expand Down

0 comments on commit cd2a03a

Please sign in to comment.