Skip to content

Commit

Permalink
Support parsing constants in shapes
Browse files Browse the repository at this point in the history
Fixes #10669
  • Loading branch information
weirdan committed Feb 8, 2024
1 parent 4a5dbca commit 2f0b85f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
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',
);
}
} 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

0 comments on commit 2f0b85f

Please sign in to comment.