Skip to content

Add support for reference in phpdoc #83

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 1 commit into from
Sep 16, 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
5 changes: 4 additions & 1 deletion doc/grammars/phpdoc-param.peg
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
PhpDocParam
= AnnotationName Type IsVariadic? ParameterName Description?
= AnnotationName Type IsReference? IsVariadic? ParameterName Description?

AnnotationName
= '@param'

IsReference
= '&'

IsVariaric
= '...'

Expand Down
9 changes: 7 additions & 2 deletions src/Ast/PhpDoc/ParamTagValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ParamTagValueNode implements PhpDocTagValueNode
/** @var TypeNode */
public $type;

/** @var bool */
public $isReference;

/** @var bool */
public $isVariadic;

Expand All @@ -22,9 +25,10 @@ class ParamTagValueNode implements PhpDocTagValueNode
/** @var string (may be empty) */
public $description;

public function __construct(TypeNode $type, bool $isVariadic, string $parameterName, string $description)
public function __construct(TypeNode $type, bool $isReference, bool $isVariadic, string $parameterName, string $description)
{
$this->type = $type;
$this->isReference = $isReference;
$this->isVariadic = $isVariadic;
$this->parameterName = $parameterName;
$this->description = $description;
Expand All @@ -33,8 +37,9 @@ public function __construct(TypeNode $type, bool $isVariadic, string $parameterN

public function __toString(): string
{
$reference = $this->isReference ? '&' : '';
$variadic = $this->isVariadic ? '...' : '';
return trim("{$this->type} {$variadic}{$this->parameterName} {$this->description}");
return trim("{$this->type} {$reference}{$variadic}{$this->parameterName} {$this->description}");
}

}
3 changes: 2 additions & 1 deletion src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
private function parseParamTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamTagValueNode
{
$type = $this->typeParser->parse($tokens);
$isReference = $tokens->tryConsumeTokenType(Lexer::TOKEN_REFERENCE);
$isVariadic = $tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC);
$parameterName = $this->parseRequiredVariableName($tokens);
$description = $this->parseOptionalDescription($tokens);
return new Ast\PhpDoc\ParamTagValueNode($type, $isVariadic, $parameterName, $description);
return new Ast\PhpDoc\ParamTagValueNode($type, $isReference, $isVariadic, $parameterName, $description);
}


Expand Down
84 changes: 84 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function provideParamTagsData(): \Iterator
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
false,
'$foo',
''
)
Expand All @@ -115,6 +116,7 @@ public function provideParamTagsData(): \Iterator
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
false,
'$foo',
'optional description'
)
Expand All @@ -130,6 +132,7 @@ public function provideParamTagsData(): \Iterator
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
true,
'$foo',
''
Expand All @@ -146,6 +149,75 @@ public function provideParamTagsData(): \Iterator
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
true,
'$foo',
'optional description'
)
),
]),
];

yield [
'OK reference without description',
'/** @param Foo &$foo */',
new PhpDocNode([
new PhpDocTagNode(
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
true,
false,
'$foo',
''
)
),
]),
];

yield [
'OK reference with description',
'/** @param Foo &$foo optional description */',
new PhpDocNode([
new PhpDocTagNode(
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
true,
false,
'$foo',
'optional description'
)
),
]),
];

yield [
'OK reference variadic without description',
'/** @param Foo &...$foo */',
new PhpDocNode([
new PhpDocTagNode(
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
true,
true,
'$foo',
''
)
),
]),
];

yield [
'OK reference variadic with description',
'/** @param Foo &...$foo optional description */',
new PhpDocNode([
new PhpDocTagNode(
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
true,
true,
'$foo',
'optional description'
Expand Down Expand Up @@ -1827,6 +1899,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
false,
'$foo',
'1st multi world description'
)
Expand All @@ -1836,6 +1909,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('Bar'),
false,
false,
'$bar',
'2nd multi world description'
)
Expand All @@ -1855,6 +1929,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
false,
'$foo',
'1st multi world description
some text in the middle'
Expand All @@ -1865,6 +1940,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('Bar'),
false,
false,
'$bar',
'2nd multi world description'
)
Expand Down Expand Up @@ -1895,6 +1971,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
false,
'$foo',
'1st multi world description with empty lines'
)
Expand All @@ -1909,6 +1986,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('Bar'),
false,
false,
'$bar',
'2nd multi world description with empty lines'
)
Expand Down Expand Up @@ -1942,6 +2020,7 @@ public function provideMultiLinePhpDocData(): array
new ParamTagValueNode(
new IdentifierTypeNode('int'),
false,
false,
'$foo',
'@param string $bar'
)
Expand Down Expand Up @@ -2857,6 +2936,7 @@ public function provideExtendsTagsData(): \Iterator
new ParamTagValueNode(
new IdentifierTypeNode('class-string'),
false,
false,
'$test',
''
)
Expand All @@ -2873,6 +2953,7 @@ public function provideExtendsTagsData(): \Iterator
new ParamTagValueNode(
new IdentifierTypeNode('class-string'),
false,
false,
'$test',
'some description'
)
Expand Down Expand Up @@ -3166,6 +3247,7 @@ public function provideRealWorldExampleData(): \Iterator
new ParamTagValueNode(
new IdentifierTypeNode('\Drupal\Core\Field\FieldStorageDefinitionInterface'),
false,
false,
'$field_definition',
''
)
Expand Down Expand Up @@ -3243,6 +3325,7 @@ public function provideRealWorldExampleData(): \Iterator
new ParamTagValueNode(
new IdentifierTypeNode('Request'),
false,
false,
'$request',
'- The request object'
)
Expand Down Expand Up @@ -3463,6 +3546,7 @@ public function dataParseTagValue(): array
new ParamTagValueNode(
new ConstTypeNode(new ConstFetchNode('DateTimeImmutable', 'ATOM')),
false,
false,
'$a',
''
),
Expand Down