From c69cb08528420a40ab8b1348a8d4295407d6400d Mon Sep 17 00:00:00 2001 From: Jeroen Versteeg Date: Sun, 27 Oct 2024 10:36:46 +0100 Subject: [PATCH] InvalidDotOperation supports variable overloading with {% set %} --- src/Inspection/InvalidDotOperation.php | 11 ++++++++--- tests/InvalidDotOperationTest.php | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Inspection/InvalidDotOperation.php b/src/Inspection/InvalidDotOperation.php index 122fbeb..f434941 100644 --- a/src/Inspection/InvalidDotOperation.php +++ b/src/Inspection/InvalidDotOperation.php @@ -17,6 +17,7 @@ use Twig\Node\MacroNode; use Twig\Node\ModuleNode; use Twig\Node\Node; +use Twig\Node\SetNode; use Twig\Node\TypesNode; use Twig\NodeVisitor\NodeVisitorInterface; use Twig\Template; @@ -62,7 +63,8 @@ public function enterNode(Node $node, Environment $env): Node if ( $node instanceof ArrowFunctionExpression || - $node instanceof ForNode + $node instanceof ForNode || + $node instanceof SetNode ) { foreach ($this->extractScopedVariableNames($node) as $name) { $this->getCurrentVariableTypeCollector()->push($name, 'mixed'); @@ -160,9 +162,12 @@ public function leaveNode(Node $node, Environment $env): ?Node } /** @return list */ - private function extractScopedVariableNames(ArrowFunctionExpression|ForNode $node): array + private function extractScopedVariableNames(ArrowFunctionExpression|ForNode|SetNode $node): array { - if ($node instanceof ArrowFunctionExpression) { + if ( + $node instanceof ArrowFunctionExpression || + $node instanceof SetNode + ) { $names = $node->getNode('names'); $variables = [$names->getNode('0')->getAttribute('name')]; diff --git a/tests/InvalidDotOperationTest.php b/tests/InvalidDotOperationTest.php index 1ea3afe..358c17a 100644 --- a/tests/InvalidDotOperationTest.php +++ b/tests/InvalidDotOperationTest.php @@ -271,6 +271,8 @@ public static function getTwigContentWithVariableOverloads(): array ['{{ []|filter((v, foo) => foo.bar) }}', true], ['{% for k, foo in [] %}{{ foo.bar }}{% endfor %}', true], ['{% for foo, v in [] %}{{ foo.bar }}{% endfor %}', true], + ['{% set foo = 1 %}{{ foo.bar }}', true], + ['{% set foo, bar = 1, 1 %}{{ foo.bar }}', true], ['{{ []|filter((foo) => foo.bar) }} {{ foo.baz }}', false], ['{% for foo in [] %}{{ foo.bar }}{% endfor %} {{ foo.baz }}', false],