Skip to content

Commit e516af3

Browse files
committed
Allow only tags in Docblock argument
The array of tags in the docblock constructor should only contain tags. Some of the factory methods of the tags are returning a null when the tag could not be created. This causes issues during parsing. By filtering these null values in the factory this use is resolved.
1 parent 1f637ff commit e516af3

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/DocBlock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct(
6161
Assert::string($summary);
6262
Assert::boolean($isTemplateStart);
6363
Assert::boolean($isTemplateEnd);
64+
Assert::allIsInstanceOf($tags, Tag::class);
6465

6566
$this->summary = $summary;
6667
$this->description = $description ?: new DocBlock\Description('');

src/DocBlockFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
1616
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
17+
use phpDocumentor\Reflection\DocBlock\Tag;
1718
use phpDocumentor\Reflection\DocBlock\TagFactory;
1819
use phpDocumentor\Reflection\Types\Context;
1920
use Webmozart\Assert\Assert;
@@ -93,7 +94,9 @@ public function create($docblock, Types\Context $context = null, Location $locat
9394
return new DocBlock(
9495
$summary,
9596
$description ? $this->descriptionFactory->create($description, $context) : null,
96-
$this->parseTagBlock($tags, $context),
97+
array_filter($this->parseTagBlock($tags, $context), function($tag) {
98+
return $tag instanceof Tag;
99+
}),
97100
$context,
98101
$location,
99102
$templateMarker === '#@+',

tests/unit/DocBlockFactoryTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,52 @@ public function provideSummaryAndDescriptions()
239239
/**
240240
* @covers ::__construct
241241
* @covers ::create
242+
*
242243
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
243244
* @uses phpDocumentor\Reflection\DocBlock\Description
244245
* @uses phpDocumentor\Reflection\Types\Context
245246
* @uses phpDocumentor\Reflection\DocBlock\Tags\Param
246247
*/
247248
public function testTagsWithContextNamespace()
248249
{
249-
$tagFactoryMock = m::mock(TagFactory::class);
250+
$tagFactoryMock = m::mock(TagFactory::class);
250251
$fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), $tagFactoryMock);
251252
$context = new Context('MyNamespace');
252253

253254
$tagFactoryMock->shouldReceive('create')->with(m::any(), $context)->andReturn(new Param('param'));
254255
$docblock = $fixture->create('/** @param MyType $param */', $context);
255256
}
257+
258+
/**
259+
* @covers ::__construct
260+
* @covers ::create
261+
*
262+
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
263+
* @uses phpDocumentor\Reflection\DocBlock\Description
264+
*/
265+
public function testTagsAreFilteredForNullValues()
266+
{
267+
$tagString = <<<TAG
268+
@author Mike van Riel <[email protected]> This is with
269+
multiline description.
270+
TAG;
271+
272+
$tagFactory = m::mock(TagFactory::class);
273+
$tagFactory->shouldReceive('create')->with($tagString, m::any())->andReturn(null);
274+
275+
$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);
276+
277+
$given = <<<DOCBLOCK
278+
/**
279+
* This is a summary.
280+
*
281+
* @author Mike van Riel <[email protected]> This is with
282+
* multiline description.
283+
*/
284+
DOCBLOCK;
285+
286+
$docblock = $fixture->create($given, new Context(''));
287+
288+
$this->assertEquals([], $docblock->getTags());
289+
}
256290
}

tests/unit/DocBlockTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ public function testDocBlockCanHaveTags()
100100
$this->assertSame($tags, $fixture->getTags());
101101
}
102102

103+
/**
104+
* @covers ::__construct
105+
* @covers ::getTags
106+
*
107+
* @uses \phpDocumentor\Reflection\DocBlock\Description
108+
* @uses \phpDocumentor\Reflection\DocBlock\Tag
109+
*
110+
* @expectedException \InvalidArgumentException
111+
*/
112+
public function testDocBlockAllowsOnlyTags()
113+
{
114+
$tags = [
115+
null
116+
];
117+
118+
$fixture = new DocBlock('', null, $tags);
119+
}
120+
103121
/**
104122
* @covers ::__construct
105123
* @covers ::getTagsByName

0 commit comments

Comments
 (0)