Support braces in types for @param

This commit is contained in:
Mike van Riel
2019-12-27 21:02:14 +01:00
parent 506fd89d43
commit bb19583948
+13 -23
View File
@@ -29,14 +29,8 @@ use function substr;
/** /**
* Reflection class for the {@}param tag in a Docblock. * Reflection class for the {@}param tag in a Docblock.
*/ */
final class Param extends BaseTag implements Factory\StaticMethod final class Param extends TagWithType implements Factory\StaticMethod
{ {
/** @var string */
protected $name = 'param';
/** @var Type|null */
private $type;
/** @var string|null */ /** @var string|null */
private $variableName; private $variableName;
@@ -49,6 +43,7 @@ final class Param extends BaseTag implements Factory\StaticMethod
bool $isVariadic = false, bool $isVariadic = false,
?Description $description = null ?Description $description = null
) { ) {
$this->name = 'param';
$this->variableName = $variableName; $this->variableName = $variableName;
$this->type = $type; $this->type = $type;
$this->isVariadic = $isVariadic; $this->isVariadic = $isVariadic;
@@ -68,21 +63,24 @@ final class Param extends BaseTag implements Factory\StaticMethod
Assert::notNull($typeResolver); Assert::notNull($typeResolver);
Assert::notNull($descriptionFactory); Assert::notNull($descriptionFactory);
$parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE); list($firstPart, $body) = self::extractTypeFromBody($body);
Assert::isArray($parts); $type = null;
$type = null; $parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE);
$variableName = ''; $variableName = '';
$isVariadic = false; $isVariadic = false;
// if the first item that is encountered is not a variable; it is a type // if the first item that is encountered is not a variable; it is a type
if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) { if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) {
$type = $typeResolver->resolve(array_shift($parts), $context); $type = $typeResolver->resolve($firstPart, $context);
array_shift($parts); } else {
// first part is not a type; we should prepend it to the parts array for further processing
array_unshift($parts, $firstPart);
} }
// if the next item starts with a $ or ...$ it must be the variable name // if the next item starts with a $ or ...$ it must be the variable name
if (isset($parts[0]) && ($parts[0] !== '') && if (isset($parts[0])
(strpos($parts[0], '$') === 0 || strpos($parts[0], '...$') === 0) && (strlen($parts[0]) > 0)
&& ($parts[0][0] === '$' || substr($parts[0], 0, 4) === '...$')
) { ) {
$variableName = array_shift($parts); $variableName = array_shift($parts);
array_shift($parts); array_shift($parts);
@@ -110,14 +108,6 @@ final class Param extends BaseTag implements Factory\StaticMethod
return $this->variableName; return $this->variableName;
} }
/**
* Returns the variable's type or null if unknown.
*/
public function getType() : ?Type
{
return $this->type;
}
/** /**
* Returns whether this tag is variadic. * Returns whether this tag is variadic.
*/ */