Merge pull request #260 from voku/get_tags_with_type_by_name

Get tags with type by name
This commit is contained in:
Jaap van Otterdijk
2021-04-23 11:50:05 +02:00
committed by GitHub
2 changed files with 48 additions and 1 deletions
+24
View File
@@ -14,6 +14,7 @@ declare(strict_types=1);
namespace phpDocumentor\Reflection; namespace phpDocumentor\Reflection;
use phpDocumentor\Reflection\DocBlock\Tag; use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
final class DocBlock final class DocBlock
@@ -161,6 +162,29 @@ final class DocBlock
return $result; 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. * Checks if a tag of a certain type is present in this DocBlock.
* *
+24 -1
View File
@@ -16,12 +16,13 @@ namespace phpDocumentor\Reflection;
use Mockery as m; use Mockery as m;
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated; use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** /**
* @uses \Webmozart\Assert\Assert * @uses \Webmozart\Assert\Assert
* *
* @coversDefaultClass phpDocumentor\Reflection\DocBlock * @coversDefaultClass \phpDocumentor\Reflection\DocBlock
* @covers ::<private> * @covers ::<private>
*/ */
class DocBlockTest extends TestCase class DocBlockTest extends TestCase
@@ -138,6 +139,28 @@ class DocBlockTest extends TestCase
$this->assertSame([], $fixture->getTagsByName('Ebcd')); $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::getTags
* @uses \phpDocumentor\Reflection\DocBlock\Description * @uses \phpDocumentor\Reflection\DocBlock\Description