Skip to content

Commit

Permalink
parse array{} into an empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Jan 5, 2022
1 parent f5dc2ee commit 030195a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Type/ParseTreeCreator.php
Expand Up @@ -751,6 +751,13 @@ private function handleValue(array $type_token): void
$new_parent
);
++$this->t;

$nexter_token = $this->t + 1 < $this->type_token_count ? $this->type_tokens[$this->t + 1] : null;

if ($nexter_token !== null && $nexter_token[0] === '}') {
$new_leaf->terminated = true;
++$this->t;
}
break;

case '(':
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -1235,7 +1235,7 @@ private static function getTypeFromIndexAccessTree(
/**
* @param array<string, array<string, Union>> $template_type_map
* @param array<string, TypeAlias> $type_aliases
* @return TCallableKeyedArray|TKeyedArray|TObjectWithProperties
* @return TCallableKeyedArray|TKeyedArray|TObjectWithProperties|TArray
* @throws TypeParseTreeException
*/
private static function getTypeFromKeyedArrayTree(
Expand Down Expand Up @@ -1314,7 +1314,7 @@ private static function getTypeFromKeyedArrayTree(
}

if (!$properties) {
throw new TypeParseTreeException('No properties supplied for TKeyedArray');
return new TArray([Type::getNever(), Type::getNever()]);
}

if ($type === 'object') {
Expand Down
8 changes: 8 additions & 0 deletions tests/TypeParseTest.php
Expand Up @@ -893,6 +893,14 @@ public function testSingleLiteralString(): void
);
}

public function testEmptyArrayShape(): void
{
$this->assertSame(
'array<never, never>',
(string)Type::parseString('array{}')
);
}

public function testSingleLiteralInt(): void
{
$this->assertSame(
Expand Down

0 comments on commit 030195a

Please sign in to comment.