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

DocBlock::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 9
cts 9
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 7
crap 3
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
    public function getContext(): Types\Context
88 1
    {
89
        return $this->context;
90 1
    }
91
92
    /**
93
     * Returns the current location.
94
     */
95
    public function getLocation(): ?Location
96
    {
97
        return $this->location;
98 1
    }
99
100 1
    /**
101
     * Returns whether this DocBlock is the start of a Template section.
102
     *
103
     * A Docblock may serve as template for a series of subsequent DocBlocks. This is indicated by a special marker
104
     * (`#@+`) that is appended directly after the opening `/**` of a DocBlock.
105
     *
106
     * An example of such an opening is:
107
     *
108 1
     * ```
109
     * /**#@+
110 1
     *  * My DocBlock
111
     *  * /
112
     * ```
113
     *
114
     * The description and tags (not the summary!) are copied onto all subsequent DocBlocks and also applied to all
115
     * elements that follow until another DocBlock is found that contains the closing marker (`#@-`).
116
     *
117
     * @see self::isTemplateEnd() for the check whether a closing marker was provided.
118
     */
119
    public function isTemplateStart(): bool
120
    {
121
        return $this->isTemplateStart;
122
    }
123
124
    /**
125
     * Returns whether this DocBlock is the end of a Template section.
126
     *
127
     * @see self::isTemplateStart() for a more complete description of the Docblock Template functionality.
128
     */
129
    public function isTemplateEnd(): bool
130
    {
131
        return $this->isTemplateEnd;
132
    }
133
134 1
    /**
135
     * Returns the tags for this DocBlock.
136 1
     *
137
     * @return Tag[]
138
     */
139
    public function getTags()
140
    {
141
        return $this->tags;
142
    }
143
144
    /**
145
     * Returns an array of tags matching the given name. If no tags are found
146 1
     * an empty array is returned.
147
     *
148 1
     * @param string $name String to search by.
149
     *
150
     * @return Tag[]
151
     */
152
    public function getTagsByName(string $name)
153
    {
154
        $result = [];
155
156 1
        /** @var Tag $tag */
157
        foreach ($this->getTags() as $tag) {
158 1
            if ($tag->getName() !== $name) {
159
                continue;
160
            }
161
162
            $result[] = $tag;
163
        }
164
165
        return $result;
166
    }
167
168
    /**
169 2
     * Checks if a tag of a certain type is present in this DocBlock.
170
     *
171 2
     * @param string $name Tag name to check for.
172
     */
173 1
    public function hasTag(string $name): bool
174
    {
175
        /** @var Tag $tag */
176 1
        foreach ($this->getTags() as $tag) {
177 1
            if ($tag->getName() === $name) {
178 1
                return true;
179
            }
180
        }
181 1
182
        return false;
183
    }
184 1
185
    /**
186
     * Remove a tag from this DocBlock.
187
     *
188
     * @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...
189
     */
190
    public function removeTag(Tag $tagToRemove): void
191
    {
192
        foreach ($this->tags as $key => $tag) {
193
            if ($tag === $tagToRemove) {
194 2
                unset($this->tags[$key]);
195
                break;
196 2
            }
197
        }
198
    }
199 1
200 1
    /**
201 1
     * Adds a tag to this DocBlock.
202
     *
203
     * @param Tag $tag The tag to add.
204
     */
205 1
    private function addTag(Tag $tag): void
206
    {
207
        $this->tags[] = $tag;
208
    }
209
}
210