From 0bfd63eae236bc1baf5a39a1003b3db78ce7351e 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 | 4 ++-- .../Templating/Twig/Node/RenderZone.php | 4 ++-- .../Templating/Twig/TokenParser/DefaultContext.php | 2 +- .../Templating/Twig/TokenParser/RenderZone.php | 2 +- phpstan.tests.neon | 3 +++ .../Twig/TokenParser/DefaultContextTest.php | 12 +++++++++--- .../Templating/Twig/TokenParser/RenderZoneTest.php | 12 +++++++++--- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php index 2110eeeac..2d3cb324f 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php @@ -12,9 +12,9 @@ #[YieldReady] final class DefaultContext extends Node { - public function __construct(AbstractExpression $expr, int $line = 0, ?string $tag = null) + public function __construct(AbstractExpression $expr, int $line = 0) { - 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..f5a89ca66 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php @@ -19,14 +19,14 @@ #[YieldReady] final class RenderZone extends Node { - public function __construct(AbstractExpression $zone, ?AbstractExpression $context = null, int $line = 0, ?string $tag = null) + public function __construct(AbstractExpression $zone, ?AbstractExpression $context = null, int $line = 0) { $nodes = ['zone' => $zone]; if ($context instanceof AbstractExpression) { $nodes['context'] = $context; } - parent::__construct($nodes, [], $line, $tag); + parent::__construct($nodes, [], $line); } public function compile(Compiler $compiler): void diff --git a/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php b/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php index 0fcf61a7f..978fbaaad 100644 --- a/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php +++ b/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php @@ -17,7 +17,7 @@ public function parse(Token $token): Node $this->parser->getStream()->expect(Token::BLOCK_END_TYPE); - return new DefaultContextNode($expression, $token->getLine(), $this->getTag()); + return new DefaultContextNode($expression, $token->getLine()); } public function getTag(): string diff --git a/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php b/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php index b63b0dda3..35099ec68 100644 --- a/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php +++ b/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php @@ -45,7 +45,7 @@ public function parse(Token $token): Node $stream->expect(Token::BLOCK_END_TYPE); - return new RenderZoneNode($zone, $context, $token->getLine(), $this->getTag()); + return new RenderZoneNode($zone, $context, $token->getLine()); } public function getTag(): string 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..fc9549d17 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')); } @@ -73,16 +79,16 @@ public static function compileDataProvider(): iterable new DefaultContextNode( new NameExpression('foo', 1), 1, - 'nglayouts_default_context', ), + 'nglayouts_default_context', ], [ '{% nglayouts_default_context "foo" %}', new DefaultContextNode( new ConstantExpression('foo', 1), 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..da33610f0 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')); } @@ -74,8 +80,8 @@ public static function compileDataProvider(): iterable new NameExpression('zone', 1), null, 1, - 'nglayouts_render_zone', ), + 'nglayouts_render_zone', ], [ '{% nglayouts_render_zone zone context="json" %}', @@ -83,8 +89,8 @@ public static function compileDataProvider(): iterable new NameExpression('zone', 1), new ConstantExpression('json', 1), 1, - 'nglayouts_render_zone', ), + 'nglayouts_render_zone', ], ]; }