Completed
Push — master ( fc4fe7...332c96 )
by Mike
03:28
created

DocBlock::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

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