Skip to content

Commit

Permalink
InvalidDotOperation supports variable overloading with {% set %}
Browse files Browse the repository at this point in the history
  • Loading branch information
drjayvee committed Oct 27, 2024
1 parent ff51570 commit c69cb08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Inspection/InvalidDotOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -160,9 +162,12 @@ public function leaveNode(Node $node, Environment $env): ?Node
}

/** @return list<string> */
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')];
Expand Down
2 changes: 2 additions & 0 deletions tests/InvalidDotOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit c69cb08

Please sign in to comment.