Skip to content

Support for type alias tags #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2021
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
28 changes: 28 additions & 0 deletions src/Ast/PhpDoc/PhpDocNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,34 @@ public function getMethodTagValues(string $tagName = '@method'): array
}


/**
* @return TypeAliasTagValueNode[]
*/
public function getTypeAliasTagValues(string $tagName = '@phpstan-type'): array
{
return array_column(
array_filter($this->getTagsByName($tagName), static function (PhpDocTagNode $tag): bool {
return $tag->value instanceof TypeAliasTagValueNode;
}),
'value'
);
}


/**
* @return TypeAliasImportTagValueNode[]
*/
public function getTypeAliasImportTagValues(string $tagName = '@phpstan-import-type'): array
{
return array_column(
array_filter($this->getTagsByName($tagName), static function (PhpDocTagNode $tag): bool {
return $tag->value instanceof TypeAliasImportTagValueNode;
}),
'value'
);
}


public function __toString(): string
{
return "/**\n * " . implode("\n * ", $this->children) . '*/';
Expand Down
34 changes: 34 additions & 0 deletions src/Ast/PhpDoc/TypeAliasImportTagValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;

class TypeAliasImportTagValueNode implements PhpDocTagValueNode
{

/** @var string */
public $importedAlias;

/** @var IdentifierTypeNode */
public $importedFrom;

/** @var string|null */
public $importedAs;

public function __construct(string $importedAlias, IdentifierTypeNode $importedFrom, ?string $importedAs)
{
$this->importedAlias = $importedAlias;
$this->importedFrom = $importedFrom;
$this->importedAs = $importedAs;
}

public function __toString(): string
{
return trim(
"{$this->importedAlias} from {$this->importedFrom}"
. ($this->importedAs !== null ? " as {$this->importedAs}" : '')
);
}

}
28 changes: 28 additions & 0 deletions src/Ast/PhpDoc/TypeAliasTagValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\Type\TypeNode;

class TypeAliasTagValueNode implements PhpDocTagValueNode
{

/** @var string */
public $alias;

/** @var TypeNode */
public $type;

public function __construct(string $alias, TypeNode $type)
{
$this->alias = $alias;
$this->type = $type;
}


public function __toString(): string
{
return trim("{$this->alias} {$this->type}");
}

}
49 changes: 49 additions & 0 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
$tagValue = $this->parseExtendsTagValue('@use', $tokens);
break;

case '@phpstan-type':
case '@psalm-type':
$tagValue = $this->parseTypeAliasTagValue($tokens);
break;

case '@phpstan-import-type':
case '@psalm-import-type':
$tagValue = $this->parseTypeAliasImportTagValue($tokens);
break;

default:
$tagValue = new Ast\PhpDoc\GenericTagValueNode($this->parseOptionalDescription($tokens));
break;
Expand Down Expand Up @@ -364,6 +374,45 @@ private function parseExtendsTagValue(string $tagName, TokenIterator $tokens): A
throw new \PHPStan\ShouldNotHappenException();
}

private function parseTypeAliasTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeAliasTagValueNode
{
$alias = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

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

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

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

private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc\TypeAliasImportTagValueNode
{
$importedAlias = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

if (!$tokens->tryConsumeTokenValue('from')) {
throw new \PHPStan\PhpDocParser\Parser\ParserException(
$tokens->currentTokenValue(),
$tokens->currentTokenType(),
$tokens->currentTokenOffset(),
Lexer::TOKEN_IDENTIFIER
);
}

$importedFrom = $this->typeParser->parse($tokens);
assert($importedFrom instanceof IdentifierTypeNode);

$importedAs = null;
if ($tokens->tryConsumeTokenValue('as')) {
$importedAs = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
}

return new Ast\PhpDoc\TypeAliasImportTagValueNode($importedAlias, $importedFrom, $importedAs);
}

private function parseOptionalVariableName(TokenIterator $tokens): string
{
if ($tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)) {
Expand Down
169 changes: 169 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
Expand Down Expand Up @@ -66,6 +68,8 @@ protected function setUp(): void
* @dataProvider provideMultiLinePhpDocData
* @dataProvider provideTemplateTagsData
* @dataProvider provideExtendsTagsData
* @dataProvider provideTypeAliasTagsData
* @dataProvider provideTypeAliasImportTagsData
* @dataProvider provideRealWorldExampleData
* @dataProvider provideDescriptionWithOrWithoutHtml
* @param string $label
Expand Down Expand Up @@ -2858,6 +2862,171 @@ public function provideExtendsTagsData(): \Iterator
];
}

public function provideTypeAliasTagsData(): \Iterator
{
yield [
'OK',
'/** @phpstan-type TypeAlias string|int */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new TypeAliasTagValueNode(
'TypeAlias',
new UnionTypeNode([
new IdentifierTypeNode('string'),
new IdentifierTypeNode('int'),
])
)
),
]),
];

yield [
'OK with psalm syntax',
'/** @psalm-type TypeAlias=string|int */',
new PhpDocNode([
new PhpDocTagNode(
'@psalm-type',
new TypeAliasTagValueNode(
'TypeAlias',
new UnionTypeNode([
new IdentifierTypeNode('string'),
new IdentifierTypeNode('int'),
])
)
),
]),
];

yield [
'invalid without type',
'/** @phpstan-type TypeAlias */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new InvalidTagValueNode(
'TypeAlias',
new \PHPStan\PhpDocParser\Parser\ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
28,
Lexer::TOKEN_IDENTIFIER
)
)
),
]),
];

yield [
'invalid empty',
'/** @phpstan-type */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-type',
new InvalidTagValueNode(
'',
new \PHPStan\PhpDocParser\Parser\ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
18,
Lexer::TOKEN_IDENTIFIER
)
)
),
]),
];
}

public function provideTypeAliasImportTagsData(): \Iterator
{
yield [
'OK',
'/** @phpstan-import-type TypeAlias from AnotherClass */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-import-type',
new TypeAliasImportTagValueNode(
'TypeAlias',
new IdentifierTypeNode('AnotherClass'),
null
)
),
]),
];

yield [
'OK with alias',
'/** @phpstan-import-type TypeAlias from AnotherClass as DifferentAlias */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-import-type',
new TypeAliasImportTagValueNode(
'TypeAlias',
new IdentifierTypeNode('AnotherClass'),
'DifferentAlias'
)
),
]),
];

yield [
'invalid missing from',
'/** @phpstan-import-type TypeAlias */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-import-type',
new InvalidTagValueNode(
'TypeAlias',
new \PHPStan\PhpDocParser\Parser\ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
35,
Lexer::TOKEN_IDENTIFIER
)
)
),
]),
];

yield [
'invalid missing from with alias',
'/** @phpstan-import-type TypeAlias as DifferentAlias */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-import-type',
new InvalidTagValueNode(
'TypeAlias as DifferentAlias',
new \PHPStan\PhpDocParser\Parser\ParserException(
'as',
Lexer::TOKEN_IDENTIFIER,
35,
Lexer::TOKEN_IDENTIFIER
)
)
),
]),
];

yield [
'invalid empty',
'/** @phpstan-import-type */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-import-type',
new InvalidTagValueNode(
'',
new \PHPStan\PhpDocParser\Parser\ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
25,
Lexer::TOKEN_IDENTIFIER
)
)
),
]),
];
}

public function providerDebug(): \Iterator
{
$sample = '/**
Expand Down