Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing nullable collections #168

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/CollectionResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>', new Context(''));

$this->assertInstanceOf(Types\Nullable::class, $resolvedType);
$this->assertSame('?int[]', (string) $resolvedType);
}
}
2 changes: 2 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this behavior should change. I do understand that PHP doesn't allow this format and that it looks more than ugly. But this case has been added because we found some repositories using this notation.

As this library is used to parse types in docblocks and native PHP, I think we should keep this format supported.
It could result in a deprecation that we will remove in the future. But for now, this is a backward compatibility break which we cannot do with the number of consuming projects for this library.

Do you think it is possible to revert this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I did not fully appreciate the need for backwards compatibility, so I have restored support for shorthand nullable syntax in compound types as requested. Please let me know if this solution is not satisfactory.

Expand Down