Skip to content

Commit

Permalink
Fix #16 - Direct use of Node is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Jan 7, 2025
1 parent ea585ce commit cb9fd67
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 80 deletions.
34 changes: 34 additions & 0 deletions src/Node/I18nNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of Twig I18n extension.
*
* (c) 2025 phpMyAdmin contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PhpMyAdmin\Twig\Extensions\Node;

use Twig\Attribute\YieldReady;
use Twig\Node\Node;

/**
* Represents a single node
*/
#[YieldReady]
final class I18nNode extends Node
{
/**
* @param Node|null $node A single node
* @param array<string, mixed> $attributes An array of attributes (should not be nodes)
* @param int $lineno The line number
*/
public function __construct(Node|null $node, array $attributes, int $lineno)
{
parent::__construct($node === null ? [] : [$node], $attributes, $lineno);
}
}
2 changes: 1 addition & 1 deletion src/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ protected function compileString(Node $body): array
$msg = $body->getAttribute('data');
}

return [new Node([new ConstantExpression(trim($msg), $body->getTemplateLine())]), $vars];
return [new I18nNode(new ConstantExpression(trim($msg), $body->getTemplateLine()), [], 0), $vars];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpMyAdmin\Twig\Extensions\TokenParser;

use PhpMyAdmin\Twig\Extensions\Node\I18nNode;
use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\AbstractExpression;
Expand Down Expand Up @@ -96,12 +97,12 @@ protected function preParse(Token $token): array

if ($notes instanceof TextNode) {
// Don't use TextNode for $notes to avoid it getting merged with $body when optimizing.
$notes = new Node([], ['data' => $notes->getAttribute('data')], $notes->getTemplateLine());
$notes = new I18nNode(null, ['data' => $notes->getAttribute('data')], $notes->getTemplateLine());
}

if ($context instanceof TextNode) {
// Don't use TextNode for $context to avoid it getting merged with $body when optimizing.
$context = new Node([], ['data' => $context->getAttribute('data')], $context->getTemplateLine());
$context = new I18nNode(null, ['data' => $context->getAttribute('data')], $context->getTemplateLine());
}

return [$body, $plural, $count, $context, $notes, $domain, $lineno, $this->getTag()];
Expand Down
94 changes: 47 additions & 47 deletions test/Node/MoTranslatorTransTest.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\NameExpression;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
use Twig\Test\NodeTestCase;
Expand All @@ -41,25 +41,25 @@ public static function tearDownAfterClass(): void
public function testFullConstructor(): void
{
$count = new ConstantExpression(12, 0);
$body = new Node([
$body = new Nodes([
new TextNode('Hello', 0),
], [], 0);
$notes = new Node([
]);
$notes = new Nodes([
new TextNode('notes for translators', 0),
], [], 0);
$domain = new Node([
]);
$domain = new Nodes([
new TextNode('mydomain', 0),
], [], 0);
$context = new Node([
]);
$context = new Nodes([
new TextNode('mydomain', 0),
], [], 0);
$plural = new Node([
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new TextNode(' apples', 0),
], [], 0);
]);
$node = new TransNode($body, $plural, $count, $context, $notes, $domain, 0);

$this->assertEquals($body, $node->getNode('body'));
Expand All @@ -76,22 +76,22 @@ public function getTests(): array
$tests = [];

$body = new NameExpression('foo', 0);
$domain = new Node([
$domain = new Nodes([
new TextNode('coredomain', 0),
], [], 0);
]);
$node = new TransNode($body, null, null, null, null, $domain, 0);
$tests[] = [
$node,
sprintf('yield _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
$domain = new Node([
$domain = new Nodes([
new TextNode('coredomain', 0),
], [], 0);
$context = new Node([
]);
$context = new Nodes([
new TextNode('The context', 0),
], [], 0);
]);
$node = new TransNode($body, null, null, $context, null, $domain, 0);
$tests[] = [
$node,
Expand All @@ -101,11 +101,11 @@ public function getTests(): array
),
];

$body = new Node([
$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new TextNode(' pommes', 0),
], [], 0);
]);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [
$node,
Expand All @@ -116,18 +116,18 @@ public function getTests(): array
];

$count = new ConstantExpression(12, 0);
$body = new Node([
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have one apple', 0),
], [], 0);
$plural = new Node([
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new TextNode(' apples', 0),
], [], 0);
]);
$node = new TransNode($body, $plural, $count, null, null, null, 0);
$tests[] = [
$node,
Expand All @@ -140,14 +140,14 @@ public function getTests(): array
),
];

$body = new Node([
$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new TextNode(' pommes', 0),
], [], 0);
$context = new Node([
]);
$context = new Nodes([
new TextNode('The context', 0),
], [], 0);
]);
$node = new TransNode($body, null, null, $context, null, null, 0);
$tests[] = [
$node,
Expand All @@ -158,21 +158,21 @@ public function getTests(): array
];

$count = new ConstantExpression(12, 0);
$body = new Node([
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have one apple', 0),
], [], 0);
$context = new Node([
]);
$context = new Nodes([
new TextNode('The context', 0),
], [], 0);
$plural = new Node([
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new TextNode(' apples', 0),
], [], 0);
]);
$node = new TransNode($body, $plural, $count, $context, null, null, 0);
$tests[] = [
$node,
Expand All @@ -185,17 +185,17 @@ public function getTests(): array
),
];

$body = new Node([
$body = new Nodes([
new TextNode('J\'ai ', 0),
new PrintNode(new NameExpression('foo', 0), 0),
new TextNode(' pommes', 0),
], [], 0);
$context = new Node([
]);
$context = new Nodes([
new TextNode('The context', 0),
], [], 0);
$domain = new Node([
]);
$domain = new Nodes([
new TextNode('mydomain', 0),
], [], 0);
]);
$node = new TransNode($body, null, null, $context, null, $domain, 0);
$tests[] = [
$node,
Expand All @@ -206,24 +206,24 @@ public function getTests(): array
];

$count = new ConstantExpression(12, 0);
$body = new Node([
$body = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have one apple', 0),
], [], 0);
$context = new Node([
]);
$context = new Nodes([
new TextNode('The context', 0),
], [], 0);
$domain = new Node([
]);
$domain = new Nodes([
new TextNode('mydomain', 0),
], [], 0);
$plural = new Node([
]);
$plural = new Nodes([
new TextNode('Hey ', 0),
new PrintNode(new NameExpression('name', 0), 0),
new TextNode(', I have ', 0),
new PrintNode(new NameExpression('count', 0), 0),
new TextNode(' apples', 0),
], [], 0);
]);
$node = new TransNode($body, $plural, $count, $context, null, $domain, 0);
$tests[] = [
$node,
Expand Down
Loading

0 comments on commit cb9fd67

Please sign in to comment.