Skip to content

Commit bdecc2d

Browse files
committed
Fix issue when processing invalid tags
When invalid tags are processed a null was returned causing all kind of issues in the normal behavior of this libary. As a solution a generic tag could be created. But that would just drop the error information in. Therefore a new tag was introduced, `invalidTag` the tag is just like the generic tag but does contain the error triggered during the creation of the tag. Which might help applications like phpdocumentor to display validation issues.
1 parent 43f90ab commit bdecc2d

File tree

7 files changed

+106
-48
lines changed

7 files changed

+106
-48
lines changed

src/DocBlock/DescriptionFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ public function create(string $contents, ?TypeContext $context = null) : Descrip
6969
$tags = [];
7070

7171
for ($i = 1; $i < $count; $i += 2) {
72-
$tag = $this->tagFactory->create($tokens[$i], $context);
73-
if ($tag !== null) {
74-
$tags[] = $tag;
75-
}
72+
$tags[] = $this->tagFactory->create($tokens[$i], $context);
7673
$tokens[$i] = '%' . ++$tagCount . '$s';
7774
}
7875

src/DocBlock/StandardTagFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
2020
use phpDocumentor\Reflection\DocBlock\Tags\Factory\StaticMethod;
2121
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
22+
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
2223
use phpDocumentor\Reflection\DocBlock\Tags\Link as LinkTag;
2324
use phpDocumentor\Reflection\DocBlock\Tags\Method;
2425
use phpDocumentor\Reflection\DocBlock\Tags\Param;
@@ -138,7 +139,7 @@ public function __construct(FqsenResolver $fqsenResolver, ?array $tagHandlers =
138139
/**
139140
* {@inheritDoc}
140141
*/
141-
public function create(string $tagLine, ?TypeContext $context = null) : ?Tag
142+
public function create(string $tagLine, ?TypeContext $context = null) : Tag
142143
{
143144
if (!$context) {
144145
$context = new TypeContext('');
@@ -215,7 +216,7 @@ private function extractTagParts(string $tagLine) : array
215216
* Creates a new tag object with the given name and body or returns null if the tag name was recognized but the
216217
* body was invalid.
217218
*/
218-
private function createTag(string $body, string $name, TypeContext $context) : ?Tag
219+
private function createTag(string $body, string $name, TypeContext $context) : Tag
219220
{
220221
$handlerClassName = $this->findHandlerClassName($name, $context);
221222
$arguments = $this->getArgumentsForParametersFromWiring(
@@ -228,7 +229,7 @@ private function createTag(string $body, string $name, TypeContext $context) : ?
228229
$callable = [$handlerClassName, 'create'];
229230
return call_user_func_array($callable, $arguments);
230231
} catch (InvalidArgumentException $e) {
231-
return null;
232+
return InvalidTag::create($body, $name, $e);
232233
}
233234
}
234235

src/DocBlock/TagFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function addParameter(string $name, $value) : void;
4949
*
5050
* @throws InvalidArgumentException If an invalid tag line was presented.
5151
*/
52-
public function create(string $tagLine, ?TypeContext $context = null) : ?Tag;
52+
public function create(string $tagLine, ?TypeContext $context = null) : Tag;
5353

5454
/**
5555
* Registers a service with the Service Locator using the FQCN of the class or the alias, if provided.

src/DocBlock/Tags/InvalidTag.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags;
6+
7+
use phpDocumentor\Reflection\DocBlock\Tag;
8+
use Throwable;
9+
use Webmozart\Assert\Assert;
10+
11+
/**
12+
* This class represents an exception during the tag creation
13+
*
14+
* Since the internals of the library are relaying on the correct syntax of a docblock
15+
* we cannot simply throw exceptions at all time because the exceptions will break the creation of a
16+
* docklock. Just silently ignore the exceptions is not an option because the user as an issue to fix.
17+
*
18+
* This tag holds that error information until a using application is able to display it. The object wil just behave
19+
* like any normal tag. So the normal application flow will not break.
20+
*/
21+
final class InvalidTag implements Tag
22+
{
23+
/** @var string */
24+
private $name;
25+
26+
/** @var string */
27+
private $body;
28+
29+
/** @var Throwable */
30+
private $throwable;
31+
32+
private function __construct(string $name, string $body, Throwable $throwable)
33+
{
34+
$this->name = $name;
35+
$this->body = $body;
36+
$this->throwable = $throwable;
37+
}
38+
39+
public function getException() : Throwable
40+
{
41+
return $this->throwable;
42+
}
43+
44+
public function getName() : string
45+
{
46+
return $this->name;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public static function create(string $body, string $name = '', ?Throwable $exception = null)
53+
{
54+
Assert::notNull($exception);
55+
56+
return new self($name, $body, $exception);
57+
}
58+
59+
public function render(?Formatter $formatter = null) : string
60+
{
61+
if ($formatter === null) {
62+
$formatter = new Formatter\PassthroughFormatter();
63+
}
64+
65+
return $formatter->format($this);
66+
}
67+
68+
public function __toString() : string
69+
{
70+
return $this->body;
71+
}
72+
}

src/DocBlockFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use LogicException;
1818
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
1919
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
20-
use phpDocumentor\Reflection\DocBlock\Tag;
2120
use phpDocumentor\Reflection\DocBlock\TagFactory;
2221
use Webmozart\Assert\Assert;
2322
use function array_shift;
@@ -236,12 +235,7 @@ private function parseTagBlock(string $tags, Types\Context $context) : array
236235
$result = [];
237236
$lines = $this->splitTagBlockIntoTagLines($tags);
238237
foreach ($lines as $key => $tagLine) {
239-
$tag = $this->tagFactory->create(trim($tagLine), $context);
240-
if (!($tag instanceof Tag)) {
241-
continue;
242-
}
243-
244-
$result[$key] = $tag;
238+
$result[$key] = $this->tagFactory->create(trim($tagLine), $context);
245239
}
246240

247241
return $result;

tests/unit/DocBlock/DescriptionFactoryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace phpDocumentor\Reflection\DocBlock;
1515

16+
use Exception;
1617
use Mockery as m;
18+
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
1719
use phpDocumentor\Reflection\DocBlock\Tags\Link as LinkTag;
1820
use phpDocumentor\Reflection\Types\Context;
1921
use PHPUnit\Framework\TestCase;
@@ -162,6 +164,31 @@ public function testIfSuperfluousStartingSpacesAreRemoved() : void
162164
$this->assertSame($expectedDescription, $description->render());
163165
}
164166

167+
/**
168+
* @uses \phpDocumentor\Reflection\DocBlock\Description
169+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
170+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
171+
* @uses \phpDocumentor\Reflection\Types\Context
172+
*
173+
* @covers ::__construct
174+
* @covers ::create
175+
*/
176+
public function testDescriptionWithBrokenInlineTags() : void
177+
{
178+
$contents = 'This {@see $name} is a broken use case, but used in real life.';
179+
$context = new Context('');
180+
$tagFactory = m::mock(TagFactory::class);
181+
$tagFactory->shouldReceive('create')
182+
->once()
183+
->with('@see $name', $context)
184+
->andReturn(InvalidTag::create('$name', 'see', new Exception()));
185+
186+
$factory = new DescriptionFactory($tagFactory);
187+
$description = $factory->create($contents, $context);
188+
189+
$this->assertSame($contents, $description->render());
190+
}
191+
165192
/**
166193
* Provides a series of example strings that the parser should correctly interpret and return.
167194
*

tests/unit/DocBlockFactoryTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -279,37 +279,4 @@ public function testTagsWithContextNamespace() : void
279279

280280
$this->assertInstanceOf(DocBlock::class, $docblock);
281281
}
282-
283-
/**
284-
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
285-
* @uses \phpDocumentor\Reflection\DocBlock\Description
286-
*
287-
* @covers ::__construct
288-
* @covers ::create
289-
*/
290-
public function testTagsAreFilteredForNullValues() : void
291-
{
292-
$tagString = <<<TAG
293-
@author Mike van Riel <[email protected]> This is with
294-
multiline description.
295-
TAG;
296-
297-
$tagFactory = m::mock(TagFactory::class);
298-
$tagFactory->shouldReceive('create')->with($tagString, m::any())->andReturn(null);
299-
300-
$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);
301-
302-
$given = <<<DOCBLOCK
303-
/**
304-
* This is a summary.
305-
*
306-
* @author Mike van Riel <[email protected]> This is with
307-
* multiline description.
308-
*/
309-
DOCBLOCK;
310-
311-
$docblock = $fixture->create($given, new Context(''));
312-
313-
$this->assertEquals([], $docblock->getTags());
314-
}
315282
}

0 commit comments

Comments
 (0)