Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions doc/grammars/types.pp3
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,17 @@ ArrayShapeUnsealed
* What the keys and the values of whatever is left over are written as.
*/
ArrayShapeUnsealedType
: AngleBracketOpen() Trivia() Type() Trivia()
( <T_COMMA> Trivia() Type() Trivia() )? <T_CLOSE_ANGLE_BRACKET>
: AngleBracketOpen() Trivia() ArrayShapeUnsealedArguments() <T_CLOSE_ANGLE_BRACKET>
;

/**
* The key may declare a template the value is written with, as in
* ...<K of key-of<Config>, Config[K]>; a declared template is always a key, so
* the value has to follow it.
*/
ArrayShapeUnsealedArguments
: TemplateDeclaration() Trivia() <T_COMMA> Trivia() Type() Trivia()
| !TemplateDeclarationStart() Type() Trivia() ( <T_COMMA> Trivia() Type() Trivia() )?
;

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1060,11 +1060,21 @@ private function parseArrayShapeUnsealedType(TokenIterator $tokens): Ast\Type\Ar
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
$tokens->skipNewLineTokensAndConsumeComments();

$valueType = $this->parse($tokens);
// a template may be declared by the key, as in ...<K of key-of<Config>, Config[K]>,
// so a declaration has to be followed by the value type
$templateDeclaration = $this->tryParseTemplateDeclaration($tokens);
$valueType = $templateDeclaration ?? $this->parse($tokens);
$tokens->skipNewLineTokensAndConsumeComments();

$keyType = null;
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
if ($templateDeclaration !== null) {
$tokens->consumeTokenType(Lexer::TOKEN_COMMA);
$tokens->skipNewLineTokensAndConsumeComments();

$keyType = $valueType;
$valueType = $this->parse($tokens);
$tokens->skipNewLineTokensAndConsumeComments();
} elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$tokens->skipNewLineTokensAndConsumeComments();

$keyType = $valueType;
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,52 @@ public function provideParseData(): array
ArrayShapeNode::KIND_NON_EMPTY_LIST,
),
],
[
'array{a: int, ...<K of key-of<Config>|int, Config[K]>}',
ArrayShapeNode::createUnsealed(
[
new ArrayShapeItemNode(
new IdentifierTypeNode('a'),
false,
new IdentifierTypeNode('int'),
),
],
new ArrayShapeUnsealedTypeNode(
new OffsetAccessTypeNode(new IdentifierTypeNode('Config'), new IdentifierTypeNode('K')),
new TemplateDeclarationTypeNode('K', new UnionTypeNode([
new GenericTypeNode(
new IdentifierTypeNode('key-of'),
[new IdentifierTypeNode('Config')],
[GenericTypeNode::VARIANCE_INVARIANT],
),
new IdentifierTypeNode('int'),
])),
),
ArrayShapeNode::KIND_ARRAY,
),
],
[
'array{...<K of int>}',
new ParserException(
'>',
Lexer::TOKEN_CLOSE_ANGLE_BRACKET,
18,
Lexer::TOKEN_COMMA,
null,
null,
),
],
[
'array{...<of, string>}',
ArrayShapeNode::createUnsealed(
[],
new ArrayShapeUnsealedTypeNode(
new IdentifierTypeNode('string'),
new IdentifierTypeNode('of'),
),
ArrayShapeNode::KIND_ARRAY,
),
],
[
'array{...<string>}',
ArrayShapeNode::createUnsealed(
Expand Down
Loading