Skip to content

Commit 8c3953e

Browse files
committed
"DocBlock" -> add "getTagsWithTypeByName()"
-> because I saw errors like this ```Call to undefined method phpDocumentor\Reflection\DocBlock\Tags\InvalidTag::getType()``` -> https://github.com/Roave/BetterReflection/blob/5b4d1c53768e2a3bc32ab348752663733fd70546/src/TypesFinder/FindReturnType.php#L53
1 parent 069a785 commit 8c3953e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/DocBlock.php

Lines changed: 22 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,27 @@ 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+
$result[] = $tag;
180+
}
181+
}
182+
183+
return $result;
184+
}
185+
164186
/**
165187
* Checks if a tag of a certain type is present in this DocBlock.
166188
*

tests/unit/DocBlockTest.php

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

1616
use Mockery as m;
17+
use phpDocumentor\Reflection\DocBlock\Tags\Author;
1718
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
1819
use phpDocumentor\Reflection\Types\Context;
20+
use phpDocumentor\Reflection\Types\String_;
1921
use PHPUnit\Framework\TestCase;
2022

2123
/**
@@ -138,6 +140,28 @@ public function testFindTagsInDocBlockByName() : void
138140
$this->assertSame([], $fixture->getTagsByName('Ebcd'));
139141
}
140142

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

0 commit comments

Comments
 (0)