Skip to content

Commit

Permalink
Merge #18 - Fix deprecations from Twig
Browse files Browse the repository at this point in the history
Pull-request: #18
Fixes: #17
Fixes: #16
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Jan 7, 2025
2 parents ea585ce + 23db94d commit 4fae79a
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 134 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint-and-analyse-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up PHP 8.1
- name: Set up PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.2

- name: Validate composer.json and composer.lock
run: composer validate --strict
Expand All @@ -33,10 +33,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up PHP 8.1
- name: Set up PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.2

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.2', '8.3', '8.4']
experimental: [false]
composer-options: ['']
os: [ubuntu-latest]
include:
- { php-version: '8.4', composer-options: '--ignore-platform-req=php+', experimental: true, os: ubuntu-latest }
- { php-version: '8.5', composer-options: '--ignore-platform-req=php+', experimental: true, os: ubuntu-latest }
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## [Unreleased]

* Drop support for Twig < 3.13.0
* Drop support for PHP 7.2, PHP 7.3, PHP 7.4 and PHP 8.0
* Drop support for Twig < 3.17.0
* Drop support for PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0 and PHP 8.1

## [4.1.3] - 2024-09-08

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"source": "https://github.com/phpmyadmin/twig-i18n-extension"
},
"require": {
"php": "^8.1",
"twig/twig": "^3.13"
"php": "^8.2",
"twig/twig": "^3.17"
},
"require-dev": {
"phpmyadmin/coding-standard": "^4.0",
Expand Down
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
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);
}
}
16 changes: 6 additions & 10 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 All @@ -253,7 +249,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
9 changes: 5 additions & 4 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

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;
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 @@ -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 Expand Up @@ -132,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
Loading

0 comments on commit 4fae79a

Please sign in to comment.