Completed
Pull Request — master (#137)
by Tomáš
01:16
created

DocBlock   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 194
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
A getSummary() 0 4 1
A getDescription() 0 4 1
A getContext() 0 4 1
A getLocation() 0 4 1
A isTemplateStart() 0 4 1
A isTemplateEnd() 0 4 1
A getTags() 0 4 1
A getTagsByName() 0 15 3
A hasTag() 0 11 3
A removeTag() 0 9 3
A addTag() 0 4 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of phpDocumentor.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright 2010-2015 Mike van Riel<[email protected]>
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Reflection;
15
16
use phpDocumentor\Reflection\DocBlock\Tag;
17
use Webmozart\Assert\Assert;
18
19
final class DocBlock
20
{
21
    /** @var string The opening line for this docblock. */
22
    private $summary = '';
23
24
    /** @var DocBlock\Description The actual description for this docblock. */
25
    private $description = null;
26
27
    /** @var Tag[] An array containing all the tags in this docblock; except inline. */
28
    private $tags = [];
29
30
    /** @var Types\Context Information about the context of this DocBlock. */
31
    private $context = null;
32
33
    /** @var Location Information about the location of this DocBlock. */
34
    private $location = null;
35
36
    /** @var bool Is this DocBlock (the start of) a template? */
37
    private $isTemplateStart = false;
38
39
    /** @var bool Does this DocBlock signify the end of a DocBlock template? */
40
    private $isTemplateEnd = false;
41
42
    /**
43
     * @param DocBlock\Tag[] $tags
44
     * @param Types\Context $context The context in which the DocBlock occurs.
45
     * @param Location $location The location within the file that this DocBlock occurs in.
46
     */
47
    public function __construct(
48
        string $summary = '',
49
        ?DocBlock\Description $description = null,
50 16
        array $tags = [],
51
        ?Types\Context $context = null,
52
        ?Location $location = null,
53
        bool $isTemplateStart = false,
54
        bool $isTemplateEnd = false
55
    ) {
56
        Assert::allIsInstanceOf($tags, Tag::class);
57
58
        $this->summary = $summary;
59 16
        $this->description = $description ?: new DocBlock\Description('');
60 15
        foreach ($tags as $tag) {
61 14
            $this->addTag($tag);
62 13
        }
63
64 12
        $this->context = $context;
65 12
        $this->location = $location;
66 12
67 4
        $this->isTemplateEnd = $isTemplateEnd;
68
        $this->isTemplateStart = $isTemplateStart;
69
    }
70 12
71 12
    public function getSummary(): string
72
    {
73 12
        return $this->summary;
74 12
    }
75 12
76
    /**
77
     * @return DocBlock\Description
78
     */
79
    public function getDescription(): DocBlock\Description
80 1
    {
81
        return $this->description;
82 1
    }
83
84
    /**
85
     * Returns the current context.
86
     *
87
     */
88 1
    public function getContext(): Types\Context
89
    {
90 1
        return $this->context;
91
    }
92
93
    /**
94
     * Returns the current location.
95
     */
96
    public function getLocation(): ?Location
97
    {
98 1
        return $this->location;
99
    }
100 1
101
    /**
102
     * Returns whether this DocBlock is the start of a Template section.
103
     *
104
     * A Docblock may serve as template for a series of subsequent DocBlocks. This is indicated by a special marker
105
     * (`#@+`) that is appended directly after the opening `/**` of a DocBlock.
106
     *
107
     * An example of such an opening is:
108 1
     *
109
     * ```
110 1
     * /**#@+
111
     *  * My DocBlock
112
     *  * /
113
     * ```
114
     *
115
     * The description and tags (not the summary!) are copied onto all subsequent DocBlocks and also applied to all
116
     * elements that follow until another DocBlock is found that contains the closing marker (`#@-`).
117
     *
118
     * @see self::isTemplateEnd() for the check whether a closing marker was provided.
119
     *
120
     */
121
    public function isTemplateStart(): bool
122
    {
123
        return $this->isTemplateStart;
124
    }
125
126
    /**
127
     * Returns whether this DocBlock is the end of a Template section.
128
     *
129
     * @see self::isTemplateStart() for a more complete description of the Docblock Template functionality.
130
     *
131
     */
132
    public function isTemplateEnd(): bool
133
    {
134 1
        return $this->isTemplateEnd;
135
    }
136 1
137
    /**
138
     * Returns the tags for this DocBlock.
139
     *
140
     * @return Tag[]
141
     */
142
    public function getTags()
143
    {
144
        return $this->tags;
145
    }
146 1
147
    /**
148 1
     * Returns an array of tags matching the given name. If no tags are found
149
     * an empty array is returned.
150
     *
151
     * @param string $name String to search by.
152
     *
153
     * @return Tag[]
154
     */
155
    public function getTagsByName(string $name)
156 1
    {
157
        $result = [];
158 1
159
        /** @var Tag $tag */
160
        foreach ($this->getTags() as $tag) {
161
            if ($tag->getName() !== $name) {
162
                continue;
163
            }
164
165
            $result[] = $tag;
166
        }
167
168
        return $result;
169 2
    }
170
171 2
    /**
172
     * Checks if a tag of a certain type is present in this DocBlock.
173 1
     *
174
     * @param string $name Tag name to check for.
175
     */
176 1
    public function hasTag(string $name): bool
177 1
    {
178 1
        /** @var Tag $tag */
179
        foreach ($this->getTags() as $tag) {
180
            if ($tag->getName() === $name) {
181 1
                return true;
182
            }
183
        }
184 1
185
        return false;
186
    }
187
188
    /**
189
     * Remove a tag from this DocBlock.
190
     *
191
     * @param Tag $tag The tag to remove.
0 ignored issues
show
Bug introduced by
There is no parameter named $tag. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
192
     */
193
    public function removeTag(Tag $tagToRemove): void
194 2
    {
195
        foreach ($this->tags as $key => $tag) {
196 2
            if ($tag === $tagToRemove) {
197
                unset($this->tags[$key]);
198
                break;
199 1
            }
200 1
        }
201 1
    }
202
203
    /**
204
     * Adds a tag to this DocBlock.
205 1
     *
206
     * @param Tag $tag The tag to add.
207
     */
208
    private function addTag(Tag $tag): void
209
    {
210
        $this->tags[] = $tag;
211
    }
212
}
213