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

DocBlock::getLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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