diff --git a/src/DocBlock.php b/src/DocBlock.php index f3403d6e..06c0b5dd 100644 --- a/src/DocBlock.php +++ b/src/DocBlock.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Reflection; use phpDocumentor\Reflection\DocBlock\Tag; +use phpDocumentor\Reflection\DocBlock\Tags\TagWithType; use Webmozart\Assert\Assert; final class DocBlock @@ -161,6 +162,29 @@ public function getTagsByName(string $name) : array return $result; } + /** + * Returns an array of tags with type matching the given name. If no tags are found + * an empty array is returned. + * + * @param string $name String to search by. + * + * @return TagWithType[] + */ + public function getTagsWithTypeByName(string $name) : array + { + $result = []; + + foreach ($this->getTagsByName($name) as $tag) { + if (!$tag instanceof TagWithType) { + continue; + } + + $result[] = $tag; + } + + return $result; + } + /** * Checks if a tag of a certain type is present in this DocBlock. * diff --git a/tests/unit/DocBlockTest.php b/tests/unit/DocBlockTest.php index 3d4923a8..af31e73e 100644 --- a/tests/unit/DocBlockTest.php +++ b/tests/unit/DocBlockTest.php @@ -16,12 +16,13 @@ use Mockery as m; use phpDocumentor\Reflection\DocBlock\Tags\Deprecated; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; /** * @uses \Webmozart\Assert\Assert * - * @coversDefaultClass phpDocumentor\Reflection\DocBlock + * @coversDefaultClass \phpDocumentor\Reflection\DocBlock * @covers :: */ class DocBlockTest extends TestCase @@ -138,6 +139,28 @@ public function testFindTagsInDocBlockByName() : void $this->assertSame([], $fixture->getTagsByName('Ebcd')); } + /** + * @uses \phpDocumentor\Reflection\DocBlock::getTags + * @uses \phpDocumentor\Reflection\DocBlock\Description + * @uses \phpDocumentor\Reflection\DocBlock\Tag + * + * @covers ::__construct + * @covers ::getTagsWithTypeByName + */ + public function testFindTagsWithTypeInDocBlockByName() : void + { + $tag1 = new DocBlock\Tags\Var_('foo', new String_()); + $tag2 = new DocBlock\Tags\Var_('bar', new String_()); + $tag3 = new DocBlock\Tags\Return_(new String_()); + $tag4 = new DocBlock\Tags\Author('lall', ''); + + $fixture = new DocBlock('', null, [$tag1, $tag2, $tag3, $tag4]); + + $this->assertSame([$tag1, $tag2], $fixture->getTagsWithTypeByName('var')); + $this->assertSame([$tag3], $fixture->getTagsWithTypeByName('return')); + $this->assertSame([], $fixture->getTagsWithTypeByName('author')); + } + /** * @uses \phpDocumentor\Reflection\DocBlock::getTags * @uses \phpDocumentor\Reflection\DocBlock\Description