Skip to content

Commit 39c4868

Browse files
committed
Bugfix: dot operation validation works for nullable types
The following template will now trigger two Errors: ``` {% types { foo: '?string', bar: '?\\Exception', } %} {{ foo.bad }} {{ bar.bad }} ```
1 parent fc61537 commit 39c4868

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
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": "1.0.4",
11+
"version": "1.0.5",
1212
"autoload": {
1313
"psr-4": {
1414
"AlisQI\\TwigQI\\": "src/"

src/Inspection/InvalidDotOperation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ private function checkOperation(string $name, string $attribute, NodeLocation $l
9494

9595
$type = $variableTypeCollector->getDeclaredType($name);
9696

97+
if (str_starts_with($type, '?')) {
98+
$type = substr($type, 1);
99+
}
100+
97101
if (in_array($type, self::UNSUPPORTED_TYPES)) {
98102
trigger_error(
99103
sprintf('Invalid dot operation on unsupported type \'%s\' (at %s)', $type, $location),

tests/InvalidDotOperationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static function getInvalidTypesForDotOperator(): array
3939
['string'],
4040
['number'],
4141
['boolean'],
42+
['?string'],
43+
['?number'],
44+
['?boolean'],
4245
];
4346
}
4447

@@ -222,6 +225,23 @@ public function test_itValidatesDotOperationOnObjects(string $type, string $attr
222225
);
223226
}
224227

228+
/** @dataProvider getClassNamesAndAttributes */
229+
public function test_itValidatesDotOperationOnObjectsEvenIfNullable(string $type, string $attribute, bool $isValid): void
230+
{
231+
$this->env->createTemplate(
232+
<<<EOF
233+
{% types {foo: '?$type'} %}
234+
{{ foo.$attribute }}
235+
EOF
236+
);
237+
238+
self::assertEquals(
239+
$isValid,
240+
empty($this->errors),
241+
implode(', ', $this->errors)
242+
);
243+
}
244+
225245
public static function getAttributeShorthands(): array
226246
{
227247
$dummyClassFqn = '\\\\AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Dummy';

0 commit comments

Comments
 (0)