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

/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 254 lines | 152 code | 34 blank | 68 comment | 33 complexity | 199da85f435e2f03965da7733c62b542 MD5 | raw file
  1. <?php
  2. /**
  3. * Parses and verifies the class 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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
  16. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found');
  17. }
  18. /**
  19. * Parses and verifies the class doc comment.
  20. *
  21. * Verifies that :
  22. * <ul>
  23. * <li>A class doc comment exists.</li>
  24. * <li>There is exactly one blank line before the class comment.</li>
  25. * <li>Short description ends with a full stop.</li>
  26. * <li>There is a blank line after the short description.</li>
  27. * <li>Each paragraph of the long description ends with a full stop.</li>
  28. * <li>There is a blank line between the description and the tags.</li>
  29. * <li>Check the format of the since tag (x.x.x).</li>
  30. * </ul>
  31. *
  32. * @category PHP
  33. * @package PHP_CodeSniffer
  34. * @author Greg Sherwood <gsherwood@squiz.net>
  35. * @author Marc McIntyre <mmcintyre@squiz.net>
  36. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  37. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  38. * @version Release: @package_version@
  39. * @link http://pear.php.net/package/PHP_CodeSniffer
  40. */
  41. class Squiz_Sniffs_Commenting_ClassCommentSniff implements PHP_CodeSniffer_Sniff
  42. {
  43. /**
  44. * Returns an array of tokens this test wants to listen for.
  45. *
  46. * @return array
  47. */
  48. public function register()
  49. {
  50. return array(T_CLASS);
  51. }//end register()
  52. /**
  53. * Processes this test, when one of its tokens is encountered.
  54. *
  55. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  56. * @param int $stackPtr The position of the current token
  57. * in the stack passed in $tokens.
  58. *
  59. * @return void
  60. */
  61. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  62. {
  63. $this->currentFile = $phpcsFile;
  64. $tokens = $phpcsFile->getTokens();
  65. $find = array(
  66. T_ABSTRACT,
  67. T_WHITESPACE,
  68. T_FINAL,
  69. );
  70. // Extract the class comment docblock.
  71. $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
  72. if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
  73. $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr, 'WrongStyle');
  74. return;
  75. } else if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
  76. $phpcsFile->addError('Missing class doc comment', $stackPtr, 'Missing');
  77. return;
  78. }
  79. $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
  80. $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
  81. // Distinguish file and class comment.
  82. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1));
  83. if ($prevClassToken === false) {
  84. // This is the first class token in this file, need extra checks.
  85. $prevNonComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentStart - 1), null, true);
  86. if ($prevNonComment !== false) {
  87. $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1));
  88. if ($prevComment === false) {
  89. // There is only 1 doc comment between open tag and class token.
  90. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
  91. if ($newlineToken !== false) {
  92. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, $phpcsFile->eolChar);
  93. if ($newlineToken !== false) {
  94. // Blank line between the class and the doc block.
  95. // The doc block is most likely a file comment.
  96. $phpcsFile->addError('Missing class doc comment', ($stackPtr + 1), 'Missing');
  97. return;
  98. }
  99. }//end if
  100. }//end if
  101. // Exactly one blank line before the class comment.
  102. $prevTokenEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
  103. if ($prevTokenEnd !== false) {
  104. $blankLineBefore = 0;
  105. for ($i = ($prevTokenEnd + 1); $i < $commentStart; $i++) {
  106. if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) {
  107. $blankLineBefore++;
  108. }
  109. }
  110. if ($blankLineBefore !== 2) {
  111. $error = 'There must be exactly one blank line before the class comment';
  112. $phpcsFile->addError($error, ($commentStart - 1), 'SpacingBefore');
  113. }
  114. }
  115. }//end if
  116. }//end if
  117. $commentString = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  118. // Parse the class comment docblock.
  119. try {
  120. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($commentString, $phpcsFile);
  121. $this->commentParser->parse();
  122. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  123. $line = ($e->getLineWithinComment() + $commentStart);
  124. $phpcsFile->addError($e->getMessage(), $line, 'FailedParse');
  125. return;
  126. }
  127. $comment = $this->commentParser->getComment();
  128. if (is_null($comment) === true) {
  129. $error = 'Class doc comment is empty';
  130. $phpcsFile->addError($error, $commentStart, 'Empty');
  131. return;
  132. }
  133. // The first line of the comment should just be the /** code.
  134. $eolPos = strpos($commentString, $phpcsFile->eolChar);
  135. $firstLine = substr($commentString, 0, $eolPos);
  136. if ($firstLine !== '/**') {
  137. $error = 'The open comment tag must be the only content on the line';
  138. $phpcsFile->addError($error, $commentStart, 'SpacingAfterOpen');
  139. }
  140. // Check for a comment description.
  141. $short = rtrim($comment->getShortComment(), $phpcsFile->eolChar);
  142. if (trim($short) === '') {
  143. $error = 'Missing short description in class doc comment';
  144. $phpcsFile->addError($error, $commentStart, 'MissingShort');
  145. return;
  146. }
  147. // No extra newline before short description.
  148. $newlineCount = 0;
  149. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  150. if ($short !== '' && $newlineSpan > 0) {
  151. $error = 'Extra newline(s) found before class comment short description';
  152. $phpcsFile->addError($error, ($commentStart + 1), 'SpacingBeforeShort');
  153. }
  154. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  155. // Exactly one blank line between short and long description.
  156. $long = $comment->getLongComment();
  157. if (empty($long) === false) {
  158. $between = $comment->getWhiteSpaceBetween();
  159. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  160. if ($newlineBetween !== 2) {
  161. $error = 'There must be exactly one blank line between descriptions in class comment';
  162. $phpcsFile->addError($error, ($commentStart + $newlineCount + 1), 'SpacingBetween');
  163. }
  164. $newlineCount += $newlineBetween;
  165. $testLong = trim($long);
  166. if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
  167. $error = 'Class comment long description must start with a capital letter';
  168. $phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
  169. }
  170. }
  171. // Exactly one blank line before tags.
  172. $tags = $this->commentParser->getTagOrders();
  173. if (count($tags) > 1) {
  174. $newlineSpan = $comment->getNewlineAfter();
  175. if ($newlineSpan !== 2) {
  176. $error = 'There must be exactly one blank line before the tags in class comment';
  177. if ($long !== '') {
  178. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  179. }
  180. $phpcsFile->addError($error, ($commentStart + $newlineCount), 'SpacingBeforeTags');
  181. $short = rtrim($short, $phpcsFile->eolChar.' ');
  182. }
  183. }
  184. // Short description must be single line and end with a full stop.
  185. $testShort = trim($short);
  186. $lastChar = $testShort[(strlen($testShort) - 1)];
  187. if (substr_count($testShort, $phpcsFile->eolChar) !== 0) {
  188. $error = 'Class comment short description must be on a single line';
  189. $phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
  190. }
  191. if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
  192. $error = 'Class comment short description must start with a capital letter';
  193. $phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
  194. }
  195. if ($lastChar !== '.') {
  196. $error = 'Class comment short description must end with a full stop';
  197. $phpcsFile->addError($error, ($commentStart + 1), 'ShortFullStop');
  198. }
  199. // No tags are allowed in the class comment.
  200. $tags = $this->commentParser->getTags();
  201. foreach ($tags as $errorTag) {
  202. $error = '@%s tag is not allowed in class comment';
  203. $data = array($errorTag['tag']);
  204. $phpcsFile->addWarning($error, ($commentStart + $errorTag['line']), 'TagNotAllowed', $data);
  205. }
  206. // The last content should be a newline and the content before
  207. // that should not be blank. If there is more blank space
  208. // then they have additional blank lines at the end of the comment.
  209. $words = $this->commentParser->getWords();
  210. $lastPos = (count($words) - 1);
  211. if (trim($words[($lastPos - 1)]) !== ''
  212. || strpos($words[($lastPos - 1)], $this->currentFile->eolChar) === false
  213. || trim($words[($lastPos - 2)]) === ''
  214. ) {
  215. $error = 'Additional blank lines found at end of class comment';
  216. $this->currentFile->addError($error, $commentEnd, 'SpacingAfter');
  217. }
  218. }//end process()
  219. }//end class
  220. ?>