Skip to content

Commit 31c8ddb

Browse files
committed
Make leading backslash in FQN optional. Fix #10
`{% types foo: "Exception" }` is now valid.
1 parent 315d56d commit 31c8ddb

File tree

9 files changed

+29
-8
lines changed

9 files changed

+29
-8
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 3.1.1
2+
* Feature (/ bugfix 😉): leading backslash in FQN is optional
3+
14
# 3.1.0
25
* Feature: Detect invalid named arguments in macro calls
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Here's the list of types supported by TwigQI:
145145
* Scalar: `string`, `number`, `boolean`, `null`, `object` (although a class is preferred)
146146
* Classes, interfaces and traits
147147

148-
Use FQNs with a starting backslash. Note that backslashes must be escaped in Twig strings [until v4](https://github.com/twigphp/Twig/pull/4199).
148+
FQNs _may_ contain a leading backslash. Note that backslashes must be escaped in Twig strings [until v4](https://github.com/twigphp/Twig/pull/4199).
149149
* Three types of iterables, with increasing specificity
150150
* `iterable` declares nothing more or less than that the variable is iterable
151151
* `iterable<ValueType>` declares the values' type

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

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Inspection/InvalidTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class InvalidTypes implements NodeVisitorInterface
2929
'float' => 'number',
3030
];
3131

32-
private const FQN_REGEX = '/^\\\\[A-Za-z_][A-Za-z0-9_]*(\\\\[A-Za-z_][A-Za-z0-9_]*)*$/';
32+
private const FQN_REGEX = '/^(\\\\)?[A-Za-z_][A-Za-z0-9_]*(\\\\[A-Za-z_][A-Za-z0-9_]*)*$/';
3333

3434
public function __construct(
3535
private readonly LoggerInterface $logger,

tests/InvalidConstantTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static function getConstants(): array
2626
['DATE_W3C', true],
2727
['DateTime', false],
2828
['DateTime::W3C', true],
29+
['\\\\DateTime::W3C', true],
2930
['DateTime::NOPE', false],
3031
['Twig\\\\Token::ARROW_TYPE', true],
3132
['Twig\\\\Token::NOPE', false],

tests/InvalidDotOperationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ public function test_itValidatesDotOperationOnObjectsEvenIfNullable(string $type
280280
);
281281
}
282282

283+
public function test_itSupportsFQNWithoutLeadingSlash(): void
284+
{
285+
$this->env->createTemplate(
286+
<<<EOF
287+
{% types {foo: 'Exception'} %}
288+
{{ foo.message }}
289+
EOF
290+
);
291+
292+
self::assertEmpty(
293+
$this->errors,
294+
"FQN without leading slash should be accepted"
295+
);
296+
}
297+
283298
public static function getAttributeShorthands(): array
284299
{
285300
$dummyClassFqn = '\\\\AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Dummy';

tests/InvalidEnumCaseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ protected function createUniqueExtensionClass(LoggerInterface $logger): Extensio
2222

2323
public static function getEnomCases(): array
2424
{
25-
$enum = '\\\\AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Enom';
25+
$enum = 'AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Enom';
2626

2727
return [
2828
[$enum, 'This', true],
29+
["\\\\$enum", 'This', true],
2930
[$enum, 'That', true],
3031
[$enum, 'Invalid', false],
3132
[$enum, 'cases', true],

tests/InvalidTypesTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public static function getTypes(): array
3737
["any", false],
3838
["resource", false],
3939

40-
["\\\\Exception", true],
41-
["\\\\Iterator", true],
40+
["\\\\Exception", true], // class
41+
["\\\\Iterator", true], // interface
4242
["\\\\AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Enom", true],
4343
["\\\\AlisQI\\\\TwigQI\\\\Tests\\\\Type\\\\Traet", true],
4444
["\\\\Twig\\\\Token", true],
45-
["Exception", false],
45+
["Exception", true], // leading \ is optional
46+
["Twig\\\\Token", true],
4647
["\\\\Foo", false],
4748
["\\\\Inv alid", false],
4849
["\\\\Inv-alid", false],

0 commit comments

Comments
 (0)