Drupal investigation

Comment.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Tokenizes doc block comments.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. /**
  15. * Tokenizes doc block comments.
  16. *
  17. * @category PHP
  18. * @package PHP_CodeSniffer
  19. * @author Greg Sherwood <gsherwood@squiz.net>
  20. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  21. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  22. * @version Release: @package_version@
  23. * @link http://pear.php.net/package/PHP_CodeSniffer
  24. */
  25. class PHP_CodeSniffer_Tokenizers_Comment
  26. {
  27. /**
  28. * Creates an array of tokens when given some PHP code.
  29. *
  30. * Starts by using token_get_all() but does a lot of extra processing
  31. * to insert information about the context of the token.
  32. *
  33. * @param string $string The string to tokenize.
  34. * @param string $eolChar The EOL character to use for splitting strings.
  35. * @param int $stackPtr The position of the first token in the file.
  36. *
  37. * @return array
  38. */
  39. public function tokenizeString($string, $eolChar, $stackPtr)
  40. {
  41. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  42. echo "\t\t*** START COMMENT TOKENIZING ***".PHP_EOL;
  43. }
  44. $tokens = array();
  45. $numChars = strlen($string);
  46. /*
  47. Doc block comments start with /*, but typically contain an
  48. extra star when they are used for function and class comments.
  49. */
  50. $char = ($numChars - strlen(ltrim($string, '/*')));
  51. $openTag = substr($string, 0, $char);
  52. $string = ltrim($string, '/*');
  53. $tokens[$stackPtr] = array(
  54. 'content' => $openTag,
  55. 'code' => T_DOC_COMMENT_OPEN_TAG,
  56. 'type' => 'T_DOC_COMMENT_OPEN_TAG',
  57. 'comment_tags' => array(),
  58. );
  59. $openPtr = $stackPtr;
  60. $stackPtr++;
  61. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  62. $content = PHP_CodeSniffer::prepareForOutput($openTag);
  63. echo "\t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content".PHP_EOL;
  64. }
  65. /*
  66. Strip off the close tag so it doesn't interfere with any
  67. of our comment line processing. The token will be added to the
  68. stack just before we return it.
  69. */
  70. $closeTag = array(
  71. 'content' => substr($string, strlen(rtrim($string, '/*'))),
  72. 'code' => T_DOC_COMMENT_CLOSE_TAG,
  73. 'type' => 'T_DOC_COMMENT_CLOSE_TAG',
  74. 'comment_opener' => $openPtr,
  75. );
  76. if ($closeTag['content'] === false) {
  77. $closeTag['content'] = '';
  78. }
  79. $string = rtrim($string, '/*');
  80. /*
  81. Process each line of the comment.
  82. */
  83. $lines = explode($eolChar, $string);
  84. $numLines = count($lines);
  85. foreach ($lines as $lineNum => $string) {
  86. if ($lineNum !== ($numLines - 1)) {
  87. $string .= $eolChar;
  88. }
  89. $char = 0;
  90. $numChars = strlen($string);
  91. // We've started a new line, so process the indent.
  92. $space = $this->_collectWhitespace($string, $char, $numChars);
  93. if ($space !== null) {
  94. $tokens[$stackPtr] = $space;
  95. $stackPtr++;
  96. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  97. $content = PHP_CodeSniffer::prepareForOutput($space['content']);
  98. echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content".PHP_EOL;
  99. }
  100. $char += strlen($space['content']);
  101. if ($char === $numChars) {
  102. break;
  103. }
  104. }
  105. if ($string === '') {
  106. continue;
  107. }
  108. if ($string[$char] === '*') {
  109. // This is a function or class doc block line.
  110. $char++;
  111. $tokens[$stackPtr] = array(
  112. 'content' => '*',
  113. 'code' => T_DOC_COMMENT_STAR,
  114. 'type' => 'T_DOC_COMMENT_STAR',
  115. );
  116. $stackPtr++;
  117. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  118. echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *".PHP_EOL;
  119. }
  120. }
  121. // Now we are ready to process the actual content of the line.
  122. $lineTokens = $this->_processLine($string, $eolChar, $char, $numChars);
  123. foreach ($lineTokens as $lineToken) {
  124. $tokens[$stackPtr] = $lineToken;
  125. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  126. $content = PHP_CodeSniffer::prepareForOutput($lineToken['content']);
  127. $type = $lineToken['type'];
  128. echo "\t\tCreate comment token: $type => $content".PHP_EOL;
  129. }
  130. if ($lineToken['code'] === T_DOC_COMMENT_TAG) {
  131. $tokens[$openPtr]['comment_tags'][] = $stackPtr;
  132. }
  133. $stackPtr++;
  134. }
  135. }//end foreach
  136. $tokens[$stackPtr] = $closeTag;
  137. $tokens[$openPtr]['comment_closer'] = $stackPtr;
  138. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  139. $content = PHP_CodeSniffer::prepareForOutput($closeTag['content']);
  140. echo "\t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content".PHP_EOL;
  141. }
  142. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  143. echo "\t\t*** END COMMENT TOKENIZING ***".PHP_EOL;
  144. }
  145. return $tokens;
  146. }//end tokenizeString()
  147. /**
  148. * Process a single line of a comment.
  149. *
  150. * @param string $string The comment string being tokenized.
  151. * @param string $eolChar The EOL character to use for splitting strings.
  152. * @param int $start The position in the string to start processing.
  153. * @param int $end The position in the string to end processing.
  154. *
  155. * @return array
  156. */
  157. private function _processLine($string, $eolChar, $start, $end)
  158. {
  159. $tokens = array();
  160. // Collect content padding.
  161. $space = $this->_collectWhitespace($string, $start, $end);
  162. if ($space !== null) {
  163. $tokens[] = $space;
  164. $start += strlen($space['content']);
  165. }
  166. if (isset($string[$start]) === false) {
  167. return $tokens;
  168. }
  169. if ($string[$start] === '@') {
  170. // The content up until the first whitespace is the tag name.
  171. $matches = array();
  172. preg_match('/@[^\s]+/', $string, $matches, 0, $start);
  173. if (isset($matches[0]) === true) {
  174. $tagName = $matches[0];
  175. $start += strlen($tagName);
  176. $tokens[] = array(
  177. 'content' => $tagName,
  178. 'code' => T_DOC_COMMENT_TAG,
  179. 'type' => 'T_DOC_COMMENT_TAG',
  180. );
  181. // Then there will be some whitespace.
  182. $space = $this->_collectWhitespace($string, $start, $end);
  183. if ($space !== null) {
  184. $tokens[] = $space;
  185. $start += strlen($space['content']);
  186. }
  187. }
  188. }//end if
  189. // Process the rest of the line.
  190. $eol = strpos($string, $eolChar, $start);
  191. if ($eol === false) {
  192. $eol = $end;
  193. }
  194. if ($eol > $start) {
  195. $tokens[] = array(
  196. 'content' => substr($string, $start, ($eol - $start)),
  197. 'code' => T_DOC_COMMENT_STRING,
  198. 'type' => 'T_DOC_COMMENT_STRING',
  199. );
  200. }
  201. if ($eol !== $end) {
  202. $tokens[] = array(
  203. 'content' => substr($string, $eol, strlen($eolChar)),
  204. 'code' => T_DOC_COMMENT_WHITESPACE,
  205. 'type' => 'T_DOC_COMMENT_WHITESPACE',
  206. );
  207. }
  208. return $tokens;
  209. }//end _processLine()
  210. /**
  211. * Collect consecutive whitespace into a single token.
  212. *
  213. * @param string $string The comment string being tokenized.
  214. * @param int $start The position in the string to start processing.
  215. * @param int $end The position in the string to end processing.
  216. *
  217. * @return array|null
  218. */
  219. private function _collectWhitespace($string, $start, $end)
  220. {
  221. $space = '';
  222. for ($start; $start < $end; $start++) {
  223. if ($string[$start] !== ' ' && $string[$start] !== "\t") {
  224. break;
  225. }
  226. $space .= $string[$start];
  227. }
  228. if ($space === '') {
  229. return null;
  230. }
  231. $token = array(
  232. 'content' => $space,
  233. 'code' => T_DOC_COMMENT_WHITESPACE,
  234. 'type' => 'T_DOC_COMMENT_WHITESPACE',
  235. );
  236. return $token;
  237. }//end _collectWhitespace()
  238. }//end class