From 1a5d9a2dbc54b48239a6c4b82db52e11a58212b6 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Fri, 27 Dec 2019 20:51:03 +0100 Subject: [PATCH] Add braces support to Property, Property-Read, Property-Write and Var --- src/DocBlock/Tags/Property.php | 33 +++++++++---------------- src/DocBlock/Tags/PropertyRead.php | 33 +++++++++---------------- src/DocBlock/Tags/PropertyWrite.php | 33 +++++++++---------------- src/DocBlock/Tags/Var_.php | 38 +++++++++++------------------ 4 files changed, 50 insertions(+), 87 deletions(-) diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index 290a4fa..76cd098 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -29,19 +29,16 @@ use function substr; /** * Reflection class for a {@}property tag in a Docblock. */ -final class Property extends BaseTag implements Factory\StaticMethod +final class Property extends TagWithType implements Factory\StaticMethod { - /** @var string */ - protected $name = 'property'; - - /** @var Type|null */ - private $type; - /** @var string|null */ protected $variableName = ''; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { + Assert::string($variableName); + + $this->name = 'property'; $this->variableName = $variableName; $this->type = $type; $this->description = $description; @@ -60,15 +57,17 @@ final class Property extends BaseTag implements Factory\StaticMethod Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); - $parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE); - Assert::isArray($parts); - $type = null; + list($firstPart, $body) = self::extractTypeFromBody($body); + $type = null; + $parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE); $variableName = ''; // if the first item that is encountered is not a variable; it is a type - if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) { - $type = $typeResolver->resolve(array_shift($parts), $context); - array_shift($parts); + if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) { + $type = $typeResolver->resolve($firstPart, $context); + } 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 @@ -94,14 +93,6 @@ final class Property extends BaseTag implements Factory\StaticMethod return $this->variableName; } - /** - * Returns the variable's type or null if unknown. - */ - public function getType() : ?Type - { - return $this->type; - } - /** * Returns a string representation for this tag. */ diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 4e0c904..762286b 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -29,19 +29,16 @@ use function substr; /** * Reflection class for a {@}property-read tag in a Docblock. */ -final class PropertyRead extends BaseTag implements Factory\StaticMethod +final class PropertyRead extends TagWithType implements Factory\StaticMethod { - /** @var string */ - protected $name = 'property-read'; - - /** @var Type|null */ - private $type; - /** @var string|null */ protected $variableName = ''; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { + Assert::string($variableName); + + $this->name = 'property-read'; $this->variableName = $variableName; $this->type = $type; $this->description = $description; @@ -60,15 +57,17 @@ final class PropertyRead extends BaseTag implements Factory\StaticMethod Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); - $parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE); - Assert::isArray($parts); - $type = null; + list($firstPart, $body) = self::extractTypeFromBody($body); + $type = null; + $parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE); $variableName = ''; // if the first item that is encountered is not a variable; it is a type - if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) { - $type = $typeResolver->resolve(array_shift($parts), $context); - array_shift($parts); + if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) { + $type = $typeResolver->resolve($firstPart, $context); + } 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 @@ -94,14 +93,6 @@ final class PropertyRead extends BaseTag implements Factory\StaticMethod return $this->variableName; } - /** - * Returns the variable's type or null if unknown. - */ - public function getType() : ?Type - { - return $this->type; - } - /** * Returns a string representation for this tag. */ diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index 014b1ac..98f6141 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -30,19 +30,16 @@ use function substr; /** * Reflection class for a {@}property-write tag in a Docblock. */ -final class PropertyWrite extends BaseTag implements Factory\StaticMethod +final class PropertyWrite extends TagWithType implements Factory\StaticMethod { /** @var string */ - protected $name = 'property-write'; - - /** @var Type|null */ - private $type; - - /** @var string|null */ protected $variableName = ''; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { + Assert::string($variableName); + + $this->name = 'property-write'; $this->variableName = $variableName; $this->type = $type; $this->description = $description; @@ -61,15 +58,17 @@ final class PropertyWrite extends BaseTag implements Factory\StaticMethod Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); - $parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE); - Assert::isArray($parts); - $type = null; + list($firstPart, $body) = self::extractTypeFromBody($body); + $type = null; + $parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE); $variableName = ''; // if the first item that is encountered is not a variable; it is a type - if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) { - $type = $typeResolver->resolve(array_shift($parts), $context); - array_shift($parts); + if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) { + $type = $typeResolver->resolve($firstPart, $context); + } 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 @@ -95,14 +94,6 @@ final class PropertyWrite extends BaseTag implements Factory\StaticMethod return $this->variableName; } - /** - * Returns the variable's type or null if unknown. - */ - public function getType() : ?Type - { - return $this->type; - } - /** * Returns a string representation for this tag. */ diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index 1bd5b8a..cc21758 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -29,22 +29,19 @@ use function substr; /** * Reflection class for a {@}var tag in a Docblock. */ -final class Var_ extends BaseTag implements Factory\StaticMethod +final class Var_ extends TagWithType implements Factory\StaticMethod { - /** @var string */ - protected $name = 'var'; - - /** @var Type|null */ - private $type; - /** @var string|null */ protected $variableName = ''; public function __construct(?string $variableName, ?Type $type = null, ?Description $description = null) { + Assert::string($variableName); + + $this->name = 'var'; $this->variableName = $variableName; - $this->type = $type; - $this->description = $description; + $this->type = $type; + $this->description = $description; } /** @@ -60,16 +57,17 @@ final class Var_ extends BaseTag implements Factory\StaticMethod Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); - $parts = preg_split('/(\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE); - Assert::isArray($parts); - Assert::allString($parts); - $type = null; + list($firstPart, $body) = self::extractTypeFromBody($body); + $parts = preg_split('/(\s+)/Su', $body, 2, PREG_SPLIT_DELIM_CAPTURE); + $type = null; $variableName = ''; // if the first item that is encountered is not a variable; it is a type - if (isset($parts[0]) && ($parts[0] !== '') && ($parts[0][0] !== '$')) { - $type = $typeResolver->resolve(array_shift($parts), $context); - array_shift($parts); + if ($firstPart && (strlen($firstPart) > 0) && ($firstPart[0] !== '$')) { + $type = $typeResolver->resolve($firstPart, $context); + } 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 @@ -95,14 +93,6 @@ final class Var_ extends BaseTag implements Factory\StaticMethod return $this->variableName; } - /** - * Returns the variable's type or null if unknown. - */ - public function getType() : ?Type - { - return $this->type; - } - /** * Returns a string representation for this tag. */