/test/CodeSniffer/Standards/Xodoa/Sniffs/Commenting/ClassCommentSniff.php

https://github.com/benlumley/Elastica · PHP · 211 lines · 114 code · 28 blank · 69 comment · 24 complexity · 1ddf09cde90ba779df0d7b19d77e77f8 MD5 · raw file

  1. <?php
  2. /**
  3. * Parses and verifies the doc comments for classes.
  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: ClassCommentSniff.php,v 1.17 2007/11/26 22:11:18 squiz Exp $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
  17. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found');
  18. }
  19. require_once dirname(__FILE__). '/FileCommentSniff.php';
  20. if (class_exists('Xodoa_Sniffs_Commenting_FileCommentSniff', true) === false) {
  21. throw new PHP_CodeSniffer_Exception('Class Xodoa_Sniffs_Commenting_FileCommentSniff not found');
  22. }
  23. /**
  24. * Parses and verifies the doc comments for classes.
  25. *
  26. * Verifies that :
  27. * <ul>
  28. * <li>A doc comment exists.</li>
  29. * <li>There is a blank newline after the short description.</li>
  30. * <li>There is a blank newline between the long and short description.</li>
  31. * <li>There is a blank newline between the long description and tags.</li>
  32. * <li>Check the order of the tags.</li>
  33. * <li>Check the indentation of each tag.</li>
  34. * <li>Check required and optional tags and the format of their content.</li>
  35. * </ul>
  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: 1.0.1
  44. * @link http://pear.php.net/package/PHP_CodeSniffer
  45. */
  46. class Xodoa_Sniffs_Commenting_ClassCommentSniff extends Xodoa_Sniffs_Commenting_FileCommentSniff
  47. {
  48. /**
  49. * Returns an array of tokens this test wants to listen for.
  50. *
  51. * @return array
  52. */
  53. public function register()
  54. {
  55. return array(T_CLASS);
  56. }//end register()
  57. /**
  58. * Processes this test, when one of its tokens is encountered.
  59. *
  60. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  61. * @param int $stackPtr The position of the current token in the
  62. * stack passed in $tokens.
  63. *
  64. * @return void
  65. */
  66. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  67. {
  68. $this->currentFile = $phpcsFile;
  69. $tokens = $phpcsFile->getTokens();
  70. $find = array(
  71. T_ABSTRACT,
  72. T_WHITESPACE,
  73. T_FINAL,
  74. );
  75. // Extract the class comment docblock.
  76. $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
  77. if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
  78. $phpcsFile->addError('You must use "/**" style comments for a class comment', $stackPtr);
  79. return;
  80. } else if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
  81. $phpcsFile->addError('Missing class doc comment', $stackPtr);
  82. return;
  83. }
  84. $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
  85. $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
  86. // Distinguish file and class comment.
  87. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1));
  88. if ($prevClassToken === false) {
  89. // This is the first class token in this file, need extra checks.
  90. $prevNonComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentStart - 1), null, true);
  91. if ($prevNonComment !== false) {
  92. $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1));
  93. if ($prevComment === false) {
  94. // There is only 1 doc comment between open tag and class token.
  95. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
  96. if ($newlineToken !== false) {
  97. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, $phpcsFile->eolChar);
  98. if ($newlineToken !== false) {
  99. // Blank line between the class and the doc block.
  100. // The doc block is most likely a file comment.
  101. $phpcsFile->addError('Missing class doc comment', ($stackPtr + 1));
  102. return;
  103. }
  104. }//end if
  105. }//end if
  106. }//end if
  107. }//end if
  108. $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  109. // Parse the class comment.docblock.
  110. try {
  111. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
  112. $this->commentParser->parse();
  113. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  114. $line = ($e->getLineWithinComment() + $commentStart);
  115. $phpcsFile->addError($e->getMessage(), $line);
  116. return;
  117. }
  118. $comment = $this->commentParser->getComment();
  119. if (is_null($comment) === true) {
  120. $error = 'Class doc comment is empty';
  121. $phpcsFile->addError($error, $commentStart);
  122. return;
  123. }
  124. // No extra newline before short description.
  125. $short = $comment->getShortComment();
  126. $newlineCount = 0;
  127. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  128. if ($short !== '' && $newlineSpan > 0) {
  129. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  130. $error = "Extra $line found before class comment short description";
  131. $phpcsFile->addError($error, ($commentStart + 1));
  132. }
  133. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  134. // Exactly one blank line between short and long description.
  135. $long = $comment->getLongComment();
  136. if (empty($long) === false) {
  137. $between = $comment->getWhiteSpaceBetween();
  138. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  139. if ($newlineBetween !== 2) {
  140. $error = 'There must be exactly one blank line between descriptions in class comment';
  141. $phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
  142. }
  143. $newlineCount += $newlineBetween;
  144. }
  145. // Exactly one blank line before tags.
  146. $tags = $this->commentParser->getTagOrders();
  147. if (count($tags) > 1) {
  148. $newlineSpan = $comment->getNewlineAfter();
  149. if ($newlineSpan !== 2) {
  150. $error = 'There must be exactly one blank line before the tags in class comment';
  151. if ($long !== '') {
  152. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  153. }
  154. $phpcsFile->addError($error, ($commentStart + $newlineCount));
  155. $short = rtrim($short, $phpcsFile->eolChar.' ');
  156. }
  157. }
  158. // Check each tag.
  159. $this->processTags($commentStart, $commentEnd);
  160. }//end process()
  161. /**
  162. * Process the version tag.
  163. *
  164. * @param int $errorPos The line number where the error occurs.
  165. *
  166. * @return void
  167. */
  168. protected function processVersion($errorPos)
  169. {
  170. $version = $this->commentParser->getVersion();
  171. if ($version !== null) {
  172. $content = $version->getContent();
  173. $matches = array();
  174. if (empty($content) === true) {
  175. $error = 'Content missing for @version tag in class comment';
  176. $this->currentFile->addError($error, $errorPos);
  177. } else if ((strstr($content, 'Release:') === false)) {
  178. $error = "Invalid version \"$content\" in class comment; consider \"Release: <package_version>\" instead";
  179. $this->currentFile->addWarning($error, $errorPos);
  180. }
  181. }
  182. }//end processVersion()
  183. }//end class