Deployee /
docblock
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace Deployee\Components\DocBlock; |
||
| 5 | |||
| 6 | |||
| 7 | use phpDocumentor\Reflection\DocBlock\Tags\Generic; |
||
| 8 | use phpDocumentor\Reflection\DocBlockFactory; |
||
| 9 | use phpDocumentor\Reflection\DocBlockFactoryInterface; |
||
| 10 | |||
| 11 | class DocBlock implements DocBlockInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var DocBlockFactoryInterface |
||
| 15 | */ |
||
| 16 | private $factory; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var |
||
| 20 | */ |
||
| 21 | private static $blocks = []; |
||
| 22 | |||
| 23 | 3 | public function __construct() |
|
| 24 | { |
||
| 25 | 3 | $this->factory = DocBlockFactory::createInstance(); |
|
| 26 | 3 | } |
|
| 27 | |||
| 28 | /** |
||
| 29 | * @param string $class |
||
| 30 | * @return array |
||
| 31 | * @throws \ReflectionException |
||
| 32 | */ |
||
| 33 | 3 | public function getTags(string $class): array |
|
| 34 | { |
||
| 35 | 3 | $return = []; |
|
| 36 | try { |
||
| 37 | 3 | $tags = $this->createDocBlock($class)->getTags(); |
|
| 38 | } |
||
| 39 | 2 | catch(\InvalidArgumentException $e){ |
|
| 40 | 1 | return []; |
|
| 41 | } |
||
| 42 | |||
| 43 | /* @var Generic $tag */ |
||
| 44 | 1 | foreach ($tags as $tag) { |
|
| 45 | 1 | if (!isset($return[$tag->getName()])) { |
|
| 46 | 1 | $return[$tag->getName()] = []; |
|
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | 1 | if ($tag->getDescription()) { |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 51 | 1 | $return[$tag->getName()][] = $tag->getDescription()->render(); |
|
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | 1 | return $return; |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $class |
||
| 60 | * @param string $tagName |
||
| 61 | * @return array |
||
| 62 | * @throws \ReflectionException |
||
| 63 | */ |
||
| 64 | 2 | public function getTagsByName(string $class, string $tagName): array |
|
| 65 | { |
||
| 66 | 2 | $return = []; |
|
| 67 | |||
| 68 | try { |
||
| 69 | 2 | $tags = $this->createDocBlock($class)->getTagsByName($tagName); |
|
| 70 | } |
||
| 71 | 1 | catch(\InvalidArgumentException $e){ |
|
| 72 | 1 | return []; |
|
| 73 | } |
||
| 74 | |||
| 75 | /* @var Generic $tag */ |
||
| 76 | 1 | foreach ($tags as $tag) { |
|
| 77 | 1 | if($tag->getDescription()) { |
|
| 78 | 1 | $return[] = $tag->getDescription()->render(); |
|
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | 1 | return $return; |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $class |
||
| 87 | * @param string $tagName |
||
| 88 | * @return bool |
||
| 89 | * @throws \ReflectionException |
||
| 90 | */ |
||
| 91 | 2 | public function hasTag(string $class, string $tagName): bool |
|
| 92 | { |
||
| 93 | try { |
||
| 94 | 2 | return $this->createDocBlock($class)->hasTag($tagName); |
|
| 95 | } |
||
| 96 | 1 | catch(\InvalidArgumentException $e){ |
|
| 97 | 1 | return false; |
|
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $class |
||
| 103 | * @return \phpDocumentor\Reflection\DocBlock |
||
| 104 | * @throws \ReflectionException |
||
| 105 | */ |
||
| 106 | 3 | private function createDocBlock(string $class): \phpDocumentor\Reflection\DocBlock |
|
| 107 | { |
||
| 108 | 3 | $hash = sha1($class); |
|
| 109 | 3 | if(!isset($this->blocks[$hash])){ |
|
| 110 | 3 | self::$blocks[$hash] = $this->factory->create(new \ReflectionClass($class)); |
|
| 111 | } |
||
| 112 | |||
| 113 | 1 | return self::$blocks[$hash]; |
|
| 114 | } |
||
| 115 | } |