mirror of
https://github.com/danog/phpdoc-parser.git
synced 2025-01-22 13:51:20 +01:00
PhpDocParser: support variadic parameters in @param tag
This commit is contained in:
parent
597aa4da36
commit
4e272700b3
@ -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]*
|
||||
|
||||
|
@ -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}");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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'
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user