/library/phpdocx/lib/php_codesniffer/CodeSniffer/CommentParser/AbstractDocElement.php

https://github.com/r1zib/salesforce · PHP · 354 lines · 125 code · 58 blank · 171 comment · 15 complexity · a7bb1aeb0f17dabbc4a1a6279f4475e0 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 302087 2010-08-11 01:51:18Z squiz $
  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.3.0RC1
  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(
  121. $previousElement,
  122. array $tokens,
  123. $tag,
  124. PHP_CodeSniffer_File $phpcsFile
  125. ) {
  126. if ($previousElement !== null
  127. && ($previousElement instanceof PHP_CodeSniffer_CommentParser_DocElement) === false
  128. ) {
  129. $error = '$previousElement must be an instance of DocElement';
  130. throw new Exception($error);
  131. }
  132. $this->phpcsFile = $phpcsFile;
  133. $this->previousElement = $previousElement;
  134. if ($previousElement !== null) {
  135. $this->previousElement->nextElement = $this;
  136. }
  137. $this->tag = $tag;
  138. $this->tokens = $tokens;
  139. $subElements = $this->getSubElements();
  140. if (is_array($subElements) === false) {
  141. throw new Exception('getSubElements() must return an array');
  142. }
  143. $whitespace = '';
  144. $currElem = 0;
  145. $lastElement = '';
  146. $lastElementWhitespace = null;
  147. $numSubElements = count($subElements);
  148. foreach ($this->tokens as $token) {
  149. if (trim($token) === '') {
  150. $whitespace .= $token;
  151. } else {
  152. if ($currElem < ($numSubElements - 1)) {
  153. $element = $subElements[$currElem];
  154. $this->processSubElement($element, $token, $whitespace);
  155. $whitespace = '';
  156. $currElem++;
  157. } else {
  158. if ($lastElementWhitespace === null) {
  159. $lastElementWhitespace = $whitespace;
  160. }
  161. $lastElement .= $whitespace.$token;
  162. $whitespace = '';
  163. }
  164. }
  165. }//end foreach
  166. $lastElement = ltrim($lastElement);
  167. $lastElementName = $subElements[($numSubElements - 1)];
  168. // Process the last element in this tag.
  169. $this->processSubElement(
  170. $lastElementName,
  171. $lastElement,
  172. $lastElementWhitespace
  173. );
  174. $this->afterWhitespace = $whitespace;
  175. }//end __construct()
  176. /**
  177. * Returns the element that exists before this.
  178. *
  179. * @return PHP_CodeSniffer_CommentParser_DocElement
  180. */
  181. public function getPreviousElement()
  182. {
  183. return $this->previousElement;
  184. }//end getPreviousElement()
  185. /**
  186. * Returns the element that exists after this.
  187. *
  188. * @return PHP_CodeSniffer_CommentParser_DocElement
  189. */
  190. public function getNextElement()
  191. {
  192. return $this->nextElement;
  193. }//end getNextElement()
  194. /**
  195. * Returns the whitespace that exists before this element.
  196. *
  197. * @return string
  198. */
  199. public function getWhitespaceBefore()
  200. {
  201. if ($this->previousElement !== null) {
  202. return $this->previousElement->getWhitespaceAfter();
  203. }
  204. return '';
  205. }//end getWhitespaceBefore()
  206. /**
  207. * Returns the whitespace that exists after this element.
  208. *
  209. * @return string
  210. */
  211. public function getWhitespaceAfter()
  212. {
  213. return $this->afterWhitespace;
  214. }//end getWhitespaceAfter()
  215. /**
  216. * Returns the order that this element appears in the comment.
  217. *
  218. * @return int
  219. */
  220. public function getOrder()
  221. {
  222. if ($this->previousElement === null) {
  223. return 1;
  224. } else {
  225. return ($this->previousElement->getOrder() + 1);
  226. }
  227. }//end getOrder()
  228. /**
  229. * Returns the tag that this element represents, ommiting the @ symbol.
  230. *
  231. * @return string
  232. */
  233. public function getTag()
  234. {
  235. return $this->tag;
  236. }//end getTag()
  237. /**
  238. * Returns the raw content of this element, ommiting the tag.
  239. *
  240. * @return string
  241. */
  242. public function getRawContent()
  243. {
  244. return implode('', $this->tokens);
  245. }//end getRawContent()
  246. /**
  247. * Returns the comment tokens.
  248. *
  249. * @return array
  250. */
  251. public function getTokens()
  252. {
  253. return $this->tokens;
  254. }//end getTokens()
  255. /**
  256. * Returns the line in which this element first occured.
  257. *
  258. * @return int
  259. */
  260. public function getLine()
  261. {
  262. if ($this->previousElement === null) {
  263. // First element is on line one.
  264. return 1;
  265. } else {
  266. $previousContent = $this->previousElement->getRawContent();
  267. $previousLine = $this->previousElement->getLine();
  268. return ($previousLine + substr_count($previousContent, $this->phpcsFile->eolChar));
  269. }
  270. }//end getLine()
  271. /**
  272. * Returns the sub element names that make up this element in the order they
  273. * appear in the element.
  274. *
  275. * @return array(string)
  276. * @see processSubElement()
  277. */
  278. abstract protected function getSubElements();
  279. /**
  280. * Called to process each sub element as sepcified in the return value
  281. * of getSubElements().
  282. *
  283. * @param string $name The name of the element to process.
  284. * @param string $content The content of the the element.
  285. * @param string $whitespaceBefore The whitespace found before this element.
  286. *
  287. * @return void
  288. * @see getSubElements()
  289. */
  290. abstract protected function processSubElement(
  291. $name,
  292. $content,
  293. $whitespaceBefore
  294. );
  295. }//end class
  296. ?>