/plugins/sfAuditPlugin/lib/vendor/PHP/CodeSniffer/CommentParser/CommentElement.php

https://github.com/mikesname/ehri-ica-atom · PHP · 246 lines · 108 code · 38 blank · 100 comment · 17 complexity · 52b89a4da4a8776239c054572c8b03d5 MD5 · raw file

  1. <?php
  2. /**
  3. * A class to represent Comments of a doc comment.
  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: CommentElement.php,v 1.14 2008/12/02 02:38:33 squiz Exp $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_CommentParser_SingleElement', true) === false) {
  17. $error = 'Class PHP_CodeSniffer_CommentParser_SingleElement not found';
  18. throw new PHP_CodeSniffer_Exception($error);
  19. }
  20. /**
  21. * A class to represent Comments of a doc comment.
  22. *
  23. * Comments are in the following format.
  24. * <code>
  25. * /** <--this is the start of the comment.
  26. * * This is a short comment description
  27. * *
  28. * * This is a long comment description
  29. * * <-- this is the end of the comment
  30. * * @return something
  31. * {@/}
  32. * </code>
  33. *
  34. * Note that the sentence before two newlines is assumed
  35. * the short comment description.
  36. *
  37. * @category PHP
  38. * @package PHP_CodeSniffer
  39. * @author Greg Sherwood <gsherwood@squiz.net>
  40. * @author Marc McIntyre <mmcintyre@squiz.net>
  41. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  42. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  43. * @version Release: @package_version@
  44. * @link http://pear.php.net/package/PHP_CodeSniffer
  45. */
  46. class PHP_CodeSniffer_CommentParser_CommentElement extends PHP_CodeSniffer_CommentParser_SingleElement
  47. {
  48. /**
  49. * Constructs a PHP_CodeSniffer_CommentParser_CommentElement.
  50. *
  51. * @param PHP_CodeSniffer_CommentParser_DocElemement $previousElement The element
  52. * that
  53. * appears
  54. * before this
  55. * element.
  56. * @param array $tokens The tokens
  57. * that make
  58. * up this
  59. * element.
  60. * @param PHP_CodeSniffer_File $phpcsFile The file
  61. * that this
  62. * element is
  63. * in.
  64. */
  65. public function __construct(
  66. $previousElement,
  67. $tokens,
  68. PHP_CodeSniffer_File $phpcsFile
  69. ) {
  70. parent::__construct($previousElement, $tokens, 'comment', $phpcsFile);
  71. }//end __construct()
  72. /**
  73. * Returns the short comment description.
  74. *
  75. * @return string
  76. * @see getLongComment()
  77. */
  78. public function getShortComment()
  79. {
  80. $pos = $this->_getShortCommentEndPos();
  81. if ($pos === -1) {
  82. return '';
  83. }
  84. return implode('', array_slice($this->tokens, 0, ($pos + 1)));
  85. }//end getShortComment()
  86. /**
  87. * Returns the last token position of the short comment description.
  88. *
  89. * @return int The last token position of the short comment description
  90. * @see _getLongCommentStartPos()
  91. */
  92. private function _getShortCommentEndPos()
  93. {
  94. $found = false;
  95. $whiteSpace = array(
  96. ' ',
  97. "\t",
  98. );
  99. foreach ($this->tokens as $pos => $token) {
  100. $token = str_replace($whiteSpace, '', $token);
  101. if ($token === $this->phpcsFile->eolChar) {
  102. if ($found === false) {
  103. // Include newlines before short description.
  104. continue;
  105. } else {
  106. if (isset($this->tokens[($pos + 1)]) === true) {
  107. if ($this->tokens[($pos + 1)] === $this->phpcsFile->eolChar) {
  108. return ($pos - 1);
  109. }
  110. } else {
  111. return $pos;
  112. }
  113. }
  114. } else {
  115. $found = true;
  116. }
  117. }//end foreach
  118. return (count($this->tokens) - 1);
  119. }//end _getShortCommentEndPos()
  120. /**
  121. * Returns the long comment description.
  122. *
  123. * @return string
  124. * @see getShortComment
  125. */
  126. public function getLongComment()
  127. {
  128. $start = $this->_getLongCommentStartPos();
  129. if ($start === -1) {
  130. return '';
  131. }
  132. return implode('', array_slice($this->tokens, $start));
  133. }//end getLongComment()
  134. /**
  135. * Returns the start position of the long comment description.
  136. *
  137. * Returns -1 if there is no long comment.
  138. *
  139. * @return int The start position of the long comment description.
  140. * @see _getShortCommentEndPos()
  141. */
  142. private function _getLongCommentStartPos()
  143. {
  144. $pos = ($this->_getShortCommentEndPos() + 1);
  145. if ($pos === (count($this->tokens) - 1)) {
  146. return -1;
  147. }
  148. $count = count($this->tokens);
  149. for ($i = $pos; $i < $count; $i++) {
  150. $content = trim($this->tokens[$i]);
  151. if ($content !== '') {
  152. if ($content{0} === '@') {
  153. return -1;
  154. }
  155. return $i;
  156. }
  157. }
  158. return -1;
  159. }//end _getLongCommentStartPos()
  160. /**
  161. * Returns the whitespace that exists between
  162. * the short and the long comment description.
  163. *
  164. * @return string
  165. */
  166. public function getWhiteSpaceBetween()
  167. {
  168. $endShort = ($this->_getShortCommentEndPos() + 1);
  169. $startLong = ($this->_getLongCommentStartPos() - 1);
  170. if ($startLong === -1) {
  171. return '';
  172. }
  173. return implode(
  174. '',
  175. array_slice($this->tokens, $endShort, ($startLong - $endShort))
  176. );
  177. }//end getWhiteSpaceBetween()
  178. /**
  179. * Returns the number of newlines that exist before the tags.
  180. *
  181. * @return int
  182. */
  183. public function getNewlineAfter()
  184. {
  185. $long = $this->getLongComment();
  186. if ($long !== '') {
  187. $long = rtrim($long, ' ');
  188. $long = strrev($long);
  189. $newlines = strspn($long, $this->phpcsFile->eolChar);
  190. } else {
  191. $endShort = ($this->_getShortCommentEndPos() + 1);
  192. $after = implode('', array_slice($this->tokens, $endShort));
  193. $after = trim($after, ' ');
  194. $newlines = strspn($after, $this->phpcsFile->eolChar);
  195. }
  196. return ($newlines / strlen($this->phpcsFile->eolChar));
  197. }//end getNewlineAfter()
  198. /**
  199. * Returns true if there is no comment.
  200. *
  201. * @return boolean
  202. */
  203. public function isEmpty()
  204. {
  205. return (trim($this->getContent()) === '');
  206. }//end isEmpty()
  207. }//end class
  208. ?>