Skip to content
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

Support @extends, @implements, @uses #31

Merged
merged 1 commit into from
Jul 16, 2019
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/ExtendsTagValueNode.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\GenericTypeNode;

class ExtendsTagValueNode implements PhpDocTagValueNode
{

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

/** @var string (may be empty) */
public $description;

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


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

}
28 changes: 28 additions & 0 deletions src/Ast/PhpDoc/ImplementsTagValueNode.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\GenericTypeNode;

class ImplementsTagValueNode implements PhpDocTagValueNode
{

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

/** @var string (may be empty) */
public $description;

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


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

}
42 changes: 42 additions & 0 deletions src/Ast/PhpDoc/PhpDocNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ public function getTemplateTagValues(): array
}


/**
* @return ExtendsTagValueNode[]
*/
public function getExtendsTagValues(): array
{
return array_column(
array_filter($this->getTagsByName('@extends'), static function (PhpDocTagNode $tag): bool {
return $tag->value instanceof ExtendsTagValueNode;
}),
'value'
);
}


/**
* @return ImplementsTagValueNode[]
*/
public function getImplementsTagValues(): array
{
return array_column(
array_filter($this->getTagsByName('@implements'), static function (PhpDocTagNode $tag): bool {
return $tag->value instanceof ImplementsTagValueNode;
}),
'value'
);
}


/**
* @return UsesTagValueNode[]
*/
public function getUsesTagValues(): array
{
return array_column(
array_filter($this->getTagsByName('@uses'), static function (PhpDocTagNode $tag): bool {
return $tag->value instanceof UsesTagValueNode;
}),
'value'
);
}


/**
* @return ReturnTagValueNode[]
*/
Expand Down
28 changes: 28 additions & 0 deletions src/Ast/PhpDoc/UsesTagValueNode.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\GenericTypeNode;

class UsesTagValueNode implements PhpDocTagValueNode
{

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

/** @var string (may be empty) */
public $description;

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


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

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

case '@extends':
case '@implements':
case '@uses':
$tagValue = $this->parseExtendsTagValue($tag, $tokens);
break;

default:
$tagValue = new Ast\PhpDoc\GenericTagValueNode($this->parseOptionalDescription($tokens));
break;
Expand Down Expand Up @@ -292,6 +298,25 @@ private function parseTemplateTagValue(TokenIterator $tokens): Ast\PhpDoc\Templa
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description);
}

private function parseExtendsTagValue(string $tagName, TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode
{
$baseType = new IdentifierTypeNode($tokens->currentTokenValue());
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

$type = $this->typeParser->parseGeneric($tokens, $baseType);

$description = $this->parseOptionalDescription($tokens);

switch ($tagName) {
case '@extends':
return new Ast\PhpDoc\ExtendsTagValueNode($type, $description);
case '@implements':
return new Ast\PhpDoc\ImplementsTagValueNode($type, $description);
case '@uses':
return new Ast\PhpDoc\UsesTagValueNode($type, $description);
}
}

private function parseOptionalVariableName(TokenIterator $tokens): string
{
if ($tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private function parseNullable(TokenIterator $tokens): Ast\Type\TypeNode
}


private function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\TypeNode
public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\GenericTypeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
$genericTypes[] = $this->parse($tokens);
Expand Down
142 changes: 142 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueParameterNode;
Expand All @@ -17,8 +19,10 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
Expand Down Expand Up @@ -51,6 +55,7 @@ protected function setUp(): void
* @dataProvider provideSingleLinePhpDocData
* @dataProvider provideMultiLinePhpDocData
* @dataProvider provideTemplateTagsData
* @dataProvider provideExtendsTagsData
* @dataProvider provideRealWorldExampleData
* @param string $label
* @param string $input
Expand Down Expand Up @@ -2368,6 +2373,143 @@ public function provideTemplateTagsData(): \Iterator
];
}

public function provideExtendsTagsData(): \Iterator
{
yield [
'OK with one argument',
'/** @extends Foo<A> */',
new PhpDocNode([
new PhpDocTagNode(
'@extends',
new ExtendsTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('A'),
]
),
''
)
),
]),
];

yield [
'OK with two arguments',
'/** @extends Foo<A,B> */',
new PhpDocNode([
new PhpDocTagNode(
'@extends',
new ExtendsTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('A'),
new IdentifierTypeNode('B'),
]
),
''
)
),
]),
];

yield [
'OK @implements',
'/** @implements Foo<A,B> */',
new PhpDocNode([
new PhpDocTagNode(
'@implements',
new ImplementsTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('A'),
new IdentifierTypeNode('B'),
]
),
''
)
),
]),
];

yield [
'OK @uses',
'/** @uses Foo<A,B> */',
new PhpDocNode([
new PhpDocTagNode(
'@uses',
new UsesTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('A'),
new IdentifierTypeNode('B'),
]
),
''
)
),
]),
];

yield [
'OK with description',
'/** @extends Foo<A> extends foo*/',
new PhpDocNode([
new PhpDocTagNode(
'@extends',
new ExtendsTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[new IdentifierTypeNode('A')]
),
'extends foo'
)
),
]),
];

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

yield [
'invalid without arguments',
'/** @extends Foo */',
new PhpDocNode([
new PhpDocTagNode(
'@extends',
new InvalidTagValueNode(
'Foo',
new \PHPStan\PhpDocParser\Parser\ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
17,
Lexer::TOKEN_OPEN_ANGLE_BRACKET
)
)
),
]),
];
}

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