Skip to content

Commit 0199eb2

Browse files
committed
InvalidDotOperation ignores non-constant attributes
The inspection threw an Exception if the `GetAttrExpression`'s attribute was not a `ConstantExpression`, such as ``` {{ foo.('bar' ?: 'baz') }} ``` Fixes #4
1 parent 4d939b1 commit 0199eb2

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "Jeroen Versteeg"
99
}
1010
],
11-
"version": "2.0.0",
11+
"version": "2.0.1",
1212
"autoload": {
1313
"psr-4": {
1414
"AlisQI\\TwigQI\\": "src/"

src/Inspection/InvalidDotOperation.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use ReflectionClass;
1111
use Twig\Environment;
1212
use Twig\Node\Expression\ArrowFunctionExpression;
13+
use Twig\Node\Expression\ConstantExpression;
1314
use Twig\Node\Expression\GetAttrExpression;
15+
use Twig\Node\Expression\NameExpression;
1416
use Twig\Node\Expression\Variable\ContextVariable;
1517
use Twig\Node\ForNode;
1618
use Twig\Node\MacroNode;
@@ -73,11 +75,12 @@ public function enterNode(Node $node, Environment $env): Node
7375
if (
7476
$node instanceof GetAttrExpression &&
7577
$node->getAttribute('type') !== Template::ARRAY_CALL &&
76-
($nameNode = $node->getNode('node')) instanceof ContextVariable
78+
($nameNode = $node->getNode('node')) instanceof ContextVariable &&
79+
($attributeNode = $node->getNode('attribute')) instanceof ConstantExpression
7780
) {
7881
$location = new NodeLocation($node);
7982
$name = $nameNode->getAttribute('name');
80-
$attribute = $node->getNode('attribute')->getAttribute('value');
83+
$attribute = $attributeNode->getAttribute('value');
8184
$this->checkOperation($name, $attribute, $location);
8285
}
8386

@@ -97,7 +100,7 @@ private function checkOperation(string $name, string $attribute, NodeLocation $l
97100
if (str_starts_with($type, '?')) {
98101
$type = substr($type, 1);
99102
}
100-
103+
101104
if (in_array($type, self::UNSUPPORTED_TYPES)) {
102105
trigger_error(
103106
sprintf('Invalid dot operation on unsupported type \'%s\' (at %s)', $type, $location),
@@ -187,7 +190,7 @@ private function extractScopedVariableTypes(ArrowFunctionExpression|ForNode|SetN
187190
}
188191

189192
if ($node instanceof ForNode) {
190-
$keyName = $node->getNode('key_target') ->getAttribute('name');
193+
$keyName = $node->getNode('key_target')->getAttribute('name');
191194
$valueName = $node->getNode('value_target')->getAttribute('name');
192195

193196
$keyType = $valueType = 'mixed';

tests/InvalidDotOperationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ public function test_itIgnoresAttributesOnExpressions(): void
3131
);
3232
}
3333

34+
public function test_itIgnoresEnumCaseValues(): void
35+
{
36+
$this->env->createTemplate(<<<EOF
37+
{{ enum('\\\\AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Enom').This.value }}
38+
EOF);
39+
40+
self::assertEmpty(
41+
$this->errors,
42+
implode(', ', $this->errors)
43+
);
44+
}
45+
46+
public function test_itIgnoresAttributeExpressions(): void
47+
{
48+
$this->env->createTemplate(<<<EOF
49+
{{ foo.('bar' ?: 'baz') }}
50+
EOF);
51+
52+
self::assertEmpty(
53+
$this->errors,
54+
implode(', ', $this->errors)
55+
);
56+
}
57+
3458
public function test_itIgnoresUndeclaredVariables(): void
3559
{
3660
$this->env->createTemplate(<<<EOF

tests/Type/Enom.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace AlisQI\TwigQI\Tests\Type;
66

7-
enum Enom {
8-
case This;
9-
case That;
7+
enum Enom: string {
8+
case This = 'this';
9+
case That = 'that';
1010
}

0 commit comments

Comments
 (0)