Skip to content

Commit 9aaa423

Browse files
authored
Merge pull request #168 from hemberger/issue-163
Fix parsing nullable collections
2 parents 77a3251 + daf6ab4 commit 9aaa423

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/TypeResolver.php

Lines changed: 3 additions & 6 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(
@@ -293,13 +295,8 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
293295

294296
$tokens->next();
295297
} else {
296-
$type = $this->resolveSingleType($token, $context);
298+
$types[] = $this->resolveSingleType($token, $context);
297299
$tokens->next();
298-
if ($parserContext === self::PARSER_IN_NULLABLE) {
299-
return $type;
300-
}
301-
302-
$types[] = $type;
303300
}
304301
}
305302

tests/unit/CollectionResolverTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,21 @@ public function testResolvingList(): void
328328
$this->assertInstanceOf(Types\String_::class, $valueType);
329329
$this->assertInstanceOf(Types\Integer::class, $keyType);
330330
}
331+
332+
/**
333+
* @uses \phpDocumentor\Reflection\Types\Context
334+
* @uses \phpDocumentor\Reflection\Types\Nullable
335+
*
336+
* @covers ::__construct
337+
* @covers ::resolve
338+
*/
339+
public function testResolvingNullableArray(): void
340+
{
341+
$fixture = new TypeResolver();
342+
343+
$resolvedType = $fixture->resolve('?array<int>', new Context(''));
344+
345+
$this->assertInstanceOf(Types\Nullable::class, $resolvedType);
346+
$this->assertSame('?int[]', (string) $resolvedType);
347+
}
331348
}

tests/unit/TypeResolverTest.php

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

410+
// Note that in PHP types it is illegal to use shorthand nullable
411+
// syntax with unions. This would be 'string|boolean|null' instead.
410412
$resolvedType = $fixture->resolve('?string|null|?boolean');
411413

412414
$this->assertSame('?string|null|?bool', (string) $resolvedType);

0 commit comments

Comments
 (0)