DocBlock.php 5.1 KB

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