From d02b2bd112517653d84b200943850d375b37d14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 19 Apr 2024 16:20:00 -0300 Subject: [PATCH] Drop support for Twig < 3.9.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- CHANGELOG.md | 1 + composer.json | 4 ++-- src/Node/TransNode.php | 14 +++---------- src/TokenParser/TransTokenParser.php | 4 ++-- test/Node/MoTranslatorTransTest.php | 27 +++++++----------------- test/Node/TransTest.php | 31 +++++++++++----------------- 6 files changed, 28 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7348358..729867d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] +* Drop support for Twig < 3.9.0 * Drop support for PHP 7.2, PHP 7.3, PHP 7.4 and PHP 8.0 ## [4.1.1] - 2024-04-19 diff --git a/composer.json b/composer.json index 6a0a3f2..c84e9d2 100644 --- a/composer.json +++ b/composer.json @@ -21,11 +21,11 @@ }, "require": { "php": "^8.1", - "twig/twig": "^3.7" + "twig/twig": "^3.9" }, "require-dev": { "phpmyadmin/coding-standard": "^4.0", - "phpmyadmin/motranslator": "^5.2", + "phpmyadmin/motranslator": "^6.0-dev", "phpstan/extension-installer": "^1.3", "phpstan/phpstan": "^1.10", "phpstan/phpstan-phpunit": "^1.3", diff --git a/src/Node/TransNode.php b/src/Node/TransNode.php index e498918..2d4ca36 100644 --- a/src/Node/TransNode.php +++ b/src/Node/TransNode.php @@ -27,7 +27,6 @@ use Twig\Node\TextNode; use function array_merge; -use function class_exists; use function count; use function sprintf; use function str_replace; @@ -122,14 +121,13 @@ public function compile(Compiler $compiler) if ($this->hasNode('notes')) { $message = trim($this->getNode('notes')->getAttribute('data')); - // line breaks are not allowed cause we want a single line comment + // line breaks are not allowed because we want a single line comment $message = str_replace(["\n", "\r"], ' ', $message); $compiler->raw(static::$notesLabel . $message . "\n"); } if ($vars) { - $compiler - ->raw($this->echoOrYield() . ' strtr(' . $function . '('); + $compiler->raw('yield strtr(' . $function . '('); if ($hasDomain) { [$domain] = $this->compileString($this->getNode('domain')); @@ -178,8 +176,7 @@ public function compile(Compiler $compiler) $compiler->raw("));\n"); } else { - $compiler - ->raw($this->echoOrYield() . ' ' . $function . '('); + $compiler->raw('yield ' . $function . '('); if ($hasDomain) { [$domain] = $this->compileString($this->getNode('domain')); @@ -311,9 +308,4 @@ protected function getTransFunction(bool $hasPlural, bool $hasContext, bool $has // gettext($msgid); return $functionPrefix . ($hasContext ? 'pgettext' : 'gettext'); } - - private function echoOrYield(): string - { - return class_exists(YieldReady::class) ? 'yield' : 'echo'; - } } diff --git a/src/TokenParser/TransTokenParser.php b/src/TokenParser/TransTokenParser.php index 62e50c1..44fd954 100644 --- a/src/TokenParser/TransTokenParser.php +++ b/src/TokenParser/TransTokenParser.php @@ -95,12 +95,12 @@ protected function preParse(Token $token): array $this->checkTransString($body, $lineno); if ($notes instanceof TextNode) { - // Don't use TextNode for $notes to avoid it getting merged with $body in Twig >= 3.9.0. + // Don't use TextNode for $notes to avoid it getting merged with $body when optimizing. $notes = new Node([], ['data' => $notes->getAttribute('data')], $notes->getTemplateLine()); } if ($context instanceof TextNode) { - // Don't use TextNode for $context to avoid it getting merged with $body in Twig >= 3.9.0. + // Don't use TextNode for $context to avoid it getting merged with $body when optimizing. $context = new Node([], ['data' => $context->getAttribute('data')], $context->getTemplateLine()); } diff --git a/test/Node/MoTranslatorTransTest.php b/test/Node/MoTranslatorTransTest.php index 1c413c0..d7e6092 100644 --- a/test/Node/MoTranslatorTransTest.php +++ b/test/Node/MoTranslatorTransTest.php @@ -15,7 +15,6 @@ namespace PhpMyAdmin\Tests\Twig\Extensions\Node; use PhpMyAdmin\Twig\Extensions\Node\TransNode; -use Twig\Attribute\YieldReady; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\NameExpression; use Twig\Node\Node; @@ -23,16 +22,10 @@ use Twig\Node\TextNode; use Twig\Test\NodeTestCase; -use function class_exists; use function sprintf; class MoTranslatorTransTest extends NodeTestCase { - private function echoOrYield(): string - { - return class_exists(YieldReady::class) ? 'yield' : 'echo'; - } - public static function setUpBeforeClass(): void { TransNode::$notesLabel = '// l10n: '; @@ -89,7 +82,7 @@ public function getTests(): array $node = new TransNode($body, null, null, null, null, $domain, 0); $tests[] = [ $node, - sprintf($this->echoOrYield() . ' _dgettext("coredomain", %s);', $this->getVariableGetter('foo')), + sprintf('yield _dgettext("coredomain", %s);', $this->getVariableGetter('foo')), ]; $body = new NameExpression('foo', 0); @@ -103,7 +96,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() . ' _dpgettext("coredomain", "The context", %s);', + 'yield _dpgettext("coredomain", "The context", %s);', $this->getVariableGetter('foo'), ), ]; @@ -117,7 +110,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() . ' strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', + 'yield strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo'), ), ]; @@ -139,7 +132,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() . ' strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,' + 'yield strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,' . ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,' . ' "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), @@ -159,8 +152,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() - . ' strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', + 'yield strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo'), ), ]; @@ -185,8 +177,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() - . ' strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,' + 'yield strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,' . ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,' . ' "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), @@ -209,8 +200,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() - . ' strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', + 'yield strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo'), ), ]; @@ -238,8 +228,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() - . ' strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",' + 'yield strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",' . ' "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s,' . ' "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), diff --git a/test/Node/TransTest.php b/test/Node/TransTest.php index a902027..b051a9d 100644 --- a/test/Node/TransTest.php +++ b/test/Node/TransTest.php @@ -15,7 +15,6 @@ namespace PhpMyAdmin\Tests\Twig\Extensions\Node; use PhpMyAdmin\Twig\Extensions\Node\TransNode; -use Twig\Attribute\YieldReady; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\FilterExpression; use Twig\Node\Expression\NameExpression; @@ -24,16 +23,10 @@ use Twig\Node\TextNode; use Twig\Test\NodeTestCase; -use function class_exists; use function sprintf; class TransTest extends NodeTestCase { - private function echoOrYield(): string - { - return class_exists(YieldReady::class) ? 'yield' : 'echo'; - } - public function testConstructor(): void { $count = new ConstantExpression(12, 0); @@ -97,7 +90,7 @@ public function testEnableDebugNotEnabled(): void $sourceCode = $compiler->compile($node)->getSource(); $this->assertSame( '// custom: Notes for translators' . "\n" - . $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",' + . 'yield strtr(ngettext("There is 1 pending task",' . ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n", $sourceCode, ); @@ -126,7 +119,7 @@ public function testEnableDebugEnabled(): void $sourceCode = $compiler->compile($node)->getSource(); $this->assertSame( '// line 80' . "\n" . '// custom: Notes for translators' . "\n" - . $this->echoOrYield() . ' strtr(ngettext("There' + . 'yield strtr(ngettext("There' . ' is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n", $sourceCode, ); @@ -147,22 +140,22 @@ public function getTests(): array $node = new TransNode($body, null, null, null, null, $domain, 0); $tests[] = [ $node, - sprintf($this->echoOrYield() . ' dgettext("coredomain", %s);', $this->getVariableGetter('foo')), + sprintf('yield dgettext("coredomain", %s);', $this->getVariableGetter('foo')), ]; $body = new NameExpression('foo', 0); $node = new TransNode($body, null, null, null, null, null, 0); - $tests[] = [$node, sprintf($this->echoOrYield() . ' gettext(%s);', $this->getVariableGetter('foo'))]; + $tests[] = [$node, sprintf('yield gettext(%s);', $this->getVariableGetter('foo'))]; $body = new ConstantExpression('Hello', 0); $node = new TransNode($body, null, null, null, null, null, 0); - $tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");']; + $tests[] = [$node, 'yield gettext("Hello");']; $body = new Node([ new TextNode('Hello', 0), ], [], 0); $node = new TransNode($body, null, null, null, null, null, 0); - $tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");']; + $tests[] = [$node, 'yield gettext("Hello");']; $body = new Node([ new TextNode('J\'ai ', 0), @@ -173,7 +166,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', + 'yield strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo'), ), ]; @@ -195,7 +188,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() . ' strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have' + 'yield strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have' . ' %%count%% apples", abs(12)), array("%%name%%" => %s,' . ' "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), @@ -217,7 +210,7 @@ public function getTests(): array $tests[] = [ $node, sprintf( - $this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', + 'yield strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo'), ), ]; @@ -226,7 +219,7 @@ public function getTests(): array $body = new ConstantExpression('Hello', 0); $notes = new TextNode('Notes for translators', 0); $node = new TransNode($body, null, null, null, $notes, null, 0); - $tests[] = [$node, "// notes: Notes for translators\n" . $this->echoOrYield() . ' gettext("Hello");']; + $tests[] = [$node, "// notes: Notes for translators\n" . 'yield gettext("Hello");']; $body = new ConstantExpression('Hello', 0); $notes = new TextNode("Notes for translators\nand line breaks", 0); @@ -234,7 +227,7 @@ public function getTests(): array $tests[] = [ $node, "// notes: Notes for translators and line breaks\n" - . $this->echoOrYield() . ' gettext("Hello");', + . 'yield gettext("Hello");', ]; $count = new ConstantExpression(5, 0); @@ -249,7 +242,7 @@ public function getTests(): array $tests[] = [ $node, '// notes: Notes for translators' . "\n" - . $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",' + . 'yield strtr(ngettext("There is 1 pending task",' . ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));', ];