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:
parent
e56483bb54
commit
7253e01000
@ -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
|
||||
);
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user