Skip to content

Commit

Permalink
Fix deprecation in Twig 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Sep 6, 2024
1 parent 79b509c commit 0bfd63e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions phpstan.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- Symfony\Component\HttpKernel\Kernel::VERSION_ID
- Twig\Environment::MAJOR_VERSION

excludePaths:
- tests/application/public/bundles/
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Twig\Parser;
use Twig\Source;

use function method_exists;

final class DefaultContextTest extends TestCase
{
private Environment $environment;
Expand Down Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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',
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Twig\Parser;
use Twig\Source;

use function method_exists;

final class RenderZoneTest extends TestCase
{
private Environment $environment;
Expand Down Expand Up @@ -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'));
}

Expand Down Expand Up @@ -74,17 +80,17 @@ public static function compileDataProvider(): iterable
new NameExpression('zone', 1),
null,
1,
'nglayouts_render_zone',
),
'nglayouts_render_zone',
],
[
'{% nglayouts_render_zone zone context="json" %}',
new RenderZoneNode(
new NameExpression('zone', 1),
new ConstantExpression('json', 1),
1,
'nglayouts_render_zone',
),
'nglayouts_render_zone',
],
];
}
Expand Down

0 comments on commit 0bfd63e

Please sign in to comment.