Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Get tags with type by name #260

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/DocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/DocBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::<private>
*/
class DocBlockTest extends TestCase
Expand Down Expand Up @@ -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
Expand Down