Skip to content

Commit

Permalink
Merge pull request #7312 from orklah/empty-array
Browse files Browse the repository at this point in the history
Support PHPStan notation for empty-arrays
  • Loading branch information
orklah committed Jan 6, 2022
2 parents b6559dc + 9d3a51d commit f3bc55e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Internal/Type/ParseTreeCreator.php
Expand Up @@ -751,6 +751,16 @@ 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;
} elseif ($nexter_token === null) {
throw new TypeParseTreeException('Unclosed bracket in keyed array');
}

break;

case '(':
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -1233,7 +1233,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 @@ -1312,7 +1312,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: 0 additions & 8 deletions tests/AnnotationTest.php
Expand Up @@ -1490,14 +1490,6 @@ public function bar() {
',
'error_message' => 'UndefinedDocblockClass',
],
'preventBadTKeyedArrayFormat' => [
'<?php
/**
* @param array{} $arr
*/
function bar(array $arr): void {}',
'error_message' => 'InvalidDocblock',
],
'noPhpStormAnnotationsThankYou' => [
'<?php
/** @param ArrayIterator|string[] $i */
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 f3bc55e

Please sign in to comment.