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
16 changes: 15 additions & 1 deletion doc/grammars/types.pp3
Original file line number Diff line number Diff line change
Expand Up @@ -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<T of Foo, T>. 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() ( <T_KEYWORD_OF> | <T_KEYWORD_AS> )
;

Variance
Expand Down
50 changes: 50 additions & 0 deletions src/Ast/Type/TemplateDeclarationTypeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\Type;

use PHPStan\PhpDocParser\Ast\NodeAttributes;

/**
* A template declared in a generic type argument, like the "T of Foo" in
* class-string-map<T of Foo, T> or the "K of key-of<Config>" in
* array<K of key-of<Config>, 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<string, mixed> $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;
}

}
45 changes: 45 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
82 changes: 82 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2564,6 +2565,87 @@ public function provideParseData(): array
null,
),
],
[
'class-string-map<T of Foo, T>',
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<T as Foo, list<T>>',
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<Config>|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<covariant of, of>',
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('of'),
new IdentifierTypeNode('of'),
],
[
GenericTypeNode::VARIANCE_COVARIANT,
GenericTypeNode::VARIANCE_INVARIANT,
],
),
],
[
'Foo<T of>',
new ParserException(
'>',
Lexer::TOKEN_CLOSE_ANGLE_BRACKET,
8,
Lexer::TOKEN_IDENTIFIER,
null,
null,
),
],
[
'Foo<typovariant Bar>',
new ParserException(
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3036,6 +3037,29 @@ public function enterNode(Node $node)

},
];

yield [
self::nowdoc('
/**
* @var class-string-map<T as Foo, list<T>>
*/'),
self::nowdoc('
/**
* @var class-string-map<T as Bar, list<T>>
*/'),
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof TemplateDeclarationTypeNode) {
$node->bound = new IdentifierTypeNode('Bar');
}

return $node;
}

},
];
}

/**
Expand Down
Loading