PhpDocParser: support variadic parameters in @param tag

This commit is contained in:
Jan Tvrdik 2017-11-19 18:07:18 +01:00
parent 597aa4da36
commit 4e272700b3
4 changed files with 34 additions and 4 deletions

View File

@ -1,9 +1,12 @@
PhpDocParam
= AnnotationName Type ParameterName Description?
= AnnotationName Type IsVariadic? ParameterName Description?
AnnotationName
= '@param'
IsVariaric
= '...'
ParameterName
= '$' [a-zA-Z_\127-\255][a-zA-Z0-9_\127-\255]*

View File

@ -10,15 +10,19 @@ class ParamTagValueNode implements PhpDocTagValueNode
/** @var TypeNode */
public $type;
/** @var bool */
public $isVariadic;
/** @var string (may be empty) */
public $parameterName;
/** @var string (may be empty) */
public $description;
public function __construct(TypeNode $type, string $parameterName, string $description)
public function __construct(TypeNode $type, bool $isVariadic, string $parameterName, string $description)
{
$this->type = $type;
$this->isVariadic = $isVariadic;
$this->parameterName = $parameterName;
$this->description = $description;
}
@ -26,7 +30,8 @@ class ParamTagValueNode implements PhpDocTagValueNode
public function __toString(): string
{
return trim("{$this->type} {$this->parameterName} {$this->description}");
$variadic = $this->isVariadic ? '...' : '';
return trim("{$this->type} {$variadic}{$this->parameterName} {$this->description}");
}
}

View File

@ -122,9 +122,10 @@ class PhpDocParser
private function parseParamTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamTagValueNode
{
$type = $this->typeParser->parse($tokens);
$isVariadic = $tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC);
$parameterName = $this->parseRequiredVariableName($tokens);
$description = $this->parseOptionalDescription($tokens);
return new Ast\PhpDoc\ParamTagValueNode($type, $parameterName, $description);
return new Ast\PhpDoc\ParamTagValueNode($type, $isVariadic, $parameterName, $description);
}

View File

@ -95,6 +95,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
'$foo',
''
)
@ -127,6 +128,22 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
'$foo',
'optional description '
)
),
]),
],
[
'/** @param Foo ...$foo optional description */',
new PhpDocNode([
new PhpDocTextNode(' '),
new PhpDocTagNode(
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
true,
'$foo',
'optional description '
)
@ -289,6 +306,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
'$foo',
'1st multi world description'
)
@ -298,6 +316,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Bar'),
false,
'$bar',
'2nd multi world description'
)
@ -317,6 +336,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Foo'),
false,
'$foo',
'1st multi world description'
)
@ -326,6 +346,7 @@ class PhpDocParserTest extends \PHPUnit\Framework\TestCase
'@param',
new ParamTagValueNode(
new IdentifierTypeNode('Bar'),
false,
'$bar',
'2nd multi world description'
)