From 1142e374714d176d52f682889b4768700154a0e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Mirtes?= Date: Sat, 28 Nov 2026 13:11:24 +0000 Subject: [PATCH] Add TemplateDeclarationTypeNode for "T of Foo" in generic arguments A generic type argument may now declare a template with an upper bound, like class-string-map or array, Config[K]>. It is read into a TemplateDeclarationTypeNode holding the name and the bound, for every generic type: which types give it a meaning is up to the tool resolving the types. "T as Foo" is read the same way and printed as "T of Foo", like a @template tag. The bound is required, so class-string-map and array are read as before. Amp-Thread-ID: https://ampcode.com/threads/T-01a22165-e448-7588-8b56-bc4dd19cc3a4 --- doc/grammars/types.pp3 | 16 +++- src/Ast/Type/TemplateDeclarationTypeNode.php | 50 ++++++++++++ src/Parser/TypeParser.php | 45 +++++++++++ src/Printer/Printer.php | 4 + tests/PHPStan/Parser/TypeParserTest.php | 82 ++++++++++++++++++++ tests/PHPStan/Printer/PrinterTest.php | 24 ++++++ 6 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 src/Ast/Type/TemplateDeclarationTypeNode.php diff --git a/doc/grammars/types.pp3 b/doc/grammars/types.pp3 index a1b92aeb..67b43e6c 100644 --- a/doc/grammars/types.pp3 +++ b/doc/grammars/types.pp3 @@ -275,7 +275,21 @@ GenericArgumentTail GenericArgument : Wildcard() | Variance() Type() - | !Variance() Type() + | !Variance() TemplateDeclaration() + | !Variance() !TemplateDeclarationStart() Type() + ; + +/** + * A template declared in a generic type argument, like the "T of Foo" in + * class-string-map. Every generic type reads it; which of them give + * it a meaning is left to the tool resolving the types. + */ +TemplateDeclaration + : TemplateDeclarationStart() Type() + ; + +TemplateDeclarationStart + : Identifier() ( | ) ; Variance diff --git a/src/Ast/Type/TemplateDeclarationTypeNode.php b/src/Ast/Type/TemplateDeclarationTypeNode.php new file mode 100644 index 00000000..66b3a609 --- /dev/null +++ b/src/Ast/Type/TemplateDeclarationTypeNode.php @@ -0,0 +1,50 @@ + or the "K of key-of" in + * array, Config[K]>. + * + * The template is scoped to the generic type it is declared in. The parser reads + * it in the arguments of every generic type; which types give it a meaning is up + * to the tool resolving the types. + */ +class TemplateDeclarationTypeNode implements TypeNode +{ + + use NodeAttributes; + + public string $name; + + public TypeNode $bound; + + public function __construct(string $name, TypeNode $bound) + { + $this->name = $name; + $this->bound = $bound; + } + + public function __toString(): string + { + return "{$this->name} of {$this->bound}"; + } + + /** + * @param array $properties + */ + public static function __set_state(array $properties): self + { + $instance = new self($properties['name'], $properties['bound']); + if (isset($properties['attributes'])) { + foreach ($properties['attributes'] as $key => $value) { + $instance->setAttribute($key, $value); + } + } + return $instance; + } + +} diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index e3990653..60312093 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -507,12 +507,57 @@ public function parseGenericTypeArgument(TokenIterator $tokens): array $variance = Ast\Type\GenericTypeNode::VARIANCE_COVARIANT; } else { $variance = Ast\Type\GenericTypeNode::VARIANCE_INVARIANT; + + $templateDeclaration = $this->tryParseTemplateDeclaration($tokens); + if ($templateDeclaration !== null) { + return [$templateDeclaration, $variance]; + } } $type = $this->parse($tokens); return [$type, $variance]; } + /** + * Reads a template declared in a generic type argument, like "T of Foo". + * Returns null when the argument is not one, leaving the tokens untouched. + * + * @phpstan-impure + */ + private function tryParseTemplateDeclaration(TokenIterator $tokens): ?Ast\Type\TemplateDeclarationTypeNode + { + if (!$tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) { + return null; + } + + $startLine = $tokens->currentTokenLine(); + $startIndex = $tokens->currentTokenIndex(); + $name = $tokens->currentTokenValue(); + + $tokens->pushSavePoint(); + $tokens->next(); + if (!$tokens->tryConsumeTokenValue('of') && !$tokens->tryConsumeTokenValue('as')) { + $tokens->rollback(); + return null; + } + $tokens->dropSavePoint(); + + // The comments written before the template belong to it, not to its bound + $comments = $tokens->flushComments(); + $bound = $this->parse($tokens); + $template = $this->enrichWithAttributes( + $tokens, + new Ast\Type\TemplateDeclarationTypeNode($name, $bound), + $startLine, + $startIndex, + ); + if ($this->config->useCommentsAttributes) { + $template->setAttribute(Ast\Attribute::COMMENTS, $comments); + } + + return $template; + } + /** * @throws ParserException * @param ?callable(TokenIterator): string $parseDescription diff --git a/src/Printer/Printer.php b/src/Printer/Printer.php index 61deed7e..615d7f9c 100644 --- a/src/Printer/Printer.php +++ b/src/Printer/Printer.php @@ -63,6 +63,7 @@ use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode; use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; +use PHPStan\PhpDocParser\Ast\Type\TemplateDeclarationTypeNode; use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; @@ -502,6 +503,9 @@ private function printType(TypeNode $node): string if ($node instanceof IdentifierTypeNode) { return $node->name; } + if ($node instanceof TemplateDeclarationTypeNode) { + return $node->name . ' of ' . $this->printType($node->bound); + } if ($node instanceof NameNode) { return $node->toCodeString(); } diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 47362f8a..7ab07439 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -31,6 +31,7 @@ use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode; use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; +use PHPStan\PhpDocParser\Ast\Type\TemplateDeclarationTypeNode; use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; @@ -2564,6 +2565,87 @@ public function provideParseData(): array null, ), ], + [ + 'class-string-map', + new GenericTypeNode( + new IdentifierTypeNode('class-string-map'), + [ + new TemplateDeclarationTypeNode('T', new IdentifierTypeNode('Foo')), + new IdentifierTypeNode('T'), + ], + [ + GenericTypeNode::VARIANCE_INVARIANT, + GenericTypeNode::VARIANCE_INVARIANT, + ], + ), + ], + [ + 'class-string-map>', + new GenericTypeNode( + new IdentifierTypeNode('class-string-map'), + [ + new TemplateDeclarationTypeNode('T', new IdentifierTypeNode('Foo')), + new GenericTypeNode( + new IdentifierTypeNode('list'), + [new IdentifierTypeNode('T')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + ], + [ + GenericTypeNode::VARIANCE_INVARIANT, + GenericTypeNode::VARIANCE_INVARIANT, + ], + ), + ], + [ + 'array< + K of key-of|int, + Config[K], + >', + new GenericTypeNode( + new IdentifierTypeNode('array'), + [ + new TemplateDeclarationTypeNode('K', new UnionTypeNode([ + new GenericTypeNode( + new IdentifierTypeNode('key-of'), + [new IdentifierTypeNode('Config')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + new IdentifierTypeNode('int'), + ])), + new OffsetAccessTypeNode(new IdentifierTypeNode('Config'), new IdentifierTypeNode('K')), + ], + [ + GenericTypeNode::VARIANCE_INVARIANT, + GenericTypeNode::VARIANCE_INVARIANT, + ], + ), + ], + [ + 'Foo', + new GenericTypeNode( + new IdentifierTypeNode('Foo'), + [ + new IdentifierTypeNode('of'), + new IdentifierTypeNode('of'), + ], + [ + GenericTypeNode::VARIANCE_COVARIANT, + GenericTypeNode::VARIANCE_INVARIANT, + ], + ), + ], + [ + 'Foo', + new ParserException( + '>', + Lexer::TOKEN_CLOSE_ANGLE_BRACKET, + 8, + Lexer::TOKEN_IDENTIFIER, + null, + null, + ), + ], [ 'Foo', new ParserException( diff --git a/tests/PHPStan/Printer/PrinterTest.php b/tests/PHPStan/Printer/PrinterTest.php index f69d7e09..242965d2 100644 --- a/tests/PHPStan/Printer/PrinterTest.php +++ b/tests/PHPStan/Printer/PrinterTest.php @@ -50,6 +50,7 @@ use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; use PHPStan\PhpDocParser\Ast\Type\RelativeNameNode; +use PHPStan\PhpDocParser\Ast\Type\TemplateDeclarationTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; @@ -3036,6 +3037,29 @@ public function enterNode(Node $node) }, ]; + + yield [ + self::nowdoc(' + /** + * @var class-string-map> + */'), + self::nowdoc(' + /** + * @var class-string-map> + */'), + new class extends AbstractNodeVisitor { + + public function enterNode(Node $node) + { + if ($node instanceof TemplateDeclarationTypeNode) { + $node->bound = new IdentifierTypeNode('Bar'); + } + + return $node; + } + + }, + ]; } /**