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

Class consts in array shapes #10678

Merged
merged 1 commit into from
Feb 9, 2024
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
9 changes: 0 additions & 9 deletions src/Psalm/Internal/Type/ParseTreeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,6 @@ private function handleValue(array $type_token): void
case '::':
$nexter_token = $this->t + 2 < $this->type_token_count ? $this->type_tokens[$this->t + 2] : null;

if ($this->current_leaf instanceof ParseTree\KeyedArrayTree
&& $nexter_token
&& strtolower($nexter_token[0]) !== 'class'
) {
throw new TypeParseTreeException(
':: in array key is only allowed for ::class',
);
}

if (!$nexter_token
|| (!preg_match('/^([a-zA-Z_][a-zA-Z_0-9]*\*?|\*)$/', $nexter_token[0])
&& strtolower($nexter_token[0]) !== 'class')
Expand Down
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,12 @@ private static function getTypeFromKeyedArrayTree(
if ($const_name === 'class') {
$property_key = $fq_classlike_name;
$class_string = true;
} else {
} elseif ($property_branch->value[0] === '"' || $property_branch->value[0] === "'") {
$property_key = $property_branch->value;
} else {
throw new TypeParseTreeException(
':: in array key is only allowed for ::class',
weirdan marked this conversation as resolved.
Show resolved Hide resolved
);
}
} else {
$property_key = $property_branch->value;
Expand Down
5 changes: 5 additions & 0 deletions tests/TypeParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ public function testTKeyedArrayWithQuotedKeys(): void
$this->assertSame('array{\'\\"\': int, \'\\\'\': string}', (string) Type::parseString('array{"\\"": int, "\\\'": string}'));
}

public function testTKeyedArrayWithClassConstantValueType(): void
{
$this->assertSame('list{A::X|A::Y, B::X}', (string) Type::parseString('list{A::X|A::Y, B::X}'));
}

public function testTKeyedArrayWithClassConstantKey(): void
{
$this->expectException(TypeParseTreeException::class);
Expand Down