DocBlock.php 5.8 KB

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