annotate vendor/phpdocumentor/reflection-docblock/src/DocBlock.php @ 2:5311817fb629

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