/public_html/lib/pear/PHP/CodeSniffer/CommentParser/AbstractDocElement.php

https://github.com/hatone/moodle · PHP · 327 lines · 107 code · 54 blank · 166 comment · 15 complexity · f4ffbf466b88fd4560e51d770d9a4d71 MD5 · raw file

  1. <?php
  2. /**
  3. * A class to handle most of the parsing operations of a doc comment element.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @author Marc McIntyre <mmcintyre@squiz.net>
  11. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @version CVS: $Id: AbstractDocElement.php,v 1.2 2010/12/14 17:35:22 moodlerobot Exp $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (interface_exists('PHP_CodeSniffer_CommentParser_DocElement', true) === false) {
  17. $error = 'Interface PHP_CodeSniffer_CommentParser_DocElement not found';
  18. throw new PHP_CodeSniffer_Exception($error);
  19. }
  20. /**
  21. * A class to handle most of the parsing operations of a doc comment element.
  22. *
  23. * Extending classes should implement the getSubElements method to return
  24. * a list of elements that the doc comment element contains, in the order that
  25. * they appear in the element. For example a function parameter element has a
  26. * type, a variable name and a comment. It should therefore implement the method
  27. * as follows:
  28. *
  29. * <code>
  30. * protected function getSubElements()
  31. * {
  32. * return array(
  33. * 'type',
  34. * 'variable',
  35. * 'comment',
  36. * );
  37. * }
  38. * </code>
  39. *
  40. * The processSubElement will be called for each of the sub elements to allow
  41. * the extending class to process them. So for the parameter element we would
  42. * have:
  43. *
  44. * <code>
  45. * protected function processSubElement($name, $content, $whitespaceBefore)
  46. * {
  47. * if ($name === 'type') {
  48. * echo 'The name of the variable was '.$content;
  49. * }
  50. * // Process other tags.
  51. * }
  52. * </code>
  53. *
  54. * @category PHP
  55. * @package PHP_CodeSniffer
  56. * @author Greg Sherwood <gsherwood@squiz.net>
  57. * @author Marc McIntyre <mmcintyre@squiz.net>
  58. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  59. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  60. * @version Release: 1.1.0
  61. * @link http://pear.php.net/package/PHP_CodeSniffer
  62. */
  63. abstract class PHP_CodeSniffer_CommentParser_AbstractDocElement implements PHP_CodeSniffer_CommentParser_DocElement
  64. {
  65. /**
  66. * The element previous to this element.
  67. *
  68. * @var PHP_CodeSniffer_CommentParser_DocElement
  69. */
  70. protected $previousElement = null;
  71. /**
  72. * The element proceeding this element.
  73. *
  74. * @var PHP_CodeSniffer_CommentParser_DocElement
  75. */
  76. protected $nextElement = null;
  77. /**
  78. * The whitespace the occurs after this element and its sub elements.
  79. *
  80. * @var string
  81. */
  82. protected $afterWhitespace = '';
  83. /**
  84. * The tokens that comprise this element.
  85. *
  86. * @var array(string)
  87. */
  88. protected $tokens = array();
  89. /**
  90. * The file this element is in.
  91. *
  92. * @var array(string)
  93. */
  94. protected $phpcsFile = null;
  95. /**
  96. * The tag that this element represents (omiting the @ symbol).
  97. *
  98. * @var string
  99. */
  100. protected $tag = '';
  101. /**
  102. * Constructs a Doc Element.
  103. *
  104. * @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
  105. * that ocurred
  106. * before this.
  107. * @param array $tokens The tokens of
  108. * this element.
  109. * @param string $tag The doc
  110. * element tag
  111. * this element
  112. * represents.
  113. * @param PHP_CodeSniffer_File $phpcsFile The file that
  114. * this element
  115. * is in.
  116. *
  117. * @throws Exception If $previousElement in not a DocElement or if
  118. * getSubElements() does not return an array.
  119. */
  120. public function __construct($previousElement, array $tokens, $tag, PHP_CodeSniffer_File $phpcsFile)
  121. {
  122. if ($previousElement !== null && ($previousElement instanceof PHP_CodeSniffer_CommentParser_DocElement) === false) {
  123. $error = '$previousElement must be an instance of DocElement';
  124. throw new Exception($error);
  125. }
  126. $this->phpcsFile = $phpcsFile;
  127. $this->previousElement = $previousElement;
  128. if ($previousElement !== null) {
  129. $this->previousElement->nextElement = $this;
  130. }
  131. $this->tag = $tag;
  132. $this->tokens = $tokens;
  133. $subElements = $this->getSubElements();
  134. if (is_array($subElements) === false) {
  135. throw new Exception('getSubElements() must return an array');
  136. }
  137. $whitespace = '';
  138. $currElem = 0;
  139. $lastElement = '';
  140. $lastElementWhitespace = null;
  141. $numSubElements = count($subElements);
  142. foreach ($this->tokens as $token) {
  143. if (trim($token) === '') {
  144. $whitespace .= $token;
  145. } else {
  146. if ($currElem < ($numSubElements - 1)) {
  147. $element = $subElements[$currElem];
  148. $this->processSubElement($element, $token, $whitespace);
  149. $whitespace = '';
  150. $currElem++;
  151. } else {
  152. if ($lastElementWhitespace === null) {
  153. $lastElementWhitespace = $whitespace;
  154. }
  155. $lastElement .= $whitespace.$token;
  156. $whitespace = '';
  157. }
  158. }
  159. }//end foreach
  160. $lastElement = ltrim($lastElement);
  161. $lastElementName = $subElements[($numSubElements - 1)];
  162. // Process the last element in this tag.
  163. $this->processSubElement($lastElementName, $lastElement, $lastElementWhitespace);
  164. $this->afterWhitespace = $whitespace;
  165. }//end __construct()
  166. /**
  167. * Returns the element that exists before this.
  168. *
  169. * @return PHP_CodeSniffer_CommentParser_DocElement
  170. */
  171. public function getPreviousElement()
  172. {
  173. return $this->previousElement;
  174. }//end getPreviousElement()
  175. /**
  176. * Returns the element that exists after this.
  177. *
  178. * @return PHP_CodeSniffer_CommentParser_DocElement
  179. */
  180. public function getNextElement()
  181. {
  182. return $this->nextElement;
  183. }//end getNextElement()
  184. /**
  185. * Returns the whitespace that exists before this element.
  186. *
  187. * @return string
  188. */
  189. public function getWhitespaceBefore()
  190. {
  191. if ($this->previousElement !== null) {
  192. return $this->previousElement->getWhitespaceAfter();
  193. }
  194. return '';
  195. }//end getWhitespaceBefore()
  196. /**
  197. * Returns the whitespace that exists after this element.
  198. *
  199. * @return string
  200. */
  201. public function getWhitespaceAfter()
  202. {
  203. return $this->afterWhitespace;
  204. }//end getWhitespaceAfter()
  205. /**
  206. * Returns the order that this element appears in the comment.
  207. *
  208. * @return int
  209. */
  210. public function getOrder()
  211. {
  212. if ($this->previousElement === null) {
  213. return 1;
  214. } else {
  215. return ($this->previousElement->getOrder() + 1);
  216. }
  217. }//end getOrder()
  218. /**
  219. * Returns the tag that this element represents, ommiting the @ symbol.
  220. *
  221. * @return string
  222. */
  223. public function getTag()
  224. {
  225. return $this->tag;
  226. }//end getTag()
  227. /**
  228. * Returns the raw content of this element, ommiting the tag.
  229. *
  230. * @return string
  231. */
  232. public function getRawContent()
  233. {
  234. return implode('', $this->tokens);
  235. }//end getRawContent()
  236. /**
  237. * Returns the line in which this element first occured.
  238. *
  239. * @return int
  240. */
  241. public function getLine()
  242. {
  243. if ($this->previousElement === null) {
  244. // First element is on line one.
  245. return 1;
  246. } else {
  247. $previousContent = $this->previousElement->getRawContent();
  248. $previousLine = $this->previousElement->getLine();
  249. return ($previousLine + substr_count($previousContent, $this->phpcsFile->eolChar));
  250. }
  251. }//end getLine()
  252. /**
  253. * Returns the sub element names that make up this element in the order they
  254. * appear in the element.
  255. *
  256. * @return array(string)
  257. * @see processSubElement()
  258. */
  259. abstract protected function getSubElements();
  260. /**
  261. * Called to process each sub element as sepcified in the return value
  262. * of getSubElements().
  263. *
  264. * @param string $name The name of the element to process.
  265. * @param string $content The content of the the element.
  266. * @param string $whitespaceBefore The whitespace found before this element.
  267. *
  268. * @return void
  269. * @see getSubElements()
  270. */
  271. abstract protected function processSubElement($name, $content, $whitespaceBefore);
  272. }//end class
  273. ?>