diff --git a/src/TypeResolver.php b/src/TypeResolver.php index ca36026..f8bbae8 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -210,6 +210,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser self::PARSER_IN_COMPOUND, self::PARSER_IN_ARRAY_EXPRESSION, self::PARSER_IN_COLLECTION_EXPRESSION, + self::PARSER_IN_NULLABLE, ], true) ) { throw new RuntimeException( @@ -225,6 +226,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser self::PARSER_IN_COMPOUND, self::PARSER_IN_ARRAY_EXPRESSION, self::PARSER_IN_COLLECTION_EXPRESSION, + self::PARSER_IN_NULLABLE, ], true) ) { throw new RuntimeException( @@ -293,13 +295,8 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser $tokens->next(); } else { - $type = $this->resolveSingleType($token, $context); + $types[] = $this->resolveSingleType($token, $context); $tokens->next(); - if ($parserContext === self::PARSER_IN_NULLABLE) { - return $type; - } - - $types[] = $type; } } diff --git a/tests/unit/CollectionResolverTest.php b/tests/unit/CollectionResolverTest.php index 379aafe..40c337e 100644 --- a/tests/unit/CollectionResolverTest.php +++ b/tests/unit/CollectionResolverTest.php @@ -328,4 +328,21 @@ public function testResolvingList(): void $this->assertInstanceOf(Types\String_::class, $valueType); $this->assertInstanceOf(Types\Integer::class, $keyType); } + + /** + * @uses \phpDocumentor\Reflection\Types\Context + * @uses \phpDocumentor\Reflection\Types\Nullable + * + * @covers ::__construct + * @covers ::resolve + */ + public function testResolvingNullableArray(): void + { + $fixture = new TypeResolver(); + + $resolvedType = $fixture->resolve('?array', new Context('')); + + $this->assertInstanceOf(Types\Nullable::class, $resolvedType); + $this->assertSame('?int[]', (string) $resolvedType); + } } diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index 6fd282f..47557a6 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -407,6 +407,8 @@ public function testResolvingNullableCompoundTypes(): void { $fixture = new TypeResolver(); + // Note that in PHP types it is illegal to use shorthand nullable + // syntax with unions. This would be 'string|boolean|null' instead. $resolvedType = $fixture->resolve('?string|null|?boolean'); $this->assertSame('?string|null|?bool', (string) $resolvedType);