From 381556060ad5e7c4b6a8b197eb11cf30e42ae5bf Mon Sep 17 00:00:00 2001 From: shmax Date: Thu, 2 Apr 2026 08:26:19 -0700 Subject: [PATCH 1/4] support for generic params on alias types --- src/Ast/PhpDoc/TypeAliasTagValueNode.php | 17 +++- src/Parser/PhpDocParser.php | 18 +++- src/Printer/Printer.php | 5 +- .../Ast/ToString/PhpDocToStringTest.php | 4 + tests/PHPStan/Parser/PhpDocParserTest.php | 87 +++++++++++++++++-- 5 files changed, 119 insertions(+), 12 deletions(-) diff --git a/src/Ast/PhpDoc/TypeAliasTagValueNode.php b/src/Ast/PhpDoc/TypeAliasTagValueNode.php index e80037dd..b4e11ed5 100644 --- a/src/Ast/PhpDoc/TypeAliasTagValueNode.php +++ b/src/Ast/PhpDoc/TypeAliasTagValueNode.php @@ -4,6 +4,7 @@ use PHPStan\PhpDocParser\Ast\NodeAttributes; use PHPStan\PhpDocParser\Ast\Type\TypeNode; +use function implode; use function trim; class TypeAliasTagValueNode implements PhpDocTagValueNode @@ -15,15 +16,25 @@ class TypeAliasTagValueNode implements PhpDocTagValueNode public TypeNode $type; - public function __construct(string $alias, TypeNode $type) + /** @var TemplateTagValueNode[] */ + public array $templateTypes; + + /** + * @param TemplateTagValueNode[] $templateTypes + */ + public function __construct(string $alias, TypeNode $type, array $templateTypes = []) { $this->alias = $alias; $this->type = $type; + $this->templateTypes = $templateTypes; } public function __toString(): string { - return trim("{$this->alias} {$this->type}"); + $templateTypes = $this->templateTypes !== [] + ? '<' . implode(', ', $this->templateTypes) . '>' + : ''; + return trim("{$this->alias}{$templateTypes} {$this->type}"); } /** @@ -31,7 +42,7 @@ public function __toString(): string */ public static function __set_state(array $properties): self { - $instance = new self($properties['alias'], $properties['type']); + $instance = new self($properties['alias'], $properties['type'], $properties['templateTypes'] ?? []); if (isset($properties['attributes'])) { foreach ($properties['attributes'] as $key => $value) { $instance->setAttribute($key, $value); diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index e0c0fa16..e44a9e3b 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -1094,6 +1094,21 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA $alias = $tokens->currentTokenValue(); $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); + $templateTypes = []; + if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) { + do { + $startLine = $tokens->currentTokenLine(); + $startIndex = $tokens->currentTokenIndex(); + $templateTypes[] = $this->enrichWithAttributes( + $tokens, + $this->typeParser->parseTemplateTagValue($tokens), + $startLine, + $startIndex, + ); + } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)); + $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET); + } + // support phan-type/psalm-type syntax $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL); @@ -1114,12 +1129,13 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA } } - return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type); + return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type, $templateTypes); } catch (ParserException $e) { $this->parseOptionalDescription($tokens, false); return new Ast\PhpDoc\TypeAliasTagValueNode( $alias, $this->enrichWithAttributes($tokens, new Ast\Type\InvalidTypeNode($e), $startLine, $startIndex), + $templateTypes, ); } } diff --git a/src/Printer/Printer.php b/src/Printer/Printer.php index 615d7f9c..249e41b0 100644 --- a/src/Printer/Printer.php +++ b/src/Printer/Printer.php @@ -420,8 +420,11 @@ private function printTagValue(PhpDocTagValueNode $node): string ); } if ($node instanceof TypeAliasTagValueNode) { + $templateTypes = $node->templateTypes !== [] + ? '<' . implode(', ', array_map(fn (TemplateTagValueNode $templateNode): string => $this->print($templateNode), $node->templateTypes)) . '>' + : ''; $type = $this->printType($node->type); - return trim("{$node->alias} {$type}"); + return trim("{$node->alias}{$templateTypes} {$type}"); } if ($node instanceof UsesTagValueNode) { $type = $this->printType($node->type); diff --git a/tests/PHPStan/Ast/ToString/PhpDocToStringTest.php b/tests/PHPStan/Ast/ToString/PhpDocToStringTest.php index 47a25db3..dbbc9d5a 100644 --- a/tests/PHPStan/Ast/ToString/PhpDocToStringTest.php +++ b/tests/PHPStan/Ast/ToString/PhpDocToStringTest.php @@ -229,6 +229,10 @@ public static function provideClassCases(): Generator yield from [ ['Foo array', new TypeAliasTagValueNode('Foo', $arrayOfStrings)], + ['Wrapper T', new TypeAliasTagValueNode('Wrapper', new IdentifierTypeNode('T'), [new TemplateTagValueNode('T', null, '')])], + ['Pair TFirst', new TypeAliasTagValueNode('Pair', new IdentifierTypeNode('TFirst'), [new TemplateTagValueNode('TFirst', null, ''), new TemplateTagValueNode('TSecond', null, '')])], + ['Collection array', new TypeAliasTagValueNode('Collection', $arrayOfStrings, [new TemplateTagValueNode('T', new IdentifierTypeNode('object'), '')])], + ['WithDefault T', new TypeAliasTagValueNode('WithDefault', new IdentifierTypeNode('T'), [new TemplateTagValueNode('T', null, '', new IdentifierTypeNode('string'))])], ['Test from Foo\Bar', new TypeAliasImportTagValueNode('Test', $bar, null)], ['Test from Foo\Bar as Foo', new TypeAliasImportTagValueNode('Test', $bar, 'Foo')], ]; diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index b9b59ba6..55b2d15a 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -5178,16 +5178,89 @@ public function provideTypeAliasTagsData(): Iterator Lexer::TOKEN_CLOSE_PHPDOC, 18, Lexer::TOKEN_IDENTIFIER, - null, - 1, - ), + null, + 1, ), ), - ]), - ]; - } + ), + ]), + ]; + + yield [ + 'OK with one template type', + '/** @phpstan-type Wrapper T */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Wrapper', + new IdentifierTypeNode('T'), + [ + new TemplateTagValueNode('T', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with two template types', + '/** @phpstan-type Pair TFirst */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Pair', + new IdentifierTypeNode('TFirst'), + [ + new TemplateTagValueNode('TFirst', null, ''), + new TemplateTagValueNode('TSecond', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with bounded template type', + '/** @phpstan-type Collection list */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Collection', + new GenericTypeNode( + new IdentifierTypeNode('list'), + [new IdentifierTypeNode('T')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + [ + new TemplateTagValueNode('T', new IdentifierTypeNode('object'), ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with default template type', + '/** @phpstan-type WithDefault T */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'WithDefault', + new IdentifierTypeNode('T'), + [ + new TemplateTagValueNode('T', null, '', new IdentifierTypeNode('string')), + ], + ), + ), + ]), + ]; +} - public function provideTypeAliasImportTagsData(): Iterator +public function provideTypeAliasImportTagsData(): Iterator { yield [ 'OK', From a97e7658204d6cbb9c3b28a73b76b96e1fc84a65 Mon Sep 17 00:00:00 2001 From: shmax Date: Thu, 2 Apr 2026 08:47:52 -0700 Subject: [PATCH 2/4] linting --- tests/PHPStan/Parser/PhpDocParserTest.php | 160 +++++++++++----------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index 55b2d15a..b2c1685b 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -5178,89 +5178,89 @@ public function provideTypeAliasTagsData(): Iterator Lexer::TOKEN_CLOSE_PHPDOC, 18, Lexer::TOKEN_IDENTIFIER, - null, - 1, + null, + 1, + ), ), ), - ), - ]), - ]; - - yield [ - 'OK with one template type', - '/** @phpstan-type Wrapper T */', - new PhpDocNode([ - new PhpDocTagNode( - '@phpstan-type', - new TypeAliasTagValueNode( - 'Wrapper', - new IdentifierTypeNode('T'), - [ - new TemplateTagValueNode('T', null, ''), - ], - ), - ), - ]), - ]; - - yield [ - 'OK with two template types', - '/** @phpstan-type Pair TFirst */', - new PhpDocNode([ - new PhpDocTagNode( - '@phpstan-type', - new TypeAliasTagValueNode( - 'Pair', - new IdentifierTypeNode('TFirst'), - [ - new TemplateTagValueNode('TFirst', null, ''), - new TemplateTagValueNode('TSecond', null, ''), - ], - ), - ), - ]), - ]; - - yield [ - 'OK with bounded template type', - '/** @phpstan-type Collection list */', - new PhpDocNode([ - new PhpDocTagNode( - '@phpstan-type', - new TypeAliasTagValueNode( - 'Collection', - new GenericTypeNode( - new IdentifierTypeNode('list'), - [new IdentifierTypeNode('T')], - [GenericTypeNode::VARIANCE_INVARIANT], - ), - [ - new TemplateTagValueNode('T', new IdentifierTypeNode('object'), ''), - ], - ), - ), - ]), - ]; - - yield [ - 'OK with default template type', - '/** @phpstan-type WithDefault T */', - new PhpDocNode([ - new PhpDocTagNode( - '@phpstan-type', - new TypeAliasTagValueNode( - 'WithDefault', - new IdentifierTypeNode('T'), - [ - new TemplateTagValueNode('T', null, '', new IdentifierTypeNode('string')), - ], - ), - ), - ]), - ]; -} + ]), + ]; + + yield [ + 'OK with one template type', + '/** @phpstan-type Wrapper T */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Wrapper', + new IdentifierTypeNode('T'), + [ + new TemplateTagValueNode('T', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with two template types', + '/** @phpstan-type Pair TFirst */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Pair', + new IdentifierTypeNode('TFirst'), + [ + new TemplateTagValueNode('TFirst', null, ''), + new TemplateTagValueNode('TSecond', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with bounded template type', + '/** @phpstan-type Collection list */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Collection', + new GenericTypeNode( + new IdentifierTypeNode('list'), + [new IdentifierTypeNode('T')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + [ + new TemplateTagValueNode('T', new IdentifierTypeNode('object'), ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with default template type', + '/** @phpstan-type WithDefault T */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'WithDefault', + new IdentifierTypeNode('T'), + [ + new TemplateTagValueNode('T', null, '', new IdentifierTypeNode('string')), + ], + ), + ), + ]), + ]; + } -public function provideTypeAliasImportTagsData(): Iterator + public function provideTypeAliasImportTagsData(): Iterator { yield [ 'OK', From 636aff8941a9df2279f2d104627b3955034d69d3 Mon Sep 17 00:00:00 2001 From: shmax Date: Thu, 2 Apr 2026 08:56:15 -0700 Subject: [PATCH 3/4] allow multiline --- src/Parser/PhpDocParser.php | 21 +++++++++- tests/PHPStan/Parser/PhpDocParserTest.php | 51 +++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index e44a9e3b..6c3a3cde 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -1096,6 +1096,11 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA $templateTypes = []; if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) { + // Skip whitespace and newlines after opening bracket + while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) { + $tokens->next(); + } + do { $startLine = $tokens->currentTokenLine(); $startIndex = $tokens->currentTokenIndex(); @@ -1105,7 +1110,21 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA $startLine, $startIndex, ); - } while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)); + + // Skip whitespace and newlines after template type + while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) { + $tokens->next(); + } + + if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) { + break; + } + + // Skip whitespace and newlines after comma + while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) { + $tokens->next(); + } + } while (true); $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET); } diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index b2c1685b..25b453b3 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -5258,6 +5258,57 @@ public function provideTypeAliasTagsData(): Iterator ), ]), ]; + + yield [ + 'OK multiline with two template types', + '/** + * @phpstan-type Widget< + * TFoo, + * TBar + * > array{foo: TFoo, bar: TBar} + */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Widget', + ArrayShapeNode::createSealed([ + new ArrayShapeItemNode(new IdentifierTypeNode('foo'), false, new IdentifierTypeNode('TFoo')), + new ArrayShapeItemNode(new IdentifierTypeNode('bar'), false, new IdentifierTypeNode('TBar')), + ]), + [ + new TemplateTagValueNode('TFoo', null, ''), + new TemplateTagValueNode('TBar', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK multiline with bounded template', + '/** + * @phpstan-type Collection< + * T of object + * > list + */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Collection', + new GenericTypeNode( + new IdentifierTypeNode('list'), + [new IdentifierTypeNode('T')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + [ + new TemplateTagValueNode('T', new IdentifierTypeNode('object'), ''), + ], + ), + ), + ]), + ]; } public function provideTypeAliasImportTagsData(): Iterator From da88cc2ec33eead9e7152172571898aaa5fa2064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Mirtes?= Date: Tue, 1 Dec 2026 15:31:30 +0000 Subject: [PATCH 4/4] Read the templates of a type alias like the templates of a callable The templates are read by TypeParser::parseTemplateTypes(), which allows comments and a trailing comma, and the grammar describes them for the fuzzer. TemplateTypes is declared before the type so that format-preserving printing visits them in token order. Amp-Thread-ID: https://ampcode.com/threads/T-01a23235-ed63-7495-8268-50e3db8a9e95 --- doc/grammars/phpdoc-block.pp3 | 15 ++- src/Ast/PhpDoc/TypeAliasTagValueNode.php | 4 +- src/Parser/PhpDocParser.php | 36 +------ src/Parser/TypeParser.php | 7 +- src/Printer/Printer.php | 1 + tests/PHPStan/Parser/PhpDocParserTest.php | 121 ++++++++++++++++++++++ tests/PHPStan/Printer/PrinterTest.php | 87 ++++++++++++++++ 7 files changed, 233 insertions(+), 38 deletions(-) diff --git a/doc/grammars/phpdoc-block.pp3 b/doc/grammars/phpdoc-block.pp3 index bbb74384..36ca9f83 100644 --- a/doc/grammars/phpdoc-block.pp3 +++ b/doc/grammars/phpdoc-block.pp3 @@ -263,12 +263,25 @@ ExtendsTagValue /** * A type written under a name of its own, like: * - @phpstan-type Foo = int|string + * - @phpstan-type Pair array{TFirst, TSecond} * * Nothing may be written after the type but the end of its line, so that a * description is never read as part of the type it follows. */ TypeAliasTagValue - : Identifier() ? Type() &EndOfLine() + : Identifier() ( TypeAliasTemplates() | !AngleBracketOpen() ) ? Type() &EndOfLine() + ; + +/** + * The templates a type alias is declared with, read by the very method reading + * the templates of a callable, so they are written the same way: one per line + * if need be, with comments and a trailing comma. + * + * Any "<" opens them, even one opening what looks like an HTML tag. + */ +TypeAliasTemplates + : AngleBracketOpen() Trivia() CallableTemplateArgument() Trivia() + CallableTemplateArgumentTail()* ; /** diff --git a/src/Ast/PhpDoc/TypeAliasTagValueNode.php b/src/Ast/PhpDoc/TypeAliasTagValueNode.php index b4e11ed5..fc6a29a5 100644 --- a/src/Ast/PhpDoc/TypeAliasTagValueNode.php +++ b/src/Ast/PhpDoc/TypeAliasTagValueNode.php @@ -14,11 +14,11 @@ class TypeAliasTagValueNode implements PhpDocTagValueNode public string $alias; - public TypeNode $type; - /** @var TemplateTagValueNode[] */ public array $templateTypes; + public TypeNode $type; + /** * @param TemplateTagValueNode[] $templateTypes */ diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index 6c3a3cde..aa0ac2f0 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -1094,39 +1094,9 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA $alias = $tokens->currentTokenValue(); $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); - $templateTypes = []; - if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) { - // Skip whitespace and newlines after opening bracket - while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) { - $tokens->next(); - } - - do { - $startLine = $tokens->currentTokenLine(); - $startIndex = $tokens->currentTokenIndex(); - $templateTypes[] = $this->enrichWithAttributes( - $tokens, - $this->typeParser->parseTemplateTagValue($tokens), - $startLine, - $startIndex, - ); - - // Skip whitespace and newlines after template type - while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) { - $tokens->next(); - } - - if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) { - break; - } - - // Skip whitespace and newlines after comma - while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) { - $tokens->next(); - } - } while (true); - $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET); - } + $templateTypes = $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET) + ? $this->typeParser->parseTemplateTypes($tokens) + : []; // support phan-type/psalm-type syntax $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL); diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 5ab82d80..d882b16f 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -603,7 +603,7 @@ public function parseTemplateTagValue( private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier, bool $hasTemplate): Ast\Type\TypeNode { $templates = $hasTemplate - ? $this->parseCallableTemplates($tokens) + ? $this->parseTemplateTypes($tokens) : []; $tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES); @@ -634,11 +634,14 @@ private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNod } /** + * Parses a list of templates in angle brackets, like the "" + * of a callable type or of a type alias. + * * @return Ast\PhpDoc\TemplateTagValueNode[] * * @phpstan-impure */ - private function parseCallableTemplates(TokenIterator $tokens): array + public function parseTemplateTypes(TokenIterator $tokens): array { $tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET); diff --git a/src/Printer/Printer.php b/src/Printer/Printer.php index 249e41b0..0eebed3d 100644 --- a/src/Printer/Printer.php +++ b/src/Printer/Printer.php @@ -125,6 +125,7 @@ final class Printer GenericTypeNode::class . '->genericTypes' => ', ', ConstExprArrayNode::class . '->items' => ', ', MethodTagValueNode::class . '->parameters' => ', ', + TypeAliasTagValueNode::class . '->templateTypes' => ', ', DoctrineArray::class . '->items' => ', ', DoctrineAnnotation::class . '->arguments' => ', ', ]; diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index 25b453b3..b13bd637 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -5309,6 +5309,110 @@ public function provideTypeAliasTagsData(): Iterator ), ]), ]; + + yield [ + 'OK with template types and psalm-type syntax', + '/** @psalm-type Foo = list */', + new PhpDocNode([ + new PhpDocTagNode( + '@psalm-type', + new TypeAliasTagValueNode( + 'Foo', + new GenericTypeNode( + new IdentifierTypeNode('list'), + [new IdentifierTypeNode('T')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + [ + new TemplateTagValueNode('T', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK with template types and a trailing comma', + '/** @phpstan-type Pair TFirst */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Pair', + new IdentifierTypeNode('TFirst'), + [ + new TemplateTagValueNode('TFirst', null, ''), + new TemplateTagValueNode('TSecond', null, ''), + ], + ), + ), + ]), + ]; + + yield [ + 'OK multiline with comments and a trailing comma', + '/** + * @phpstan-type Pair< + * TFirst, // the first one + * TSecond of object = stdClass, // the second one + * > TFirst + */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new TypeAliasTagValueNode( + 'Pair', + new IdentifierTypeNode('TFirst'), + [ + new TemplateTagValueNode('TFirst', null, ''), + new TemplateTagValueNode('TSecond', new IdentifierTypeNode('object'), '', new IdentifierTypeNode('stdClass')), + ], + ), + ), + ]), + ]; + + yield [ + 'invalid without closing angle bracket', + '/** @phpstan-type Foo T */', + new PhpDocNode([ + new PhpDocTagNode( + '@phpstan-type', + new InvalidTagValueNode( + 'Foo<> T', + new ParserException( + '>', + Lexer::TOKEN_CLOSE_ANGLE_BRACKET, + 22, + Lexer::TOKEN_IDENTIFIER, + null, + 1, + ), + ), + ), + ]), + ]; } public function provideTypeAliasImportTagsData(): Iterator @@ -7928,6 +8032,23 @@ public function dataDeepNodesLinesAndIndexes(): iterable [1, 1, 3, 1], // GenericTagValueNode is empty so start index is higher than end index ], ]; + yield [ + '/**' . PHP_EOL . + ' * @phpstan-type Pair<' . PHP_EOL . + ' * TFirst,' . PHP_EOL . + ' * TSecond of object' . PHP_EOL . + ' * > TFirst' . PHP_EOL . + ' */', + [ + [1, 6, 0, 22], // PhpDocNode + [2, 5, 2, 20], // PhpDocTagNode + [2, 5, 4, 20], // TypeAliasTagValueNode + [3, 3, 8, 8], // TemplateTagValueNode TFirst + [4, 4, 12, 16], // TemplateTagValueNode TSecond + [4, 4, 16, 16], // IdentifierTypeNode object + [5, 5, 20, 20], // IdentifierTypeNode TFirst + ], + ]; } /** diff --git a/tests/PHPStan/Printer/PrinterTest.php b/tests/PHPStan/Printer/PrinterTest.php index d5f66302..414a4dce 100644 --- a/tests/PHPStan/Printer/PrinterTest.php +++ b/tests/PHPStan/Printer/PrinterTest.php @@ -33,6 +33,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode; +use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode; use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode; use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; use PHPStan\PhpDocParser\Ast\Type\ArrayShapeUnsealedTypeNode; @@ -1663,6 +1664,92 @@ public function enterNode(Node $node) $removeImportAs, ]; + $addTypeAliasTemplateType = new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof TypeAliasTagValueNode) { + $node->templateTypes[] = new TemplateTagValueNode( + 'T', + new IdentifierTypeNode('int'), + '', + ); + } + + return $node; + } + + }; + + yield [ + '/** @phpstan-type Foo array{int} */', + '/** @phpstan-type Foo array{int} */', + $addTypeAliasTemplateType, + ]; + + yield [ + '/** @phpstan-type Foo array{U} */', + '/** @phpstan-type Foo array{U} */', + $addTypeAliasTemplateType, + ]; + + yield [ + self::nowdoc(' + /** + * @phpstan-type Foo< + * U, + * V, + * > array{U, V} + */'), + self::nowdoc(' + /** + * @phpstan-type Foo< + * U, + * V, + * T of int, + * > array{U, V} + */'), + $addTypeAliasTemplateType, + ]; + + $removeTypeAliasTemplateType = new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof TypeAliasTagValueNode) { + array_pop($node->templateTypes); + } + + return $node; + } + + }; + + yield [ + '/** @phpstan-type Foo array{T} */', + '/** @phpstan-type Foo array{T} */', + $removeTypeAliasTemplateType, + ]; + + $changeTypeAliasTemplateBound = new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof TypeAliasTagValueNode) { + $node->templateTypes[1]->bound = new IdentifierTypeNode('object'); + } + + return $node; + } + + }; + + yield [ + '/** @phpstan-type Foo array{T, U} */', + '/** @phpstan-type Foo array{T, U} */', + $changeTypeAliasTemplateBound, + ]; + $addMethodTemplateType = new class extends AbstractNodeVisitor { public function enterNode(Node $node)