Debian Codegraph @

lib/3rdParty/composer/phpdocumentor/reflection-docblock/src/DocBlock.php

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