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

/php/PHP_CodeSniffer/src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php

http://github.com/jonswar/perl-code-tidyall
PHP | 388 lines | 158 code | 21 blank | 209 comment | 17 complexity | 61f2880f5995dbf46c5f6efe635cd4b7 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, 0BSD, MIT
  1. <?php
  2. /**
  3. * Verifies that block comments are used appropriately.
  4. *
  5. * @author Greg Sherwood <gsherwood@squiz.net>
  6. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8. */
  9. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
  10. use PHP_CodeSniffer\Sniffs\Sniff;
  11. use PHP_CodeSniffer\Files\File;
  12. use PHP_CodeSniffer\Util\Tokens;
  13. class BlockCommentSniff implements Sniff
  14. {
  15. /**
  16. * The --tab-width CLI value that is being used.
  17. *
  18. * @var integer
  19. */
  20. private $tabWidth = null;
  21. /**
  22. * Returns an array of tokens this test wants to listen for.
  23. *
  24. * @return array
  25. */
  26. public function register()
  27. {
  28. return [
  29. T_COMMENT,
  30. T_DOC_COMMENT_OPEN_TAG,
  31. ];
  32. }//end register()
  33. /**
  34. * Processes this test, when one of its tokens is encountered.
  35. *
  36. * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
  37. * @param int $stackPtr The position of the current token in the
  38. * stack passed in $tokens.
  39. *
  40. * @return void
  41. */
  42. public function process(File $phpcsFile, $stackPtr)
  43. {
  44. if ($this->tabWidth === null) {
  45. if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) {
  46. // We have no idea how wide tabs are, so assume 4 spaces for fixing.
  47. $this->tabWidth = 4;
  48. } else {
  49. $this->tabWidth = $phpcsFile->config->tabWidth;
  50. }
  51. }
  52. $tokens = $phpcsFile->getTokens();
  53. // If it's an inline comment, return.
  54. if (substr($tokens[$stackPtr]['content'], 0, 2) !== '/*') {
  55. return;
  56. }
  57. // If this is a function/class/interface doc block comment, skip it.
  58. // We are only interested in inline doc block comments.
  59. if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) {
  60. $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  61. $ignore = [
  62. T_CLASS => true,
  63. T_INTERFACE => true,
  64. T_TRAIT => true,
  65. T_FUNCTION => true,
  66. T_PUBLIC => true,
  67. T_PRIVATE => true,
  68. T_FINAL => true,
  69. T_PROTECTED => true,
  70. T_STATIC => true,
  71. T_ABSTRACT => true,
  72. T_CONST => true,
  73. T_VAR => true,
  74. ];
  75. if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
  76. return;
  77. }
  78. $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  79. if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
  80. return;
  81. }
  82. $error = 'Block comments must be started with /*';
  83. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStart');
  84. if ($fix === true) {
  85. $phpcsFile->fixer->replaceToken($stackPtr, '/*');
  86. }
  87. $end = $tokens[$stackPtr]['comment_closer'];
  88. if ($tokens[$end]['content'] !== '*/') {
  89. $error = 'Block comments must be ended with */';
  90. $fix = $phpcsFile->addFixableError($error, $end, 'WrongEnd');
  91. if ($fix === true) {
  92. $phpcsFile->fixer->replaceToken($end, '*/');
  93. }
  94. }
  95. return;
  96. }//end if
  97. $commentLines = [$stackPtr];
  98. $nextComment = $stackPtr;
  99. $lastLine = $tokens[$stackPtr]['line'];
  100. $commentString = $tokens[$stackPtr]['content'];
  101. // Construct the comment into an array.
  102. while (($nextComment = $phpcsFile->findNext(T_WHITESPACE, ($nextComment + 1), null, true)) !== false) {
  103. if ($tokens[$nextComment]['code'] !== $tokens[$stackPtr]['code']
  104. && isset(Tokens::$phpcsCommentTokens[$tokens[$nextComment]['code']]) === false
  105. ) {
  106. // Found the next bit of code.
  107. break;
  108. }
  109. if (($tokens[$nextComment]['line'] - 1) !== $lastLine) {
  110. // Not part of the block.
  111. break;
  112. }
  113. $lastLine = $tokens[$nextComment]['line'];
  114. $commentLines[] = $nextComment;
  115. $commentString .= $tokens[$nextComment]['content'];
  116. if ($tokens[$nextComment]['code'] === T_DOC_COMMENT_CLOSE_TAG
  117. || substr($tokens[$nextComment]['content'], -2) === '*/'
  118. ) {
  119. break;
  120. }
  121. }//end while
  122. $commentText = str_replace($phpcsFile->eolChar, '', $commentString);
  123. $commentText = trim($commentText, "/* \t");
  124. if ($commentText === '') {
  125. $error = 'Empty block comment not allowed';
  126. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty');
  127. if ($fix === true) {
  128. $phpcsFile->fixer->beginChangeset();
  129. $phpcsFile->fixer->replaceToken($stackPtr, '');
  130. $lastToken = array_pop($commentLines);
  131. for ($i = ($stackPtr + 1); $i <= $lastToken; $i++) {
  132. $phpcsFile->fixer->replaceToken($i, '');
  133. }
  134. $phpcsFile->fixer->endChangeset();
  135. }
  136. return;
  137. }
  138. if (count($commentLines) === 1) {
  139. $error = 'Single line block comment not allowed; use inline ("// text") comment instead';
  140. // Only fix comments when they are the last token on a line.
  141. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  142. if ($tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) {
  143. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SingleLine');
  144. if ($fix === true) {
  145. $comment = '// '.$commentText.$phpcsFile->eolChar;
  146. $phpcsFile->fixer->replaceToken($stackPtr, $comment);
  147. }
  148. } else {
  149. $phpcsFile->addError($error, $stackPtr, 'SingleLine');
  150. }
  151. return;
  152. }
  153. $content = trim($tokens[$stackPtr]['content']);
  154. if ($content !== '/*' && $content !== '/**') {
  155. $error = 'Block comment text must start on a new line';
  156. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoNewLine');
  157. if ($fix === true) {
  158. $indent = '';
  159. if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
  160. if (isset($tokens[($stackPtr - 1)]['orig_content']) === true) {
  161. $indent = $tokens[($stackPtr - 1)]['orig_content'];
  162. } else {
  163. $indent = $tokens[($stackPtr - 1)]['content'];
  164. }
  165. }
  166. $comment = preg_replace(
  167. '/^(\s*\/\*\*?)/',
  168. '$1'.$phpcsFile->eolChar.$indent,
  169. $tokens[$stackPtr]['content'],
  170. 1
  171. );
  172. $phpcsFile->fixer->replaceToken($stackPtr, $comment);
  173. }
  174. return;
  175. }//end if
  176. $starColumn = $tokens[$stackPtr]['column'];
  177. $hasStars = false;
  178. // Make sure first line isn't blank.
  179. if (trim($tokens[$commentLines[1]]['content']) === '') {
  180. $error = 'Empty line not allowed at start of comment';
  181. $fix = $phpcsFile->addFixableError($error, $commentLines[1], 'HasEmptyLine');
  182. if ($fix === true) {
  183. $phpcsFile->fixer->replaceToken($commentLines[1], '');
  184. }
  185. } else {
  186. // Check indentation of first line.
  187. $content = $tokens[$commentLines[1]]['content'];
  188. $commentText = ltrim($content);
  189. $leadingSpace = (strlen($content) - strlen($commentText));
  190. $expected = ($starColumn + 3);
  191. if ($commentText[0] === '*') {
  192. $expected = $starColumn;
  193. $hasStars = true;
  194. }
  195. if ($leadingSpace !== $expected) {
  196. $expectedTxt = $expected.' space';
  197. if ($expected !== 1) {
  198. $expectedTxt .= 's';
  199. }
  200. $data = [
  201. $expectedTxt,
  202. $leadingSpace,
  203. ];
  204. $error = 'First line of comment not aligned correctly; expected %s but found %s';
  205. $fix = $phpcsFile->addFixableError($error, $commentLines[1], 'FirstLineIndent', $data);
  206. if ($fix === true) {
  207. if (isset($tokens[$commentLines[1]]['orig_content']) === true
  208. && $tokens[$commentLines[1]]['orig_content'][0] === "\t"
  209. ) {
  210. // Line is indented using tabs.
  211. $padding = str_repeat("\t", floor($expected / $this->tabWidth));
  212. $padding .= str_repeat(' ', ($expected % $this->tabWidth));
  213. } else {
  214. $padding = str_repeat(' ', $expected);
  215. }
  216. $phpcsFile->fixer->replaceToken($commentLines[1], $padding.$commentText);
  217. }
  218. }//end if
  219. if (preg_match('/^\p{Ll}/u', $commentText) === 1) {
  220. $error = 'Block comments must start with a capital letter';
  221. $phpcsFile->addError($error, $commentLines[1], 'NoCapital');
  222. }
  223. }//end if
  224. // Check that each line of the comment is indented past the star.
  225. foreach ($commentLines as $line) {
  226. // First and last lines (comment opener and closer) are handled separately.
  227. if ($line === $commentLines[(count($commentLines) - 1)] || $line === $commentLines[0]) {
  228. continue;
  229. }
  230. // First comment line was handled above.
  231. if ($line === $commentLines[1]) {
  232. continue;
  233. }
  234. // If it's empty, continue.
  235. if (trim($tokens[$line]['content']) === '') {
  236. continue;
  237. }
  238. $commentText = ltrim($tokens[$line]['content']);
  239. $leadingSpace = (strlen($tokens[$line]['content']) - strlen($commentText));
  240. $expected = ($starColumn + 3);
  241. if ($commentText[0] === '*') {
  242. $expected = $starColumn;
  243. $hasStars = true;
  244. }
  245. if ($leadingSpace < $expected) {
  246. $expectedTxt = $expected.' space';
  247. if ($expected !== 1) {
  248. $expectedTxt .= 's';
  249. }
  250. $data = [
  251. $expectedTxt,
  252. $leadingSpace,
  253. ];
  254. $error = 'Comment line indented incorrectly; expected at least %s but found %s';
  255. $fix = $phpcsFile->addFixableError($error, $line, 'LineIndent', $data);
  256. if ($fix === true) {
  257. if (isset($tokens[$line]['orig_content']) === true
  258. && $tokens[$line]['orig_content'][0] === "\t"
  259. ) {
  260. // Line is indented using tabs.
  261. $padding = str_repeat("\t", floor($expected / $this->tabWidth));
  262. $padding .= str_repeat(' ', ($expected % $this->tabWidth));
  263. } else {
  264. $padding = str_repeat(' ', $expected);
  265. }
  266. $phpcsFile->fixer->replaceToken($line, $padding.$commentText);
  267. }
  268. }//end if
  269. }//end foreach
  270. // Finally, test the last line is correct.
  271. $lastIndex = (count($commentLines) - 1);
  272. $content = $tokens[$commentLines[$lastIndex]]['content'];
  273. $commentText = ltrim($content);
  274. if ($commentText !== '*/' && $commentText !== '**/') {
  275. $error = 'Comment closer must be on a new line';
  276. $phpcsFile->addError($error, $commentLines[$lastIndex], 'CloserSameLine');
  277. } else {
  278. $leadingSpace = (strlen($content) - strlen($commentText));
  279. $expected = ($starColumn - 1);
  280. if ($hasStars === true) {
  281. $expected = $starColumn;
  282. }
  283. if ($leadingSpace !== $expected) {
  284. $expectedTxt = $expected.' space';
  285. if ($expected !== 1) {
  286. $expectedTxt .= 's';
  287. }
  288. $data = [
  289. $expectedTxt,
  290. $leadingSpace,
  291. ];
  292. $error = 'Last line of comment aligned incorrectly; expected %s but found %s';
  293. $fix = $phpcsFile->addFixableError($error, $commentLines[$lastIndex], 'LastLineIndent', $data);
  294. if ($fix === true) {
  295. if (isset($tokens[$line]['orig_content']) === true
  296. && $tokens[$line]['orig_content'][0] === "\t"
  297. ) {
  298. // Line is indented using tabs.
  299. $padding = str_repeat("\t", floor($expected / $this->tabWidth));
  300. $padding .= str_repeat(' ', ($expected % $this->tabWidth));
  301. } else {
  302. $padding = str_repeat(' ', $expected);
  303. }
  304. $phpcsFile->fixer->replaceToken($commentLines[$lastIndex], $padding.$commentText);
  305. }
  306. }//end if
  307. }//end if
  308. // Check that the lines before and after this comment are blank.
  309. $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  310. if ((isset($tokens[$contentBefore]['scope_closer']) === true
  311. && $tokens[$contentBefore]['scope_opener'] === $contentBefore)
  312. || $tokens[$contentBefore]['code'] === T_OPEN_TAG
  313. ) {
  314. if (($tokens[$stackPtr]['line'] - $tokens[$contentBefore]['line']) !== 1) {
  315. $error = 'Empty line not required before block comment';
  316. $phpcsFile->addError($error, $stackPtr, 'HasEmptyLineBefore');
  317. }
  318. } else {
  319. if (($tokens[$stackPtr]['line'] - $tokens[$contentBefore]['line']) < 2) {
  320. $error = 'Empty line required before block comment';
  321. $phpcsFile->addError($error, $stackPtr, 'NoEmptyLineBefore');
  322. }
  323. }
  324. $commentCloser = $commentLines[$lastIndex];
  325. $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($commentCloser + 1), null, true);
  326. if ($contentAfter !== false && ($tokens[$contentAfter]['line'] - $tokens[$commentCloser]['line']) < 2) {
  327. $error = 'Empty line required after block comment';
  328. $phpcsFile->addError($error, $commentCloser, 'NoEmptyLineAfter');
  329. }
  330. }//end process()
  331. }//end class