Move type extraction to base class and re-use in Throws

This commit is contained in:
Mike van Riel
2019-12-27 20:59:58 +01:00
parent 0a9b8ea383
commit 506fd89d43
4 changed files with 165 additions and 68 deletions
+6 -44
View File
@@ -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];
}
}
+59
View File
@@ -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];
}
}
+7 -21
View File
@@ -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;