PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zend-code/src/Reflection/DocBlockReflection.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 296 lines | 141 code | 45 blank | 110 comment | 15 complexity | 60449aa38ca881fd71ab36a932883738 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Code\Reflection;
  10. use Reflector;
  11. use Zend\Code\Reflection\DocBlock\Tag\TagInterface as DocBlockTagInterface;
  12. use Zend\Code\Reflection\DocBlock\TagManager as DocBlockTagManager;
  13. use Zend\Code\Scanner\DocBlockScanner;
  14. class DocBlockReflection implements ReflectionInterface
  15. {
  16. /**
  17. * @var Reflector
  18. */
  19. protected $reflector = null;
  20. /**
  21. * @var string
  22. */
  23. protected $docComment = null;
  24. /**
  25. * @var DocBlockTagManager
  26. */
  27. protected $tagManager = null;
  28. /**#@+
  29. * @var int
  30. */
  31. protected $startLine = null;
  32. protected $endLine = null;
  33. /**#@-*/
  34. /**
  35. * @var string
  36. */
  37. protected $cleanDocComment = null;
  38. /**
  39. * @var string
  40. */
  41. protected $longDescription = null;
  42. /**
  43. * @var string
  44. */
  45. protected $shortDescription = null;
  46. /**
  47. * @var array
  48. */
  49. protected $tags = array();
  50. /**
  51. * @var bool
  52. */
  53. protected $isReflected = false;
  54. /**
  55. * Export reflection
  56. *
  57. * Required by the Reflector interface.
  58. *
  59. * @todo What should this do?
  60. * @return void
  61. */
  62. public static function export()
  63. {
  64. }
  65. /**
  66. * @param Reflector|string $commentOrReflector
  67. * @param null|DocBlockTagManager $tagManager
  68. * @throws Exception\InvalidArgumentException
  69. * @return DocBlockReflection
  70. */
  71. public function __construct($commentOrReflector, DocBlockTagManager $tagManager = null)
  72. {
  73. if (!$tagManager) {
  74. $tagManager = new DocBlockTagManager();
  75. $tagManager->initializeDefaultTags();
  76. }
  77. $this->tagManager = $tagManager;
  78. if ($commentOrReflector instanceof Reflector) {
  79. $this->reflector = $commentOrReflector;
  80. if (!method_exists($commentOrReflector, 'getDocComment')) {
  81. throw new Exception\InvalidArgumentException('Reflector must contain method "getDocComment"');
  82. }
  83. /* @var MethodReflection $commentOrReflector */
  84. $this->docComment = $commentOrReflector->getDocComment();
  85. // determine line numbers
  86. $lineCount = substr_count($this->docComment, "\n");
  87. $this->startLine = $this->reflector->getStartLine() - $lineCount - 1;
  88. $this->endLine = $this->reflector->getStartLine() - 1;
  89. } elseif (is_string($commentOrReflector)) {
  90. $this->docComment = $commentOrReflector;
  91. } else {
  92. throw new Exception\InvalidArgumentException(sprintf(
  93. '%s must have a (string) DocComment or a Reflector in the constructor',
  94. get_class($this)
  95. ));
  96. }
  97. if ($this->docComment == '') {
  98. throw new Exception\InvalidArgumentException('DocComment cannot be empty');
  99. }
  100. $this->reflect();
  101. }
  102. /**
  103. * Retrieve contents of DocBlock
  104. *
  105. * @return string
  106. */
  107. public function getContents()
  108. {
  109. $this->reflect();
  110. return $this->cleanDocComment;
  111. }
  112. /**
  113. * Get start line (position) of DocBlock
  114. *
  115. * @return int
  116. */
  117. public function getStartLine()
  118. {
  119. $this->reflect();
  120. return $this->startLine;
  121. }
  122. /**
  123. * Get last line (position) of DocBlock
  124. *
  125. * @return int
  126. */
  127. public function getEndLine()
  128. {
  129. $this->reflect();
  130. return $this->endLine;
  131. }
  132. /**
  133. * Get DocBlock short description
  134. *
  135. * @return string
  136. */
  137. public function getShortDescription()
  138. {
  139. $this->reflect();
  140. return $this->shortDescription;
  141. }
  142. /**
  143. * Get DocBlock long description
  144. *
  145. * @return string
  146. */
  147. public function getLongDescription()
  148. {
  149. $this->reflect();
  150. return $this->longDescription;
  151. }
  152. /**
  153. * Does the DocBlock contain the given annotation tag?
  154. *
  155. * @param string $name
  156. * @return bool
  157. */
  158. public function hasTag($name)
  159. {
  160. $this->reflect();
  161. foreach ($this->tags as $tag) {
  162. if ($tag->getName() == $name) {
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * Retrieve the given DocBlock tag
  170. *
  171. * @param string $name
  172. * @return DocBlockTagInterface|false
  173. */
  174. public function getTag($name)
  175. {
  176. $this->reflect();
  177. foreach ($this->tags as $tag) {
  178. if ($tag->getName() == $name) {
  179. return $tag;
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * Get all DocBlock annotation tags
  186. *
  187. * @param string $filter
  188. * @return DocBlockTagInterface[]
  189. */
  190. public function getTags($filter = null)
  191. {
  192. $this->reflect();
  193. if ($filter === null || !is_string($filter)) {
  194. return $this->tags;
  195. }
  196. $returnTags = array();
  197. foreach ($this->tags as $tag) {
  198. if ($tag->getName() == $filter) {
  199. $returnTags[] = $tag;
  200. }
  201. }
  202. return $returnTags;
  203. }
  204. /**
  205. * Parse the DocBlock
  206. *
  207. * @return void
  208. */
  209. protected function reflect()
  210. {
  211. if ($this->isReflected) {
  212. return;
  213. }
  214. $docComment = preg_replace('#[ ]{0,1}\*/$#', '', $this->docComment);
  215. // create a clean docComment
  216. $this->cleanDocComment = preg_replace("#[ \t]*(?:/\*\*|\*/|\*)[ ]{0,1}(.*)?#", '$1', $docComment);
  217. $this->cleanDocComment = ltrim($this->cleanDocComment, "\r\n"); // @todo should be changed to remove first and last empty line
  218. $scanner = new DocBlockScanner($docComment);
  219. $this->shortDescription = ltrim($scanner->getShortDescription());
  220. $this->longDescription = ltrim($scanner->getLongDescription());
  221. foreach ($scanner->getTags() as $tag) {
  222. $this->tags[] = $this->tagManager->createTag(ltrim($tag['name'], '@'), ltrim($tag['value']));
  223. }
  224. $this->isReflected = true;
  225. }
  226. /**
  227. * @return string
  228. */
  229. public function toString()
  230. {
  231. $str = "DocBlock [ /* DocBlock */ ] {" . PHP_EOL . PHP_EOL;
  232. $str .= " - Tags [" . count($this->tags) . "] {" . PHP_EOL;
  233. foreach ($this->tags as $tag) {
  234. $str .= " " . $tag;
  235. }
  236. $str .= " }" . PHP_EOL;
  237. $str .= "}" . PHP_EOL;
  238. return $str;
  239. }
  240. /**
  241. * Serialize to string
  242. *
  243. * Required by the Reflector interface
  244. *
  245. * @return string
  246. */
  247. public function __toString()
  248. {
  249. return $this->toString();
  250. }
  251. }