DocBlock.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 = array();
  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. {
  50. Assert::string($summary);
  51. Assert::boolean($isTemplateStart);
  52. Assert::boolean($isTemplateEnd);
  53. Assert::allIsInstanceOf($tags, Tag::class);
  54. $this->summary = $summary;
  55. $this->description = $description ?: new DocBlock\Description('');
  56. foreach ($tags as $tag) {
  57. $this->addTag($tag);
  58. }
  59. $this->context = $context;
  60. $this->location = $location;
  61. $this->isTemplateEnd = $isTemplateEnd;
  62. $this->isTemplateStart = $isTemplateStart;
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getSummary()
  68. {
  69. return $this->summary;
  70. }
  71. /**
  72. * @return DocBlock\Description
  73. */
  74. public function getDescription()
  75. {
  76. return $this->description;
  77. }
  78. /**
  79. * Returns the current context.
  80. *
  81. * @return Types\Context
  82. */
  83. public function getContext()
  84. {
  85. return $this->context;
  86. }
  87. /**
  88. * Returns the current location.
  89. *
  90. * @return Location
  91. */
  92. public function getLocation()
  93. {
  94. return $this->location;
  95. }
  96. /**
  97. * Returns whether this DocBlock is the start of a Template section.
  98. *
  99. * A Docblock may serve as template for a series of subsequent DocBlocks. This is indicated by a special marker
  100. * (`#@+`) that is appended directly after the opening `/**` of a DocBlock.
  101. *
  102. * An example of such an opening is:
  103. *
  104. * ```
  105. * /**#@+
  106. * * My DocBlock
  107. * * /
  108. * ```
  109. *
  110. * The description and tags (not the summary!) are copied onto all subsequent DocBlocks and also applied to all
  111. * elements that follow until another DocBlock is found that contains the closing marker (`#@-`).
  112. *
  113. * @see self::isTemplateEnd() for the check whether a closing marker was provided.
  114. *
  115. * @return boolean
  116. */
  117. public function isTemplateStart()
  118. {
  119. return $this->isTemplateStart;
  120. }
  121. /**
  122. * Returns whether this DocBlock is the end of a Template section.
  123. *
  124. * @see self::isTemplateStart() for a more complete description of the Docblock Template functionality.
  125. *
  126. * @return boolean
  127. */
  128. public function isTemplateEnd()
  129. {
  130. return $this->isTemplateEnd;
  131. }
  132. /**
  133. * Returns the tags for this DocBlock.
  134. *
  135. * @return Tag[]
  136. */
  137. public function getTags()
  138. {
  139. return $this->tags;
  140. }
  141. /**
  142. * Returns an array of tags matching the given name. If no tags are found
  143. * an empty array is returned.
  144. *
  145. * @param string $name String to search by.
  146. *
  147. * @return Tag[]
  148. */
  149. public function getTagsByName($name)
  150. {
  151. Assert::string($name);
  152. $result = array();
  153. /** @var Tag $tag */
  154. foreach ($this->getTags() as $tag) {
  155. if ($tag->getName() != $name) {
  156. continue;
  157. }
  158. $result[] = $tag;
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Checks if a tag of a certain type is present in this DocBlock.
  164. *
  165. * @param string $name Tag name to check for.
  166. *
  167. * @return bool
  168. */
  169. public function hasTag($name)
  170. {
  171. Assert::string($name);
  172. /** @var Tag $tag */
  173. foreach ($this->getTags() as $tag) {
  174. if ($tag->getName() == $name) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. /**
  181. * Adds a tag to this DocBlock.
  182. *
  183. * @param Tag $tag The tag to add.
  184. *
  185. * @return void
  186. */
  187. private function addTag(Tag $tag)
  188. {
  189. $this->tags[] = $tag;
  190. }
  191. }