Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion doc/grammars/phpdoc-block.pp3
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,25 @@ ExtendsTagValue
/**
* A type written under a name of its own, like:
* - @phpstan-type Foo = int|string
* - @phpstan-type Pair<TFirst, TSecond of object> 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() <T_EQUAL>? Type() &EndOfLine()
: Identifier() ( TypeAliasTemplates() | !AngleBracketOpen() ) <T_EQUAL>? 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()* <T_CLOSE_ANGLE_BRACKET>
;

/**
Expand Down
17 changes: 14 additions & 3 deletions src/Ast/PhpDoc/TypeAliasTagValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,25 +14,35 @@ 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}");
}

/**
* @param array<string, mixed> $properties
*/
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);
Expand Down
7 changes: 6 additions & 1 deletion src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -634,11 +634,14 @@ private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNod
}

/**
* Parses a list of templates in angle brackets, like the "<T, U of Foo>"
* 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);

Expand Down
6 changes: 5 additions & 1 deletion src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ', ',
];
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Ast/ToString/PhpDocToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public static function provideClassCases(): Generator

yield from [
['Foo array<string>', new TypeAliasTagValueNode('Foo', $arrayOfStrings)],
['Wrapper<T> T', new TypeAliasTagValueNode('Wrapper', new IdentifierTypeNode('T'), [new TemplateTagValueNode('T', null, '')])],
['Pair<TFirst, TSecond> TFirst', new TypeAliasTagValueNode('Pair', new IdentifierTypeNode('TFirst'), [new TemplateTagValueNode('TFirst', null, ''), new TemplateTagValueNode('TSecond', null, '')])],
['Collection<T of object> array<string>', new TypeAliasTagValueNode('Collection', $arrayOfStrings, [new TemplateTagValueNode('T', new IdentifierTypeNode('object'), '')])],
['WithDefault<T = string> 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')],
];
Expand Down
Loading
Loading