mirror of
https://github.com/barryvdh/ReflectionDocBlock.git
synced 2026-08-18 10:07:12 +00:00
Move type extraction to base class and re-use in Throws
This commit is contained in:
@@ -24,17 +24,12 @@ use function preg_split;
|
||||
/**
|
||||
* Reflection class for a {@}return tag in a Docblock.
|
||||
*/
|
||||
final class Return_ extends BaseTag implements Factory\StaticMethod
|
||||
final class Return_ extends TagWithType implements Factory\StaticMethod
|
||||
{
|
||||
/** @var string */
|
||||
protected $name = 'return';
|
||||
|
||||
/** @var Type */
|
||||
private $type;
|
||||
|
||||
public function __construct(Type $type, ?Description $description = null)
|
||||
public function __construct(Type $type, Description $description = null)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->name = 'return';
|
||||
$this->type = $type;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
@@ -50,7 +45,7 @@ final class Return_ extends BaseTag implements Factory\StaticMethod
|
||||
Assert::notNull($typeResolver);
|
||||
Assert::notNull($descriptionFactory);
|
||||
|
||||
list($type, $description) = self::splitBodyIntoTypeAndTheRest($body);
|
||||
list($type, $description) = self::extractTypeFromBody($body);
|
||||
|
||||
$type = $typeResolver->resolve($type, $context);
|
||||
$description = $descriptionFactory->create($description, $context);
|
||||
@@ -58,41 +53,8 @@ final class Return_ extends BaseTag implements Factory\StaticMethod
|
||||
return new static($type, $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type section of the variable.
|
||||
*/
|
||||
public function getType() : Type
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
public function __toString() :string
|
||||
{
|
||||
return $this->type . ' ' . (string) $this->description;
|
||||
}
|
||||
|
||||
private static function splitBodyIntoTypeAndTheRest(string $body) : array
|
||||
{
|
||||
$type = '';
|
||||
$nestingLevel = 0;
|
||||
for ($i = 0; $i < strlen($body); $i++) {
|
||||
$character = $body[$i];
|
||||
|
||||
if (trim($character) === '' && $nestingLevel === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$type .= $character;
|
||||
if (in_array($character, ['<', '(', '[', '{'])) {
|
||||
$nestingLevel++;
|
||||
}
|
||||
if (in_array($character, ['>', ')', ']', '}'])) {
|
||||
$nestingLevel--;
|
||||
}
|
||||
}
|
||||
|
||||
$description = trim(substr($body, strlen($type)));
|
||||
|
||||
return [$type, $description];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of phpDocumentor.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright 2010-2015 Mike van Riel<[email protected]>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
||||
* @link http://phpdoc.org
|
||||
*/
|
||||
|
||||
namespace phpDocumentor\Reflection\DocBlock\Tags;
|
||||
|
||||
use phpDocumentor\Reflection\Type;
|
||||
|
||||
abstract class TagWithType extends BaseTag
|
||||
{
|
||||
/** @var Type */
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Returns the type section of the variable.
|
||||
*
|
||||
* @return Type
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
protected static function extractTypeFromBody(string $body) : array
|
||||
{
|
||||
$type = '';
|
||||
$nestingLevel = 0;
|
||||
for ($i = 0; $i < strlen($body); $i++) {
|
||||
$character = $body[$i];
|
||||
|
||||
if (trim($character) === '' && $nestingLevel === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$type .= $character;
|
||||
if (in_array($character, ['<', '(', '[', '{'])) {
|
||||
$nestingLevel++;
|
||||
}
|
||||
if (in_array($character, ['>', ')', ']', '}'])) {
|
||||
$nestingLevel--;
|
||||
}
|
||||
}
|
||||
|
||||
$description = trim(substr($body, strlen($type)));
|
||||
|
||||
return [$type, $description];
|
||||
}
|
||||
}
|
||||
@@ -24,17 +24,12 @@ use function preg_split;
|
||||
/**
|
||||
* Reflection class for a {@}throws tag in a Docblock.
|
||||
*/
|
||||
final class Throws extends BaseTag implements Factory\StaticMethod
|
||||
final class Throws extends TagWithType implements Factory\StaticMethod
|
||||
{
|
||||
/** @var string */
|
||||
protected $name = 'throws';
|
||||
|
||||
/** @var Type */
|
||||
private $type;
|
||||
|
||||
public function __construct(Type $type, ?Description $description = null)
|
||||
public function __construct(Type $type, Description $description = null)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->name = 'throws';
|
||||
$this->type = $type;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
@@ -50,23 +45,14 @@ final class Throws extends BaseTag implements Factory\StaticMethod
|
||||
Assert::notNull($typeResolver);
|
||||
Assert::notNull($descriptionFactory);
|
||||
|
||||
$parts = preg_split('/\s+/Su', $body, 2);
|
||||
Assert::isArray($parts);
|
||||
list($type, $description) = self::extractTypeFromBody($body);
|
||||
|
||||
$type = $typeResolver->resolve($parts[0] ?? '', $context);
|
||||
$description = $descriptionFactory->create($parts[1] ?? '', $context);
|
||||
$type = $typeResolver->resolve($type, $context);
|
||||
$description = $descriptionFactory->create($description, $context);
|
||||
|
||||
return new static($type, $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type section of the variable.
|
||||
*/
|
||||
public function getType() : Type
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
return (string) $this->type . ' ' . (string) $this->description;
|
||||
|
||||
Reference in New Issue
Block a user