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 e80037dd..fc6a29a5 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 @@ -13,17 +14,27 @@ class TypeAliasTagValueNode implements PhpDocTagValueNode public string $alias; + /** @var TemplateTagValueNode[] */ + public array $templateTypes; + public TypeNode $type; - public function __construct(string $alias, TypeNode $type) + /** + * @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..aa0ac2f0 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -1094,6 +1094,10 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA $alias = $tokens->currentTokenValue(); $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); + $templateTypes = $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET) + ? $this->typeParser->parseTemplateTypes($tokens) + : []; + // support phan-type/psalm-type syntax $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL); @@ -1114,12 +1118,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/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 615d7f9c..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' => ', ', ]; @@ -420,8 +421,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..b13bd637 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -5185,6 +5185,234 @@ public function provideTypeAliasTagsData(): Iterator ), ]), ]; + + 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 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'), ''), + ], + ), + ), + ]), + ]; + + 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 @@ -7804,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)