1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #959 - allow nullable followed by parentheses

This commit is contained in:
Matt Brown 2018-08-29 16:03:16 -04:00
parent 17ea48eda4
commit 7dd3273a62
2 changed files with 22 additions and 9 deletions

View File

@ -387,18 +387,23 @@ abstract class Type
} }
if ($parse_tree instanceof ParseTree\NullableTree) { if ($parse_tree instanceof ParseTree\NullableTree) {
$atomic_type = self::getTypeFromTree($parse_tree->children[0], false, $template_type_names); $non_nullable_type = self::getTypeFromTree($parse_tree->children[0], false, $template_type_names);
if (!$atomic_type instanceof Atomic) { if ($non_nullable_type instanceof Union) {
throw new \UnexpectedValueException( $non_nullable_type->addType(new TNull);
'Was expecting an atomic type, got ' . get_class($atomic_type) return $non_nullable_type;
);
} }
return TypeCombination::combineTypes([ if ($non_nullable_type instanceof Atomic) {
new TNull, return TypeCombination::combineTypes([
$atomic_type new TNull,
]); $non_nullable_type
]);
}
throw new \UnexpectedValueException(
'Was expecting an atomic or union type, got ' . get_class($non_nullable_type)
);
} }
if (!$parse_tree instanceof ParseTree\Value) { if (!$parse_tree instanceof ParseTree\Value) {

View File

@ -61,6 +61,14 @@ class TypeParseTest extends TestCase
$this->assertSame('null|string', (string) Type::parseString('?string')); $this->assertSame('null|string', (string) Type::parseString('?string'));
} }
/**
* @return void
*/
public function testNullableUnion()
{
$this->assertSame('string|int|null', (string) Type::parseString('?(string|int)'));
}
/** /**
* @return void * @return void
*/ */