Skip to content

Commit 99e3fb8

Browse files
authored
Merge pull request #260 from voku/get_tags_with_type_by_name
Get tags with type by name
2 parents f8d350d + 591f593 commit 99e3fb8

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/DocBlock.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace phpDocumentor\Reflection;
1515

1616
use phpDocumentor\Reflection\DocBlock\Tag;
17+
use phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
1718
use Webmozart\Assert\Assert;
1819

1920
final class DocBlock
@@ -161,6 +162,29 @@ public function getTagsByName(string $name) : array
161162
return $result;
162163
}
163164

165+
/**
166+
* Returns an array of tags with type matching the given name. If no tags are found
167+
* an empty array is returned.
168+
*
169+
* @param string $name String to search by.
170+
*
171+
* @return TagWithType[]
172+
*/
173+
public function getTagsWithTypeByName(string $name) : array
174+
{
175+
$result = [];
176+
177+
foreach ($this->getTagsByName($name) as $tag) {
178+
if (!$tag instanceof TagWithType) {
179+
continue;
180+
}
181+
182+
$result[] = $tag;
183+
}
184+
185+
return $result;
186+
}
187+
164188
/**
165189
* Checks if a tag of a certain type is present in this DocBlock.
166190
*

tests/unit/DocBlockTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
use Mockery as m;
1717
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
1818
use phpDocumentor\Reflection\Types\Context;
19+
use phpDocumentor\Reflection\Types\String_;
1920
use PHPUnit\Framework\TestCase;
2021

2122
/**
2223
* @uses \Webmozart\Assert\Assert
2324
*
24-
* @coversDefaultClass phpDocumentor\Reflection\DocBlock
25+
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock
2526
* @covers ::<private>
2627
*/
2728
class DocBlockTest extends TestCase
@@ -138,6 +139,28 @@ public function testFindTagsInDocBlockByName() : void
138139
$this->assertSame([], $fixture->getTagsByName('Ebcd'));
139140
}
140141

142+
/**
143+
* @uses \phpDocumentor\Reflection\DocBlock::getTags
144+
* @uses \phpDocumentor\Reflection\DocBlock\Description
145+
* @uses \phpDocumentor\Reflection\DocBlock\Tag
146+
*
147+
* @covers ::__construct
148+
* @covers ::getTagsWithTypeByName
149+
*/
150+
public function testFindTagsWithTypeInDocBlockByName() : void
151+
{
152+
$tag1 = new DocBlock\Tags\Var_('foo', new String_());
153+
$tag2 = new DocBlock\Tags\Var_('bar', new String_());
154+
$tag3 = new DocBlock\Tags\Return_(new String_());
155+
$tag4 = new DocBlock\Tags\Author('lall', '');
156+
157+
$fixture = new DocBlock('', null, [$tag1, $tag2, $tag3, $tag4]);
158+
159+
$this->assertSame([$tag1, $tag2], $fixture->getTagsWithTypeByName('var'));
160+
$this->assertSame([$tag3], $fixture->getTagsWithTypeByName('return'));
161+
$this->assertSame([], $fixture->getTagsWithTypeByName('author'));
162+
}
163+
141164
/**
142165
* @uses \phpDocumentor\Reflection\DocBlock::getTags
143166
* @uses \phpDocumentor\Reflection\DocBlock\Description

0 commit comments

Comments
 (0)