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

Improve parsing of list shapes #8841

Merged
merged 5 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
39 changes: 33 additions & 6 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -1390,6 +1390,12 @@ private static function getTypeFromKeyedArrayTree(

$type = $parse_tree->value;

$had_optional = false;
$had_explicit = false;
$had_implicit = false;

$previous_property_key = -1;

$is_list = true;

$sealed = true;
Expand Down Expand Up @@ -1419,7 +1425,8 @@ private static function getTypeFromKeyedArrayTree(
$from_docblock
);
$property_maybe_undefined = false;
$property_key = (string)$i;
$property_key = $i;
$had_implicit = true;
} elseif (count($property_branch->children) === 1) {
$property_type = self::getTypeFromTree(
$property_branch->children[0],
Expand All @@ -1441,23 +1448,35 @@ private static function getTypeFromKeyedArrayTree(
} else {
$property_key = $property_branch->value;
}
$is_list = false;
if ($is_list && (
!is_numeric($property_key)
|| ($had_optional && !$property_maybe_undefined)
|| $type === 'array'
|| $type === 'callable-array'
|| $previous_property_key != ($property_key-1)
)
) {
$is_list = false;
}
$had_explicit = true;
$previous_property_key = $property_key;

if ($property_key[0] === '\'' || $property_key[0] === '"') {
$property_key = stripslashes(substr($property_key, 1, -1));
}
} else {
throw new TypeParseTreeException(
'Missing property type'
);
}

if ($property_key[0] === '\'' || $property_key[0] === '"') {
$property_key = stripslashes(substr($property_key, 1, -1));
}

if (!$property_type instanceof Union) {
$property_type = new Union([$property_type], ['from_docblock' => $from_docblock]);
}

if ($property_maybe_undefined) {
$property_type->possibly_undefined = true;
$had_optional = true;
}

$properties[$property_key] = $property_type;
Expand All @@ -1466,6 +1485,10 @@ private static function getTypeFromKeyedArrayTree(
}
}

if ($had_explicit && $had_implicit) {
throw new TypeParseTreeException('Cannot mix explicit and implicit keys!');
}

if ($type === 'object') {
return new TObjectWithProperties($properties, [], [], $from_docblock);
}
Expand All @@ -1485,6 +1508,10 @@ private static function getTypeFromKeyedArrayTree(
throw new TypeParseTreeException('Unexpected brace character');
}

if ($type === 'list' && !$is_list) {
throw new TypeParseTreeException('A list shape cannot describe a non-list!');
}

if (!$properties) {
return new TArray([Type::getNever($from_docblock), Type::getNever($from_docblock)], $from_docblock);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/FileUpdates/TemporaryUpdateTest.php
Expand Up @@ -162,7 +162,7 @@ public function testErrorFix(
}

/**
* @return array<string,array{array<int, array<string, string>>,error_positions:array<int, array<int>>, ignored_issues?:array<string, string>, test_save?:bool, check_unused_code?: bool}>
* @return array<string,array{0: array<int, array<string, string>>,error_positions:array<int, array<int>>, ignored_issues?:array<string, string>, test_save?:bool, check_unused_code?: bool}>
*/
public function providerTestErrorFix(): array
{
Expand Down
94 changes: 94 additions & 0 deletions tests/TypeParseTest.php
Expand Up @@ -427,6 +427,100 @@ public function testTKeyedArrayNotSealed(): void
);
}

public function testTKeyedList(): void
{
$this->assertSame(
'list{int, int, string}',
(string)Type::parseString('list{int, int, string}')
);
}

public function testTKeyedListOptional(): void
{
$this->assertSame(
'list{0: int, 1?: int, 2?: string}',
(string)Type::parseString('list{0: int, 1?: int, 2?: string}')
danog marked this conversation as resolved.
Show resolved Hide resolved
);
}


public function testTKeyedArrayList(): void
{
$this->assertSame(
'list{int, int, string}',
(string)Type::parseString('array{int, int, string}')
);
}


public function testTKeyedArrayNonList(): void
{
$this->assertSame(
'array{0: int, 1: int, 2: string}',
(string)Type::parseString('array{0: int, 1: int, 2: string}')
);
}


public function testTKeyedCallableArrayNonList(): void
{
$this->assertSame(
'callable-array{0: class-string, 1: string}',
(string)Type::parseString('callable-array{0: class-string, 1: string}')
);
}


public function testTKeyedListNonList(): void
{
$this->expectExceptionMessage('A list shape cannot describe a non-list!');
Type::parseString('list{a: 0, b: 1, c: 2}');
}


public function testTKeyedListNonListOptional(): void
{
$this->expectExceptionMessage('A list shape cannot describe a non-list!');
Type::parseString('list{a: 0, b?: 1, c?: 2}');
}

public function testTKeyedListNonListOptionalWrongOrder1(): void
{
$this->expectExceptionMessage('A list shape cannot describe a non-list!');
Type::parseString('list{0?: 0, 1: 1, 2: 2}');
}

public function testTKeyedListNonListOptionalWrongOrder2(): void
{
$this->expectExceptionMessage('A list shape cannot describe a non-list!');
Type::parseString('list{0: 0, 1?: 1, 2: 2}');
}


public function testTKeyedListWrongOrder(): void
{
$this->expectExceptionMessage('A list shape cannot describe a non-list!');
Type::parseString('list{1: 1, 0: 0}');
}

public function testTKeyedListNonListKeys(): void
{
$this->expectExceptionMessage('A list shape cannot describe a non-list!');
Type::parseString('list{1: 1, 2: 2}');
}

public function testTKeyedListNoExplicitAndImplicitKeys(): void
{
$this->expectExceptionMessage('Cannot mix explicit and implicit keys!');
Type::parseString('list{0: 0, 1}');
}

public function testTKeyedArrayNoExplicitAndImplicitKeys(): void
{
$this->expectExceptionMessage('Cannot mix explicit and implicit keys!');
Type::parseString('array{0, test: 1}');
}

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