Skip to content

Commit da88cc2

Browse files
committed
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
1 parent 636aff8 commit da88cc2

7 files changed

Lines changed: 233 additions & 38 deletions

File tree

‎doc/grammars/phpdoc-block.pp3‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,25 @@ ExtendsTagValue
263263
/**
264264
* A type written under a name of its own, like:
265265
* - @phpstan-type Foo = int|string
266+
* - @phpstan-type Pair<TFirst, TSecond of object> array{TFirst, TSecond}
266267
*
267268
* Nothing may be written after the type but the end of its line, so that a
268269
* description is never read as part of the type it follows.
269270
*/
270271
TypeAliasTagValue
271-
: Identifier() <T_EQUAL>? Type() &EndOfLine()
272+
: Identifier() ( TypeAliasTemplates() | !AngleBracketOpen() ) <T_EQUAL>? Type() &EndOfLine()
273+
;
274+
275+
/**
276+
* The templates a type alias is declared with, read by the very method reading
277+
* the templates of a callable, so they are written the same way: one per line
278+
* if need be, with comments and a trailing comma.
279+
*
280+
* Any "<" opens them, even one opening what looks like an HTML tag.
281+
*/
282+
TypeAliasTemplates
283+
: AngleBracketOpen() Trivia() CallableTemplateArgument() Trivia()
284+
CallableTemplateArgumentTail()* <T_CLOSE_ANGLE_BRACKET>
272285
;
273286

274287
/**

‎src/Ast/PhpDoc/TypeAliasTagValueNode.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class TypeAliasTagValueNode implements PhpDocTagValueNode
1414

1515
public string $alias;
1616

17-
public TypeNode $type;
18-
1917
/** @var TemplateTagValueNode[] */
2018
public array $templateTypes;
2119

20+
public TypeNode $type;
21+
2222
/**
2323
* @param TemplateTagValueNode[] $templateTypes
2424
*/

‎src/Parser/PhpDocParser.php‎

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,39 +1094,9 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA
10941094
$alias = $tokens->currentTokenValue();
10951095
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
10961096

1097-
$templateTypes = [];
1098-
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
1099-
// Skip whitespace and newlines after opening bracket
1100-
while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) {
1101-
$tokens->next();
1102-
}
1103-
1104-
do {
1105-
$startLine = $tokens->currentTokenLine();
1106-
$startIndex = $tokens->currentTokenIndex();
1107-
$templateTypes[] = $this->enrichWithAttributes(
1108-
$tokens,
1109-
$this->typeParser->parseTemplateTagValue($tokens),
1110-
$startLine,
1111-
$startIndex,
1112-
);
1113-
1114-
// Skip whitespace and newlines after template type
1115-
while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) {
1116-
$tokens->next();
1117-
}
1118-
1119-
if (!$tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
1120-
break;
1121-
}
1122-
1123-
// Skip whitespace and newlines after comma
1124-
while ($tokens->isCurrentTokenType(Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_PHPDOC_EOL)) {
1125-
$tokens->next();
1126-
}
1127-
} while (true);
1128-
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
1129-
}
1097+
$templateTypes = $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)
1098+
? $this->typeParser->parseTemplateTypes($tokens)
1099+
: [];
11301100

11311101
// support phan-type/psalm-type syntax
11321102
$tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL);

‎src/Parser/TypeParser.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public function parseTemplateTagValue(
603603
private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier, bool $hasTemplate): Ast\Type\TypeNode
604604
{
605605
$templates = $hasTemplate
606-
? $this->parseCallableTemplates($tokens)
606+
? $this->parseTemplateTypes($tokens)
607607
: [];
608608

609609
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
@@ -634,11 +634,14 @@ private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNod
634634
}
635635

636636
/**
637+
* Parses a list of templates in angle brackets, like the "<T, U of Foo>"
638+
* of a callable type or of a type alias.
639+
*
637640
* @return Ast\PhpDoc\TemplateTagValueNode[]
638641
*
639642
* @phpstan-impure
640643
*/
641-
private function parseCallableTemplates(TokenIterator $tokens): array
644+
public function parseTemplateTypes(TokenIterator $tokens): array
642645
{
643646
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
644647

‎src/Printer/Printer.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ final class Printer
125125
GenericTypeNode::class . '->genericTypes' => ', ',
126126
ConstExprArrayNode::class . '->items' => ', ',
127127
MethodTagValueNode::class . '->parameters' => ', ',
128+
TypeAliasTagValueNode::class . '->templateTypes' => ', ',
128129
DoctrineArray::class . '->items' => ', ',
129130
DoctrineAnnotation::class . '->arguments' => ', ',
130131
];

‎tests/PHPStan/Parser/PhpDocParserTest.php‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5309,6 +5309,110 @@ public function provideTypeAliasTagsData(): Iterator
53095309
),
53105310
]),
53115311
];
5312+
5313+
yield [
5314+
'OK with template types and psalm-type syntax',
5315+
'/** @psalm-type Foo<T> = list<T> */',
5316+
new PhpDocNode([
5317+
new PhpDocTagNode(
5318+
'@psalm-type',
5319+
new TypeAliasTagValueNode(
5320+
'Foo',
5321+
new GenericTypeNode(
5322+
new IdentifierTypeNode('list'),
5323+
[new IdentifierTypeNode('T')],
5324+
[GenericTypeNode::VARIANCE_INVARIANT],
5325+
),
5326+
[
5327+
new TemplateTagValueNode('T', null, ''),
5328+
],
5329+
),
5330+
),
5331+
]),
5332+
];
5333+
5334+
yield [
5335+
'OK with template types and a trailing comma',
5336+
'/** @phpstan-type Pair<TFirst, TSecond,> TFirst */',
5337+
new PhpDocNode([
5338+
new PhpDocTagNode(
5339+
'@phpstan-type',
5340+
new TypeAliasTagValueNode(
5341+
'Pair',
5342+
new IdentifierTypeNode('TFirst'),
5343+
[
5344+
new TemplateTagValueNode('TFirst', null, ''),
5345+
new TemplateTagValueNode('TSecond', null, ''),
5346+
],
5347+
),
5348+
),
5349+
]),
5350+
];
5351+
5352+
yield [
5353+
'OK multiline with comments and a trailing comma',
5354+
'/**
5355+
* @phpstan-type Pair<
5356+
* TFirst, // the first one
5357+
* TSecond of object = stdClass, // the second one
5358+
* > TFirst
5359+
*/',
5360+
new PhpDocNode([
5361+
new PhpDocTagNode(
5362+
'@phpstan-type',
5363+
new TypeAliasTagValueNode(
5364+
'Pair',
5365+
new IdentifierTypeNode('TFirst'),
5366+
[
5367+
new TemplateTagValueNode('TFirst', null, ''),
5368+
new TemplateTagValueNode('TSecond', new IdentifierTypeNode('object'), '', new IdentifierTypeNode('stdClass')),
5369+
],
5370+
),
5371+
),
5372+
]),
5373+
];
5374+
5375+
yield [
5376+
'invalid without closing angle bracket',
5377+
'/** @phpstan-type Foo<T T */',
5378+
new PhpDocNode([
5379+
new PhpDocTagNode(
5380+
'@phpstan-type',
5381+
new InvalidTagValueNode(
5382+
'Foo<T T',
5383+
new ParserException(
5384+
'T',
5385+
Lexer::TOKEN_IDENTIFIER,
5386+
24,
5387+
Lexer::TOKEN_CLOSE_ANGLE_BRACKET,
5388+
null,
5389+
1,
5390+
),
5391+
),
5392+
),
5393+
]),
5394+
];
5395+
5396+
yield [
5397+
'invalid without template types',
5398+
'/** @phpstan-type Foo<> T */',
5399+
new PhpDocNode([
5400+
new PhpDocTagNode(
5401+
'@phpstan-type',
5402+
new InvalidTagValueNode(
5403+
'Foo<> T',
5404+
new ParserException(
5405+
'>',
5406+
Lexer::TOKEN_CLOSE_ANGLE_BRACKET,
5407+
22,
5408+
Lexer::TOKEN_IDENTIFIER,
5409+
null,
5410+
1,
5411+
),
5412+
),
5413+
),
5414+
]),
5415+
];
53125416
}
53135417

53145418
public function provideTypeAliasImportTagsData(): Iterator
@@ -7928,6 +8032,23 @@ public function dataDeepNodesLinesAndIndexes(): iterable
79288032
[1, 1, 3, 1], // GenericTagValueNode is empty so start index is higher than end index
79298033
],
79308034
];
8035+
yield [
8036+
'/**' . PHP_EOL .
8037+
' * @phpstan-type Pair<' . PHP_EOL .
8038+
' * TFirst,' . PHP_EOL .
8039+
' * TSecond of object' . PHP_EOL .
8040+
' * > TFirst' . PHP_EOL .
8041+
' */',
8042+
[
8043+
[1, 6, 0, 22], // PhpDocNode
8044+
[2, 5, 2, 20], // PhpDocTagNode
8045+
[2, 5, 4, 20], // TypeAliasTagValueNode
8046+
[3, 3, 8, 8], // TemplateTagValueNode TFirst
8047+
[4, 4, 12, 16], // TemplateTagValueNode TSecond
8048+
[4, 4, 16, 16], // IdentifierTypeNode object
8049+
[5, 5, 20, 20], // IdentifierTypeNode TFirst
8050+
],
8051+
];
79318052
}
79328053

79338054
/**

‎tests/PHPStan/Printer/PrinterTest.php‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
3434
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
3535
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
36+
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
3637
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
3738
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
3839
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeUnsealedTypeNode;
@@ -1663,6 +1664,92 @@ public function enterNode(Node $node)
16631664
$removeImportAs,
16641665
];
16651666

1667+
$addTypeAliasTemplateType = new class extends AbstractNodeVisitor {
1668+
1669+
public function enterNode(Node $node)
1670+
{
1671+
if ($node instanceof TypeAliasTagValueNode) {
1672+
$node->templateTypes[] = new TemplateTagValueNode(
1673+
'T',
1674+
new IdentifierTypeNode('int'),
1675+
'',
1676+
);
1677+
}
1678+
1679+
return $node;
1680+
}
1681+
1682+
};
1683+
1684+
yield [
1685+
'/** @phpstan-type Foo array{int} */',
1686+
'/** @phpstan-type Foo<T of int> array{int} */',
1687+
$addTypeAliasTemplateType,
1688+
];
1689+
1690+
yield [
1691+
'/** @phpstan-type Foo<U> array{U} */',
1692+
'/** @phpstan-type Foo<U, T of int> array{U} */',
1693+
$addTypeAliasTemplateType,
1694+
];
1695+
1696+
yield [
1697+
self::nowdoc('
1698+
/**
1699+
* @phpstan-type Foo<
1700+
* U,
1701+
* V,
1702+
* > array{U, V}
1703+
*/'),
1704+
self::nowdoc('
1705+
/**
1706+
* @phpstan-type Foo<
1707+
* U,
1708+
* V,
1709+
* T of int,
1710+
* > array{U, V}
1711+
*/'),
1712+
$addTypeAliasTemplateType,
1713+
];
1714+
1715+
$removeTypeAliasTemplateType = new class extends AbstractNodeVisitor {
1716+
1717+
public function enterNode(Node $node)
1718+
{
1719+
if ($node instanceof TypeAliasTagValueNode) {
1720+
array_pop($node->templateTypes);
1721+
}
1722+
1723+
return $node;
1724+
}
1725+
1726+
};
1727+
1728+
yield [
1729+
'/** @phpstan-type Foo<T, U> array{T} */',
1730+
'/** @phpstan-type Foo<T> array{T} */',
1731+
$removeTypeAliasTemplateType,
1732+
];
1733+
1734+
$changeTypeAliasTemplateBound = new class extends AbstractNodeVisitor {
1735+
1736+
public function enterNode(Node $node)
1737+
{
1738+
if ($node instanceof TypeAliasTagValueNode) {
1739+
$node->templateTypes[1]->bound = new IdentifierTypeNode('object');
1740+
}
1741+
1742+
return $node;
1743+
}
1744+
1745+
};
1746+
1747+
yield [
1748+
'/** @phpstan-type Foo<T, U> array{T, U} */',
1749+
'/** @phpstan-type Foo<T, U of object> array{T, U} */',
1750+
$changeTypeAliasTemplateBound,
1751+
];
1752+
16661753
$addMethodTemplateType = new class extends AbstractNodeVisitor {
16671754

16681755
public function enterNode(Node $node)

0 commit comments

Comments
 (0)