Skip to content

Commit

Permalink
Merge pull request #1301 from ruudk/fix-nullable
Browse files Browse the repository at this point in the history
`Undefined offset: 0` when using `@var null|string` instead of `@var string|null`
  • Loading branch information
goetas authored Mar 22, 2021
2 parents 0887ce8 + 413909d commit b1e5bb6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Metadata/Driver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ private function flattenVarTagValueTypes(array $varTagValues): array
*/
private function filterNullFromTypes(array $types): array
{
return array_filter(array_map(function (TypeNode $node) {
return array_values(array_filter(array_map(function (TypeNode $node) {
return $this->isNullType($node) ? null : $node;
}, $types));
}, $types)));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/ObjectWithPhpDocProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ final class ObjectWithPhpDocProperty
*/
private $emptyBlock;

/**
* @var string|null
*/
private $firstname;

/**
* @var null|string
*/
private $lastname;

}
17 changes: 17 additions & 0 deletions tests/Metadata/Driver/DocBlockTypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ public function testGetPropertyDocblockTypeHintDoesNotCrash(): void
)
);
}

public function testGetPropertyDocblockTypeHintDoesNotCrashWhenUnionType(): void
{
$resolver = new DocBlockTypeResolver();
self::assertSame(
'string',
$resolver->getPropertyDocblockTypeHint(
new ReflectionProperty(ObjectWithPhpDocProperty::class, 'firstname')
)
);
self::assertSame(
'string',
$resolver->getPropertyDocblockTypeHint(
new ReflectionProperty(ObjectWithPhpDocProperty::class, 'lastname')
)
);
}
}

0 comments on commit b1e5bb6

Please sign in to comment.