1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #3716 - prevent crash for Foo|? return type

This commit is contained in:
Brown 2020-06-29 17:52:55 -04:00
parent e56483bb54
commit 7253e01000
3 changed files with 26 additions and 0 deletions

View File

@ -563,6 +563,10 @@ class ParseTreeCreator
} else {
$new_parent = !$this->current_leaf instanceof ParseTree\Root ? $this->current_leaf : null;
if (!$next_token) {
throw new TypeParseTreeException('Unexpected token ?');
}
$new_leaf = new ParseTree\NullableTree(
$new_parent
);

View File

@ -342,6 +342,10 @@ class TypeParser
foreach ($parse_tree->children as $child_tree) {
if ($child_tree instanceof ParseTree\NullableTree) {
if (!isset($child_tree->children[0])) {
throw new TypeParseTreeException('Invalid ? character');
}
$atomic_type = self::getTypeFromTree(
$child_tree->children[0],
$codebase,

View File

@ -100,6 +100,24 @@ class TypeParseTest extends TestCase
$this->assertSame('int|null|string', (string) Type::parseString('?string|?int'));
}
/**
* @return void
*/
public function testBadNullableCharacterInUnion()
{
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
Type::parseString('int|array|?');
}
/**
* @return void
*/
public function testBadNullableCharacterInUnionWithFollowing()
{
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
Type::parseString('int|array|?|bool');
}
/**
* @return void
*/