From b6df23a09bb65eff612c7979be2bb46ac00be760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Fri, 6 Sep 2024 13:25:06 +0200 Subject: [PATCH] Fix deprecation in Twig 3.12 --- .../Templating/Twig/Node/DefaultContext.php | 5 ++++- .../LayoutsBundle/Templating/Twig/Node/RenderZone.php | 4 +++- phpstan.neon | 2 ++ phpstan.tests.neon | 3 +++ .../Templating/Twig/TokenParser/DefaultContextTest.php | 10 +++++++++- .../Templating/Twig/TokenParser/RenderZoneTest.php | 10 +++++++++- 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php index 2110eeeac..984295047 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php @@ -6,6 +6,7 @@ use Twig\Attribute\YieldReady; use Twig\Compiler; +use Twig\Environment; use Twig\Node\Expression\AbstractExpression; use Twig\Node\Node; @@ -14,7 +15,9 @@ final class DefaultContext extends Node { public function __construct(AbstractExpression $expr, int $line = 0, ?string $tag = null) { - parent::__construct(['expr' => $expr], [], $line, $tag); + Environment::MAJOR_VERSION === 3 && Environment::MINOR_VERSION < 12 ? + parent::__construct(['expr' => $expr], [], $line, $tag) : + parent::__construct(['expr' => $expr], [], $line); } public function compile(Compiler $compiler): void diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php index 3ddeca6b1..cf7a3e2a2 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php @@ -26,7 +26,9 @@ public function __construct(AbstractExpression $zone, ?AbstractExpression $conte $nodes['context'] = $context; } - parent::__construct($nodes, [], $line, $tag); + Environment::MAJOR_VERSION === 3 && Environment::MINOR_VERSION < 12 ? + parent::__construct($nodes, [], $line, $tag) : + parent::__construct($nodes, [], $line); } public function compile(Compiler $compiler): void diff --git a/phpstan.neon b/phpstan.neon index d92fd4f74..b094ddc1d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,6 +8,8 @@ parameters: dynamicConstantNames: - Symfony\Component\HttpKernel\Kernel::VERSION_ID - Twig\Environment::VERSION_ID + - Twig\Environment::MAJOR_VERSION + - Twig\Environment::MINOR_VERSION ignoreErrors: # Doctrine DBAL diff --git a/phpstan.tests.neon b/phpstan.tests.neon index 380327450..5d54c1bc3 100644 --- a/phpstan.tests.neon +++ b/phpstan.tests.neon @@ -9,6 +9,7 @@ parameters: treatPhpDocTypesAsCertain: false dynamicConstantNames: - Symfony\Component\HttpKernel\Kernel::VERSION_ID + - Twig\Environment::MAJOR_VERSION excludePaths: - tests/application/public/bundles/ @@ -53,6 +54,8 @@ parameters: message: '#Undefined variable: \$this#' path: tests/lib/Transfer/Output/Visitor/Integration + - "#Call to function method_exists\\(\\) with .* and 'setNodeTag' will always evaluate to true.#" + - message: "#Offset 'db' does not exist on array#" path: tests/lib/Persistence/Doctrine/DatabaseTrait.php diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php index 4e1e2968a..a56d9fd2a 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php @@ -15,6 +15,8 @@ use Twig\Parser; use Twig\Source; +use function method_exists; + final class DefaultContextTest extends TestCase { private Environment $environment; @@ -42,10 +44,14 @@ protected function setUp(): void * * @dataProvider compileDataProvider */ - public function testCompile(string $source, DefaultContextNode $node): void + public function testCompile(string $source, DefaultContextNode $node, string $tag): void { $stream = $this->environment->tokenize(new Source($source, '')); + if (method_exists($node, 'setNodeTag')) { + $node->setNodeTag($tag); + } + self::assertSame((string) $node, (string) $this->parser->parse($stream)->getNode('body')->getNode('0')); } @@ -75,6 +81,7 @@ public static function compileDataProvider(): iterable 1, 'nglayouts_default_context', ), + 'nglayouts_default_context', ], [ '{% nglayouts_default_context "foo" %}', @@ -83,6 +90,7 @@ public static function compileDataProvider(): iterable 1, 'nglayouts_default_context', ), + 'nglayouts_default_context', ], ]; } diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php index 167f49f69..39675042b 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php @@ -15,6 +15,8 @@ use Twig\Parser; use Twig\Source; +use function method_exists; + final class RenderZoneTest extends TestCase { private Environment $environment; @@ -42,10 +44,14 @@ protected function setUp(): void * * @dataProvider compileDataProvider */ - public function testCompile(string $source, RenderZoneNode $node): void + public function testCompile(string $source, RenderZoneNode $node, string $tag): void { $stream = $this->environment->tokenize(new Source($source, '')); + if (method_exists($node, 'setNodeTag')) { + $node->setNodeTag($tag); + } + self::assertSame((string) $node, (string) $this->parser->parse($stream)->getNode('body')->getNode('0')); } @@ -76,6 +82,7 @@ public static function compileDataProvider(): iterable 1, 'nglayouts_render_zone', ), + 'nglayouts_render_zone', ], [ '{% nglayouts_render_zone zone context="json" %}', @@ -85,6 +92,7 @@ public static function compileDataProvider(): iterable 1, 'nglayouts_render_zone', ), + 'nglayouts_render_zone', ], ]; }