Completed
Pull Request — master (#125)
by Tomáš
07:31 queued 06:04
created

DocBlock   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 220
ccs 45
cts 51
cp 0.8824
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
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
B __construct() 0 27 3
A getTagsByName() 0 17 3
A hasTag() 0 13 3
A removeTag() 0 9 3
A addTag() 0 4 1
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 = array();
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 15
    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
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
60 15
        Assert::string($summary);
61 14
        Assert::boolean($isTemplateStart);
62 13
        Assert::boolean($isTemplateEnd);
63 12
        Assert::allIsInstanceOf($tags, Tag::class);
64
65 11
        $this->summary = $summary;
66 11
        $this->description = $description ?: new DocBlock\Description('');
67 11
        foreach ($tags as $tag) {
68 3
            $this->addTag($tag);
69
        }
70
71 11
        $this->context = $context;
72 11
        $this->location = $location;
73
74 11
        $this->isTemplateEnd = $isTemplateEnd;
75 11
        $this->isTemplateStart = $isTemplateStart;
76 11
    }
77
78
    /**
79
     * @return string
80
     */
81 1
    public function getSummary()
82
    {
83 1
        return $this->summary;
84
    }
85
86
    /**
87
     * @return DocBlock\Description
88
     */
89 1
    public function getDescription()
90
    {
91 1
        return $this->description;
92
    }
93
94
    /**
95
     * Returns the current context.
96
     *
97
     * @return Types\Context
98
     */
99 1
    public function getContext()
100
    {
101 1
        return $this->context;
102
    }
103
104
    /**
105
     * Returns the current location.
106
     *
107
     * @return Location
108
     */
109 1
    public function getLocation()
110
    {
111 1
        return $this->location;
112
    }
113
114
    /**
115
     * Returns whether this DocBlock is the start of a Template section.
116
     *
117
     * A Docblock may serve as template for a series of subsequent DocBlocks. This is indicated by a special marker
118
     * (`#@+`) that is appended directly after the opening `/**` of a DocBlock.
119
     *
120
     * An example of such an opening is:
121
     *
122
     * ```
123
     * /**#@+
124
     *  * My DocBlock
125
     *  * /
126
     * ```
127
     *
128
     * The description and tags (not the summary!) are copied onto all subsequent DocBlocks and also applied to all
129
     * elements that follow until another DocBlock is found that contains the closing marker (`#@-`).
130
     *
131
     * @see self::isTemplateEnd() for the check whether a closing marker was provided.
132
     *
133
     * @return boolean
134
     */
135 1
    public function isTemplateStart()
136
    {
137 1
        return $this->isTemplateStart;
138
    }
139
140
    /**
141
     * Returns whether this DocBlock is the end of a Template section.
142
     *
143
     * @see self::isTemplateStart() for a more complete description of the Docblock Template functionality.
144
     *
145
     * @return boolean
146
     */
147 1
    public function isTemplateEnd()
148
    {
149 1
        return $this->isTemplateEnd;
150
    }
151
152
    /**
153
     * Returns the tags for this DocBlock.
154
     *
155
     * @return Tag[]
156
     */
157 1
    public function getTags()
158
    {
159 1
        return $this->tags;
160
    }
161
162
    /**
163
     * Returns an array of tags matching the given name. If no tags are found
164
     * an empty array is returned.
165
     *
166
     * @param string $name String to search by.
167
     *
168
     * @return Tag[]
169
     */
170 2
    public function getTagsByName($name)
171
    {
172 2
        Assert::string($name);
173
174 1
        $result = array();
175
176
        /** @var Tag $tag */
177 1
        foreach ($this->getTags() as $tag) {
178 1
            if ($tag->getName() != $name) {
179 1
                continue;
180
            }
181
182 1
            $result[] = $tag;
183
        }
184
185 1
        return $result;
186
    }
187
188
    /**
189
     * Checks if a tag of a certain type is present in this DocBlock.
190
     *
191
     * @param string $name Tag name to check for.
192
     *
193
     * @return bool
194
     */
195 2
    public function hasTag($name)
196
    {
197 2
        Assert::string($name);
198
199
        /** @var Tag $tag */
200 1
        foreach ($this->getTags() as $tag) {
201 1
            if ($tag->getName() == $name) {
202 1
                return true;
203
            }
204
        }
205
206 1
        return false;
207
    }
208
209
    /**
210
     * Remove a tag from this DocBlock.
211
     *
212
     * @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...
213
     *
214
     * @return void
215
     */
216
    public function removeTag(Tag $tagToRemove)
217
    {
218
        foreach ($this->tags as $key => $tag) {
219
            if ($tag === $tagToRemove) {
220
                unset($this->tags[$key]);
221
                break;
222
            }
223
        }
224
    }
225
226
    /**
227
     * Adds a tag to this DocBlock.
228
     *
229
     * @param Tag $tag The tag to add.
230
     *
231
     * @return void
232
     */
233 3
    private function addTag(Tag $tag)
234
    {
235 3
        $this->tags[] = $tag;
236 3
    }
237
}
238