Skip to content

Commit 506fd89

Browse files
committed
Move type extraction to base class and re-use in Throws
1 parent 0a9b8ea commit 506fd89

File tree

4 files changed

+165
-68
lines changed

4 files changed

+165
-68
lines changed

src/DocBlock/Tags/Return_.php

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,12 @@
2424
/**
2525
* Reflection class for a {@}return tag in a Docblock.
2626
*/
27-
final class Return_ extends BaseTag implements Factory\StaticMethod
27+
final class Return_ extends TagWithType implements Factory\StaticMethod
2828
{
29-
/** @var string */
30-
protected $name = 'return';
31-
32-
/** @var Type */
33-
private $type;
34-
35-
public function __construct(Type $type, ?Description $description = null)
29+
public function __construct(Type $type, Description $description = null)
3630
{
37-
$this->type = $type;
31+
$this->name = 'return';
32+
$this->type = $type;
3833
$this->description = $description;
3934
}
4035

@@ -50,49 +45,16 @@ public static function create(
5045
Assert::notNull($typeResolver);
5146
Assert::notNull($descriptionFactory);
5247

53-
list($type, $description) = self::splitBodyIntoTypeAndTheRest($body);
48+
list($type, $description) = self::extractTypeFromBody($body);
5449

5550
$type = $typeResolver->resolve($type, $context);
5651
$description = $descriptionFactory->create($description, $context);
5752

5853
return new static($type, $description);
5954
}
6055

61-
/**
62-
* Returns the type section of the variable.
63-
*/
64-
public function getType() : Type
65-
{
66-
return $this->type;
67-
}
68-
69-
public function __toString() : string
56+
public function __toString() :string
7057
{
7158
return $this->type . ' ' . (string) $this->description;
7259
}
73-
74-
private static function splitBodyIntoTypeAndTheRest(string $body) : array
75-
{
76-
$type = '';
77-
$nestingLevel = 0;
78-
for ($i = 0; $i < strlen($body); $i++) {
79-
$character = $body[$i];
80-
81-
if (trim($character) === '' && $nestingLevel === 0) {
82-
break;
83-
}
84-
85-
$type .= $character;
86-
if (in_array($character, ['<', '(', '[', '{'])) {
87-
$nestingLevel++;
88-
}
89-
if (in_array($character, ['>', ')', ']', '}'])) {
90-
$nestingLevel--;
91-
}
92-
}
93-
94-
$description = trim(substr($body, strlen($type)));
95-
96-
return [$type, $description];
97-
}
9860
}

src/DocBlock/Tags/TagWithType.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @copyright 2010-2015 Mike van Riel<[email protected]>
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT
13+
* @link http://phpdoc.org
14+
*/
15+
16+
namespace phpDocumentor\Reflection\DocBlock\Tags;
17+
18+
use phpDocumentor\Reflection\Type;
19+
20+
abstract class TagWithType extends BaseTag
21+
{
22+
/** @var Type */
23+
protected $type;
24+
25+
/**
26+
* Returns the type section of the variable.
27+
*
28+
* @return Type
29+
*/
30+
public function getType()
31+
{
32+
return $this->type;
33+
}
34+
35+
protected static function extractTypeFromBody(string $body) : array
36+
{
37+
$type = '';
38+
$nestingLevel = 0;
39+
for ($i = 0; $i < strlen($body); $i++) {
40+
$character = $body[$i];
41+
42+
if (trim($character) === '' && $nestingLevel === 0) {
43+
break;
44+
}
45+
46+
$type .= $character;
47+
if (in_array($character, ['<', '(', '[', '{'])) {
48+
$nestingLevel++;
49+
}
50+
if (in_array($character, ['>', ')', ']', '}'])) {
51+
$nestingLevel--;
52+
}
53+
}
54+
55+
$description = trim(substr($body, strlen($type)));
56+
57+
return [$type, $description];
58+
}
59+
}

src/DocBlock/Tags/Throws.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,12 @@
2424
/**
2525
* Reflection class for a {@}throws tag in a Docblock.
2626
*/
27-
final class Throws extends BaseTag implements Factory\StaticMethod
27+
final class Throws extends TagWithType implements Factory\StaticMethod
2828
{
29-
/** @var string */
30-
protected $name = 'throws';
31-
32-
/** @var Type */
33-
private $type;
34-
35-
public function __construct(Type $type, ?Description $description = null)
29+
public function __construct(Type $type, Description $description = null)
3630
{
37-
$this->type = $type;
31+
$this->name = 'throws';
32+
$this->type = $type;
3833
$this->description = $description;
3934
}
4035

@@ -50,23 +45,14 @@ public static function create(
5045
Assert::notNull($typeResolver);
5146
Assert::notNull($descriptionFactory);
5247

53-
$parts = preg_split('/\s+/Su', $body, 2);
54-
Assert::isArray($parts);
48+
list($type, $description) = self::extractTypeFromBody($body);
5549

56-
$type = $typeResolver->resolve($parts[0] ?? '', $context);
57-
$description = $descriptionFactory->create($parts[1] ?? '', $context);
50+
$type = $typeResolver->resolve($type, $context);
51+
$description = $descriptionFactory->create($description, $context);
5852

5953
return new static($type, $description);
6054
}
6155

62-
/**
63-
* Returns the type section of the variable.
64-
*/
65-
public function getType() : Type
66-
{
67-
return $this->type;
68-
}
69-
7056
public function __toString() : string
7157
{
7258
return (string) $this->type . ' ' . (string) $this->description;

tests/unit/DocBlock/Tags/ThrowsTest.php

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public function testStringRepresentationIsReturned() : void
134134
public function testFactoryMethod() : void
135135
{
136136
$descriptionFactory = m::mock(DescriptionFactory::class);
137-
$resolver = new TypeResolver();
138-
$context = new Context('');
137+
$resolver = new TypeResolver();
138+
$context = new Context('');
139139

140-
$type = new String_();
140+
$type = new String_();
141141
$description = new Description('My Description');
142142
$descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
143143

@@ -148,6 +148,96 @@ public function testFactoryMethod() : void
148148
$this->assertSame($description, $fixture->getDescription());
149149
}
150150

151+
/**
152+
* This test checks whether a braces in a Type are allowed.
153+
*
154+
* 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
155+
* could purely rely on spaces to split the individual parts of the body of a tag; but when there is a type in play
156+
* we now need to check for braces.
157+
*
158+
* This test tests whether an error occurs demonstrating that the braces were taken into account; this test is still
159+
* expected to produce an exception because the TypeResolver does not support generics.
160+
*
161+
* @covers ::create
162+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
163+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
164+
* @uses \phpDocumentor\Reflection\TypeResolver
165+
* @uses \phpDocumentor\Reflection\DocBlock\Description
166+
* @uses \phpDocumentor\Reflection\Types\String_
167+
* @uses \phpDocumentor\Reflection\Types\Context
168+
*/
169+
public function testFactoryMethodWithGenericWithSpace()
170+
{
171+
$this->expectException(\InvalidArgumentException::class);
172+
$this->expectExceptionMessage('"\array<string, string>" is not a valid Fqsen.');
173+
174+
$descriptionFactory = m::mock(DescriptionFactory::class);
175+
$resolver = new TypeResolver();
176+
$context = new Context('');
177+
178+
$description = new Description('My Description');
179+
$descriptionFactory->shouldReceive('create')
180+
->with('My Description', $context)
181+
->andReturn($description);
182+
183+
Throws::create('array<string, string> My Description', $resolver, $descriptionFactory, $context);
184+
}
185+
186+
/**
187+
* @see self::testFactoryMethodWithGenericWithSpace()
188+
*
189+
* @covers ::create
190+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
191+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
192+
* @uses \phpDocumentor\Reflection\TypeResolver
193+
* @uses \phpDocumentor\Reflection\DocBlock\Description
194+
* @uses \phpDocumentor\Reflection\Types\String_
195+
* @uses \phpDocumentor\Reflection\Types\Context
196+
*/
197+
public function testFactoryMethodWithGenericWithSpaceAndAddedEmojisToVerifyMultiByteBehaviour()
198+
{
199+
$this->expectException(\InvalidArgumentException::class);
200+
$this->expectExceptionMessage('"\array😁<string,😁 😁string>" is not a valid Fqsen.');
201+
202+
$descriptionFactory = m::mock(DescriptionFactory::class);
203+
$resolver = new TypeResolver();
204+
$context = new Context('');
205+
206+
$description = new Description('My Description');
207+
$descriptionFactory->shouldReceive('create')
208+
->with('My Description', $context)
209+
->andReturn($description);
210+
211+
Throws::create('array😁<string,😁 😁string> My Description', $resolver, $descriptionFactory, $context);
212+
}
213+
214+
/**
215+
* @covers ::create
216+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
217+
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
218+
* @uses \phpDocumentor\Reflection\TypeResolver
219+
* @uses \phpDocumentor\Reflection\DocBlock\Description
220+
* @uses \phpDocumentor\Reflection\Types\String_
221+
* @uses \phpDocumentor\Reflection\Types\Context
222+
*/
223+
public function testFactoryMethodWithEmojisToVerifyMultiByteBehaviour()
224+
{
225+
$descriptionFactory = m::mock(DescriptionFactory::class);
226+
$resolver = new TypeResolver();
227+
$context = new Context('');
228+
229+
$description = new Description('My Description');
230+
$descriptionFactory->shouldReceive('create')
231+
->with('My Description', $context)
232+
->andReturn($description);
233+
234+
$fixture = Throws::create('\My😁Class My Description', $resolver, $descriptionFactory, $context);
235+
236+
$this->assertSame('\My😁Class My Description', (string) $fixture);
237+
$this->assertEquals('\My😁Class', $fixture->getType());
238+
$this->assertSame($description, $fixture->getDescription());
239+
}
240+
151241
/**
152242
* @covers ::create
153243
*/

0 commit comments

Comments
 (0)