mirror of
https://github.com/barryvdh/ReflectionDocBlock.git
synced 2026-08-18 18:13:13 +00:00
Move type extraction to base class and re-use in Throws
This commit is contained in:
@@ -24,16 +24,11 @@ use function preg_split;
|
|||||||
/**
|
/**
|
||||||
* Reflection class for a {@}return tag in a Docblock.
|
* 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 */
|
public function __construct(Type $type, Description $description = null)
|
||||||
protected $name = 'return';
|
|
||||||
|
|
||||||
/** @var Type */
|
|
||||||
private $type;
|
|
||||||
|
|
||||||
public function __construct(Type $type, ?Description $description = null)
|
|
||||||
{
|
{
|
||||||
|
$this->name = 'return';
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
$this->description = $description;
|
$this->description = $description;
|
||||||
}
|
}
|
||||||
@@ -50,7 +45,7 @@ final class Return_ extends BaseTag implements Factory\StaticMethod
|
|||||||
Assert::notNull($typeResolver);
|
Assert::notNull($typeResolver);
|
||||||
Assert::notNull($descriptionFactory);
|
Assert::notNull($descriptionFactory);
|
||||||
|
|
||||||
list($type, $description) = self::splitBodyIntoTypeAndTheRest($body);
|
list($type, $description) = self::extractTypeFromBody($body);
|
||||||
|
|
||||||
$type = $typeResolver->resolve($type, $context);
|
$type = $typeResolver->resolve($type, $context);
|
||||||
$description = $descriptionFactory->create($description, $context);
|
$description = $descriptionFactory->create($description, $context);
|
||||||
@@ -58,41 +53,8 @@ final class Return_ extends BaseTag implements Factory\StaticMethod
|
|||||||
return new static($type, $description);
|
return new static($type, $description);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function __toString() :string
|
||||||
* Returns the type section of the variable.
|
|
||||||
*/
|
|
||||||
public function getType() : Type
|
|
||||||
{
|
|
||||||
return $this->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __toString() : string
|
|
||||||
{
|
{
|
||||||
return $this->type . ' ' . (string) $this->description;
|
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,16 +24,11 @@ use function preg_split;
|
|||||||
/**
|
/**
|
||||||
* Reflection class for a {@}throws tag in a Docblock.
|
* 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 */
|
public function __construct(Type $type, Description $description = null)
|
||||||
protected $name = 'throws';
|
|
||||||
|
|
||||||
/** @var Type */
|
|
||||||
private $type;
|
|
||||||
|
|
||||||
public function __construct(Type $type, ?Description $description = null)
|
|
||||||
{
|
{
|
||||||
|
$this->name = 'throws';
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
$this->description = $description;
|
$this->description = $description;
|
||||||
}
|
}
|
||||||
@@ -50,23 +45,14 @@ final class Throws extends BaseTag implements Factory\StaticMethod
|
|||||||
Assert::notNull($typeResolver);
|
Assert::notNull($typeResolver);
|
||||||
Assert::notNull($descriptionFactory);
|
Assert::notNull($descriptionFactory);
|
||||||
|
|
||||||
$parts = preg_split('/\s+/Su', $body, 2);
|
list($type, $description) = self::extractTypeFromBody($body);
|
||||||
Assert::isArray($parts);
|
|
||||||
|
|
||||||
$type = $typeResolver->resolve($parts[0] ?? '', $context);
|
$type = $typeResolver->resolve($type, $context);
|
||||||
$description = $descriptionFactory->create($parts[1] ?? '', $context);
|
$description = $descriptionFactory->create($description, $context);
|
||||||
|
|
||||||
return new static($type, $description);
|
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 (string) $this->type . ' ' . (string) $this->description;
|
return (string) $this->type . ' ' . (string) $this->description;
|
||||||
|
|||||||
@@ -148,6 +148,96 @@ class ThrowsTest extends TestCase
|
|||||||
$this->assertSame($description, $fixture->getDescription());
|
$this->assertSame($description, $fixture->getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test checks whether a braces in a Type are allowed.
|
||||||
|
*
|
||||||
|
* The advent of generics poses a few issues, one of them is that spaces can now be part of a type. In the past we
|
||||||
|
* could purely rely on spaces to split the individual parts of the body of a tag; but when there is a type in play
|
||||||
|
* we now need to check for braces.
|
||||||
|
*
|
||||||
|
* This test tests whether an error occurs demonstrating that the braces were taken into account; this test is still
|
||||||
|
* expected to produce an exception because the TypeResolver does not support generics.
|
||||||
|
*
|
||||||
|
* @covers ::create
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
|
||||||
|
* @uses \phpDocumentor\Reflection\TypeResolver
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\Description
|
||||||
|
* @uses \phpDocumentor\Reflection\Types\String_
|
||||||
|
* @uses \phpDocumentor\Reflection\Types\Context
|
||||||
|
*/
|
||||||
|
public function testFactoryMethodWithGenericWithSpace()
|
||||||
|
{
|
||||||
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('"\array<string, string>" is not a valid Fqsen.');
|
||||||
|
|
||||||
|
$descriptionFactory = m::mock(DescriptionFactory::class);
|
||||||
|
$resolver = new TypeResolver();
|
||||||
|
$context = new Context('');
|
||||||
|
|
||||||
|
$description = new Description('My Description');
|
||||||
|
$descriptionFactory->shouldReceive('create')
|
||||||
|
->with('My Description', $context)
|
||||||
|
->andReturn($description);
|
||||||
|
|
||||||
|
Throws::create('array<string, string> My Description', $resolver, $descriptionFactory, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see self::testFactoryMethodWithGenericWithSpace()
|
||||||
|
*
|
||||||
|
* @covers ::create
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
|
||||||
|
* @uses \phpDocumentor\Reflection\TypeResolver
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\Description
|
||||||
|
* @uses \phpDocumentor\Reflection\Types\String_
|
||||||
|
* @uses \phpDocumentor\Reflection\Types\Context
|
||||||
|
*/
|
||||||
|
public function testFactoryMethodWithGenericWithSpaceAndAddedEmojisToVerifyMultiByteBehaviour()
|
||||||
|
{
|
||||||
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('"\array😁<string,😁 😁string>" is not a valid Fqsen.');
|
||||||
|
|
||||||
|
$descriptionFactory = m::mock(DescriptionFactory::class);
|
||||||
|
$resolver = new TypeResolver();
|
||||||
|
$context = new Context('');
|
||||||
|
|
||||||
|
$description = new Description('My Description');
|
||||||
|
$descriptionFactory->shouldReceive('create')
|
||||||
|
->with('My Description', $context)
|
||||||
|
->andReturn($description);
|
||||||
|
|
||||||
|
Throws::create('array😁<string,😁 😁string> My Description', $resolver, $descriptionFactory, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::create
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
|
||||||
|
* @uses \phpDocumentor\Reflection\TypeResolver
|
||||||
|
* @uses \phpDocumentor\Reflection\DocBlock\Description
|
||||||
|
* @uses \phpDocumentor\Reflection\Types\String_
|
||||||
|
* @uses \phpDocumentor\Reflection\Types\Context
|
||||||
|
*/
|
||||||
|
public function testFactoryMethodWithEmojisToVerifyMultiByteBehaviour()
|
||||||
|
{
|
||||||
|
$descriptionFactory = m::mock(DescriptionFactory::class);
|
||||||
|
$resolver = new TypeResolver();
|
||||||
|
$context = new Context('');
|
||||||
|
|
||||||
|
$description = new Description('My Description');
|
||||||
|
$descriptionFactory->shouldReceive('create')
|
||||||
|
->with('My Description', $context)
|
||||||
|
->andReturn($description);
|
||||||
|
|
||||||
|
$fixture = Throws::create('\My😁Class My Description', $resolver, $descriptionFactory, $context);
|
||||||
|
|
||||||
|
$this->assertSame('\My😁Class My Description', (string) $fixture);
|
||||||
|
$this->assertEquals('\My😁Class', $fixture->getType());
|
||||||
|
$this->assertSame($description, $fixture->getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::create
|
* @covers ::create
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user