FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Generic type aliases by MidnightDesign · Pull Request #231 · phpstan/phpdoc-parser · GitHub

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .php  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 19 additions & 2 deletions src/Ast/PhpDoc/TypeAliasTagValueNode.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use function count;
use function implode;
use function trim;

class TypeAliasTagValueNode implements PhpDocTagValueNode
Expand All @@ -17,16 +19,31 @@ class TypeAliasTagValueNode implements PhpDocTagValueNode
/** @var TypeNode */
public $type;

public function __construct(string $alias, TypeNode $type)
/** @var array<string, TypeNode|null> */
public $typeArguments;

/**
* @param array<string, TypeNode|null> $typeArguments
*/
public function __construct(string $alias, TypeNode $type, array $typeArguments = [])
{
$this->alias = $alias;
$this->type = $type;
$this->typeArguments = $typeArguments;
}


public function __toString(): string
{
return trim("{$this->alias} {$this->type}");
$args = '';
if (count($this->typeArguments) > 0) {
$printedArgs = [];
foreach ($this->typeArguments as $name => $bound) {
$printedArgs[] = $name . ($bound === null ? '' : ' of ' . $bound);
}
$args = '<' . implode(', ', $printedArgs) . '>';
}
return trim("{$this->alias}{$args} {$this->type}");
}

}
26 changes: 23 additions & 3 deletions src/Parser/PhpDocParser.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,25 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA
$alias = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

$typeArguments = [];
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
while ($tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
$name = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
if ($tokens->tryConsumeTokenValue('of')) {
$bound = $this->typeParser->parse($tokens);
} else {
$bound = null;
}
$typeArguments[$name] = $bound;
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
continue;
}
break;
}
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
}

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

Expand All @@ -1082,19 +1101,20 @@ private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeA
}
}

return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type);
return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type, $typeArguments);
} catch (ParserException $e) {
$this->parseOptionalDescription($tokens);
return new Ast\PhpDoc\TypeAliasTagValueNode(
$alias,
$this->enrichWithAttributes($tokens, new Ast\Type\InvalidTypeNode($e), $startLine, $startIndex)
$this->enrichWithAttributes($tokens, new Ast\Type\InvalidTypeNode($e), $startLine, $startIndex),
$typeArguments
);
}
}

$type = $this->typeParser->parse($tokens);

return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type);
return new Ast\PhpDoc\TypeAliasTagValueNode($alias, $type, $typeArguments);
}

private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeAliasImportTagValueNode
Expand Down
10 changes: 9 additions & 1 deletion src/Printer/Printer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,16 @@ private function printTagValue(PhpDocTagValueNode $node): string
);
}
if ($node instanceof TypeAliasTagValueNode) {
$args = '';
if (count($node->typeArguments) > 0) {
$printedArgs = [];
foreach ($node->typeArguments as $name => $bound) {
$printedArgs[] = $name . ($bound === null ? '' : ' of ' . $this->printType($bound));
}
$args = '<' . implode(', ', $printedArgs) . '>';
}
$type = $this->printType($node->type);
return trim("{$node->alias} {$type}");
return trim("{$node->alias}{$args} {$type}");
}
if ($node instanceof UsesTagValueNode) {
$type = $this->printType($node->type);
Expand Down
54 changes: 54 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4470,6 +4470,60 @@ public function provideTypeAliasTagsData(): Iterator
),
]),
];

yield [
'Type argument',
'/** @phpstan-type TypeAlias<T> callable(T): T */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new TypeAliasTagValueNode(
'TypeAlias',
new CallableTypeNode(
new IdentifierTypeNode('callable'),
[
new CallableTypeParameterNode(
new IdentifierTypeNode('T'),
false,
false,
'',
false
),
],
new IdentifierTypeNode('T')
),
['T' => null]
)
),
]),
];

yield [
'Bound type argument',
'/** @phpstan-type TypeAlias<T of string> callable(T): T */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new TypeAliasTagValueNode(
'TypeAlias',
new CallableTypeNode(
new IdentifierTypeNode('callable'),
[
new CallableTypeParameterNode(
new IdentifierTypeNode('T'),
false,
false,
'',
false
),
],
new IdentifierTypeNode('T')
),
['T' => new IdentifierTypeNode('string')]
)
),
]),
];
}

public function provideTypeAliasImportTagsData(): Iterator
Expand Down
61 changes: 61 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
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\ArrayTypeNode;
Expand Down Expand Up @@ -1811,6 +1812,66 @@ public function dataPrintPhpDocNode(): iterable
* @param int $a
*/',
];

yield [
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new TypeAliasTagValueNode(
'TypeAlias',
new CallableTypeNode(
new IdentifierTypeNode('callable'),
[
new CallableTypeParameterNode(
new IdentifierTypeNode('T'),
false,
false,
'',
false
),
],
new IdentifierTypeNode('T')
),
['T' => null]
)
),
]),
<<<DOC
/**
* @phpstan-type TypeAlias<T> callable(T): T
*/
DOC,
];

yield [
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new TypeAliasTagValueNode(
'TypeAlias',
new CallableTypeNode(
new IdentifierTypeNode('callable'),
[
new CallableTypeParameterNode(
new IdentifierTypeNode('T'),
false,
false,
'',
false
),
],
new IdentifierTypeNode('T')
),
['T' => new IdentifierTypeNode('string')]
)
),
]),
<<<DOC
/**
* @phpstan-type TypeAlias<T of string> callable(T): T
*/
DOC,
];
}

/**
Expand Down

Back | FazBrowse Home | New Git URL