Skip to content

Commit 44500ca

Browse files
Andreas Frömerondrejmirtes
Andreas Frömer
authored andcommitted
Add support for trailing comma
1 parent 9efbbb6 commit 44500ca

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

Diff for: src/Parser/TypeParser.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,15 @@ public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode
167167
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
168168
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
169169
$genericTypes = [$this->parse($tokens)];
170-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
170+
171+
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
171172

172173
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
173174
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
175+
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
176+
// trailing comma case
177+
return new Ast\Type\GenericTypeNode($baseType, $genericTypes);
178+
}
174179
$genericTypes[] = $this->parse($tokens);
175180
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
176181
}

Diff for: tests/PHPStan/Parser/PhpDocParserTest.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -3079,29 +3079,6 @@ public function provideRealWorldExampleData(): \Iterator
30793079
]),
30803080
];
30813081

3082-
// yield [
3083-
// 'multiline generic types - trailing comma',
3084-
// '/**' . PHP_EOL .
3085-
// ' * @implements Foo<' . PHP_EOL .
3086-
// ' * A,' . PHP_EOL .
3087-
// ' * >' . PHP_EOL .
3088-
// ' */',
3089-
// new PhpDocNode([
3090-
// new PhpDocTagNode(
3091-
// '@implements',
3092-
// new ImplementsTagValueNode(
3093-
// new GenericTypeNode(
3094-
// new IdentifierTypeNode('Foo'),
3095-
// [
3096-
// new IdentifierTypeNode('A'),
3097-
// ]
3098-
// ),
3099-
// ''
3100-
// )
3101-
// ),
3102-
// ]),
3103-
// ];
3104-
31053082
yield [
31063083
'multiline generic types - leading comma',
31073084
'/**' . PHP_EOL .
@@ -3126,6 +3103,31 @@ public function provideRealWorldExampleData(): \Iterator
31263103
),
31273104
]),
31283105
];
3106+
3107+
yield [
3108+
'multiline generic types - traling comma',
3109+
'/**' . PHP_EOL .
3110+
' * @implements Foo<' . PHP_EOL .
3111+
' * A,' . PHP_EOL .
3112+
' * B,' . PHP_EOL .
3113+
' * >' . PHP_EOL .
3114+
' */',
3115+
new PhpDocNode([
3116+
new PhpDocTagNode(
3117+
'@implements',
3118+
new ImplementsTagValueNode(
3119+
new GenericTypeNode(
3120+
new IdentifierTypeNode('Foo'),
3121+
[
3122+
new IdentifierTypeNode('A'),
3123+
new IdentifierTypeNode('B'),
3124+
]
3125+
),
3126+
''
3127+
)
3128+
),
3129+
]),
3130+
];
31293131
}
31303132

31313133
public function dataParseTagValue(): array

Diff for: tests/PHPStan/Parser/TypeParserTest.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ public function provideParseData(): array
899899
new ConstTypeNode(new ConstFetchNode('QueueAttributeName', '*')),
900900
]),
901901
],
902-
[
902+
[
903903
'array<' . PHP_EOL .
904904
' Foo' . PHP_EOL .
905905
'>',
@@ -955,6 +955,26 @@ public function provideParseData(): array
955955
]
956956
),
957957
],
958+
[
959+
'array<' . PHP_EOL .
960+
' Foo,' . PHP_EOL .
961+
' array<' . PHP_EOL .
962+
' Bar,' . PHP_EOL .
963+
' >' . PHP_EOL .
964+
'>',
965+
new GenericTypeNode(
966+
new IdentifierTypeNode('array'),
967+
[
968+
new IdentifierTypeNode('Foo'),
969+
new GenericTypeNode(
970+
new IdentifierTypeNode('array'),
971+
[
972+
new IdentifierTypeNode('Bar'),
973+
]
974+
),
975+
]
976+
),
977+
],
958978
];
959979
}
960980

0 commit comments

Comments
 (0)