Skip to content

Commit daf6ab4

Browse files
committed
Restore support for shorthand nullable compounds
While PHP types do not support shorthand nullable syntax in compound types (e.g. `?string|int`), it has been requested to retain support for this in phpdoc types for backwards compatibility reasons.
1 parent 370845d commit daf6ab4

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/TypeResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
210210
self::PARSER_IN_COMPOUND,
211211
self::PARSER_IN_ARRAY_EXPRESSION,
212212
self::PARSER_IN_COLLECTION_EXPRESSION,
213+
self::PARSER_IN_NULLABLE,
213214
], true)
214215
) {
215216
throw new RuntimeException(
@@ -225,6 +226,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
225226
self::PARSER_IN_COMPOUND,
226227
self::PARSER_IN_ARRAY_EXPRESSION,
227228
self::PARSER_IN_COLLECTION_EXPRESSION,
229+
self::PARSER_IN_NULLABLE,
228230
], true)
229231
) {
230232
throw new RuntimeException(

tests/unit/TypeResolverTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,11 @@ public function testResolvingNullableCompoundTypes(): void
407407
{
408408
$fixture = new TypeResolver();
409409

410-
$this->expectException('RuntimeException');
411-
$this->expectExceptionMessage('Unexpected type separator');
410+
// Note that in PHP types it is illegal to use shorthand nullable
411+
// syntax with unions. This would be 'string|boolean|null' instead.
412412
$resolvedType = $fixture->resolve('?string|null|?boolean');
413+
414+
$this->assertSame('?string|null|?bool', (string) $resolvedType);
413415
}
414416

415417
/**

0 commit comments

Comments
 (0)