DocBlock   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 32
c 2
b 0
f 0
dl 0
loc 103
rs 10
ccs 32
cts 32
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createDocBlock() 0 8 2
A getTagsByName() 0 19 4
A getTags() 0 23 5
A hasTag() 0 7 2
A __construct() 0 3 1
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
The method getDescription() does not exist on phpDocumentor\Reflection\DocBlock\Tag. It seems like you code against a sub-type of said class. However, the method does not exist in phpDocumentor\Reflection\DocBlock\Tags\InvalidTag. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            if ($tag->/** @scrutinizer ignore-call */ getDescription()) {
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
}