/sonar/XP_CodeSniffer/SQLI/CodeSniffer/Standards/xp/Sniffs/Core/ClassHeaderSniff.php

https://github.com/Gamepay/xp-contrib · PHP · 208 lines · 90 code · 30 blank · 88 comment · 18 complexity · f5b4ca805017ce7a213130363b676f3a MD5 · raw file

  1. <?php
  2. /**
  3. * Parses and verifies the doc comments for files.
  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: ClassHeaderSniff.php 12553 2010-12-08 17:09:23Z gelli $
  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. /**
  20. * Parses and verifies the doc comments for files.
  21. *
  22. * Verifies that :
  23. * <ul>
  24. * <li>A doc comment exists.</li>
  25. * <li>There is a blank newline after the short description.</li>
  26. * <li>There is a blank newline between the long and short description.</li>
  27. * <li>There is a blank newline between the long description and tags.</li>
  28. * <li>A PHP version is specified.</li>
  29. * <li>Check the order of the tags.</li>
  30. * <li>Check the indentation of each tag.</li>
  31. * <li>Check required and optional tags and the format of their content.</li>
  32. * </ul>
  33. *
  34. * @category PHP
  35. * @package PHP_CodeSniffer
  36. * @author Greg Sherwood <gsherwood@squiz.net>
  37. * @author Marc McIntyre <mmcintyre@squiz.net>
  38. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  39. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  40. * @version Release: 1.2.0
  41. * @link http://pear.php.net/package/PHP_CodeSniffer
  42. */
  43. class xp_Sniffs_Core_ClassHeaderSniff implements PHP_CodeSniffer_Sniff
  44. {
  45. /**
  46. * The header comment parser for the current file.
  47. *
  48. * @var PHP_CodeSniffer_Comment_Parser_ClassCommentParser
  49. */
  50. protected $commentParser = null;
  51. /**
  52. * The current PHP_CodeSniffer_File object we are processing.
  53. *
  54. * @var PHP_CodeSniffer_File
  55. */
  56. protected $currentFile = null;
  57. /**
  58. * Returns an array of tokens this test wants to listen for.
  59. *
  60. * @return array
  61. */
  62. public function register()
  63. {
  64. return array(T_OPEN_TAG);
  65. }//end register()
  66. /**
  67. * Processes this test, when one of its tokens is encountered.
  68. *
  69. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  70. * @param int $stackPtr The position of the current token
  71. * in the stack passed in $tokens.
  72. *
  73. * @return void
  74. */
  75. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  76. {
  77. $this->currentFile = $phpcsFile;
  78. // We are only interested if this is the first open tag.
  79. if ($stackPtr !== 0) {
  80. if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
  81. return;
  82. }
  83. }
  84. $tokens = $phpcsFile->getTokens();
  85. // Find the next non whitespace token.
  86. $commentStart
  87. = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  88. if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
  89. // We are only interested if this is the first open tag.
  90. return;
  91. } else if ($tokens[$commentStart]['code'] !== T_COMMENT) {
  92. //$error = 'You must use "/*" style comments for a file comment';
  93. //$phpcsFile->addError($error, $commentStart);
  94. $phpcsFile->addEvent('XP_CLASS_HEADER_INVALID', $commentStart);
  95. return;
  96. } else if ($commentStart === false
  97. || $tokens[$commentStart]['code'] !== T_COMMENT
  98. ) {
  99. //$phpcsFile->addError('Missing file doc comment', $errorToken);
  100. $phpcsFile->addEvent('XP_CLASS_HEADER_MISSING', $commentStart);
  101. return;
  102. } else {
  103. // File header must directly follow opening tag
  104. if ($tokens[$commentStart]['line'] !== $tokens[$stackPtr]['line']+1) {
  105. //$error = 'File header not directly following open tag';
  106. //$phpcsFile->addError($error, $commentStart);
  107. $phpcsFile->addEvent('XP_CLASS_HEADER_NOT_AFTER_OPEN_TAG', $commentStart);
  108. }
  109. // Extract the header comment docblock.
  110. $commentEnd = $phpcsFile->findNext(
  111. T_COMMENT,
  112. ($commentStart + 1),
  113. null,
  114. true
  115. ) - 1;
  116. $comment = $phpcsFile->getTokensAsString(
  117. $commentStart,
  118. ($commentEnd - $commentStart + 1)
  119. );
  120. // Parse the header comment docblock.
  121. try {
  122. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
  123. $this->commentParser->parse();
  124. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  125. $line = ($e->getLineWithinComment() + $commentStart);
  126. //$phpcsFile->addError($e->getMessage(), $line);
  127. $phpcsFile->addEvent('XP_CLASS_HEADER_EXCEPTION', array('message' => $e->getMessage()), $line);
  128. return;
  129. }
  130. $comment = $this->commentParser->getComment();
  131. // No extra newline before short description.
  132. $short = $comment->getShortComment();
  133. $newlineCount = 0;
  134. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  135. if ($short !== '' && $newlineSpan > 0) {
  136. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  137. //$error = "Extra $line found before file comment short description";
  138. //$phpcsFile->addError($error, ($commentStart + 1));
  139. $phpcsFile->addEvent('XP_CLASS_HEADER_NEWLINE_BEFORE_SHORT_DESCRIPTION', array(), ($commentStart + 1));
  140. }
  141. $found= FALSE;
  142. if (strstr(trim($short), '$Id')) {
  143. //$error = "There must be an empty line between SVN ID tag and the description";
  144. //$phpcsFile->addError($error, ($commentStart));
  145. $phpcsFile->addEvent('XP_CLASS_HEADER_EMPTYLINE_BETWEEN_SHORT_DESCRIPTION_AND_SVNID', array(), $commentStart + 1);
  146. $found= TRUE;
  147. }
  148. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  149. // Exactly one blank line between short and long description.
  150. $long = $comment->getLongComment();
  151. if (empty($long) === false) {
  152. $between = $comment->getWhiteSpaceBetween();
  153. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  154. if ($newlineBetween !== 2) {
  155. //$error = 'There must be exactly one blank line between description and the ID Tag';
  156. //$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
  157. $phpcsFile->addEvent('XP_CLASS_HEADER_EMPTYLINE_BETWEEN_DESCRIPTION_AND_ID', array(), ($commentStart + $newlineCount + 1));
  158. }
  159. if (!strstr(trim($long), '$Id')) {
  160. //$error = 'SVN Id tag is corrupt';
  161. //$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
  162. $phpcsFile->addEvent('XP_CLASS_HEADER_SVNID_CORRUPT', array(), ($commentStart + $newlineCount + 1));
  163. }
  164. $newlineCount += $newlineBetween;
  165. } elseif (!$found) {
  166. //$error = 'SVN Id tag is missing';
  167. //$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
  168. $phpcsFile->addEvent('XP_CLASS_HEADER_SVNID_MISSING', array(), ($commentStart + $newlineCount + 1));
  169. }
  170. if (2 < $comment->getNewlineAfter()) {
  171. //$error = 'Extra content after SVN ID Tag';
  172. //$phpcsFile->addError($error, ($commentStart + $newlineCount));
  173. $phpcsFile->addEvent('XP_CLASS_HEADER_CONTENT_AFTER_SVNID', array(), ($commentStart + $newlineCount));
  174. }
  175. }//end if
  176. }//end process()
  177. }//end class
  178. ?>