Completed
Push — master ( 03b704...7524ab )
by Jaap
01:44
created

DocBlock   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 219
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 219
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

12 Methods

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