Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHPStan notation for empty-arrays #7312

Merged
merged 2 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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: 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