PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/.dev/code-sniffs/XLite/Sniffs/PHP/Commenting/ClassCommentSniff.php

https://github.com/istran/core
PHP | 292 lines | 200 code | 30 blank | 62 comment | 21 complexity | 939b6103c4270ee9692dbbc073de6e54 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  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: 6660e122e7822915b6358742729c7c2e3e4ac1b4 $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
  17. $error = 'Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found';
  18. throw new PHP_CodeSniffer_Exception($error);
  19. }
  20. if (class_exists('XLite_Sniffs_PHP_Commenting_FileCommentSniff', true) === false) {
  21. $error = 'Class XLite_Sniffs_PHP_Commenting_FileCommentSniff not found';
  22. throw new PHP_CodeSniffer_Exception($error);
  23. }
  24. /**
  25. * Parses and verifies the doc comments for classes.
  26. *
  27. * Verifies that :
  28. * <ul>
  29. * <li>A doc comment exists.</li>
  30. * <li>There is a blank newline after the short description.</li>
  31. * <li>There is a blank newline between the long and short description.</li>
  32. * <li>There is a blank newline between the long description and tags.</li>
  33. * <li>Check the order of the tags.</li>
  34. * <li>Check the indentation of each tag.</li>
  35. * <li>Check required and optional tags and the format of their content.</li>
  36. * </ul>
  37. *
  38. * @category PHP
  39. * @package PHP_CodeSniffer
  40. * @author Greg Sherwood <gsherwood@squiz.net>
  41. * @author Marc McIntyre <mmcintyre@squiz.net>
  42. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  43. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  44. * @version Release: 1.2.0RC1
  45. * @link http://pear.php.net/package/PHP_CodeSniffer
  46. */
  47. class XLite_Sniffs_PHP_Commenting_ClassCommentSniff extends XLite_Sniffs_PHP_Commenting_FileCommentSniff
  48. {
  49. protected $tags = array(
  50. 'package' => array(
  51. 'required' => true,
  52. 'allow_multiple' => false,
  53. 'order_text' => 'precedes @subpackage',
  54. ),
  55. 'subpackage' => array(
  56. 'required' => false,
  57. 'allow_multiple' => false,
  58. 'order_text' => 'follows @package',
  59. ),
  60. 'see' => array(
  61. 'required' => false,
  62. 'allow_multiple' => false,
  63. 'order_text' => 'follows @link',
  64. ),
  65. 'since' => array(
  66. 'required' => false,
  67. 'allow_multiple' => false,
  68. 'order_text' => 'follows @see (if used) or @link',
  69. ),
  70. 'deprecated' => array(
  71. 'required' => false,
  72. 'allow_multiple' => false,
  73. 'order_text' => 'follows @since (if used) or @see (if used) or @link',
  74. ),
  75. 'Entity' => array(
  76. 'required' => false,
  77. 'allow_multiple' => false,
  78. 'order_text' => 'follows @since (if used) or @see (if used) or @link or @deprecated (if used)',
  79. ),
  80. 'Table' => array(
  81. 'required' => false,
  82. 'allow_multiple' => false,
  83. 'order_text' => 'follows @Entity',
  84. ),
  85. 'Index' => array(
  86. 'required' => false,
  87. 'allow_multiple' => false,
  88. 'order_text' => 'follows @Table',
  89. ),
  90. 'UniqueConstraint' => array(
  91. 'required' => false,
  92. 'allow_multiple' => false,
  93. 'order_text' => 'follows @Table',
  94. ),
  95. 'HasLifecycleCallbacks' => array(
  96. 'required' => false,
  97. 'allow_multiple' => false,
  98. 'order_text' => 'follows @Table',
  99. ),
  100. 'InheritanceType' => array(
  101. 'required' => false,
  102. 'allow_multiple' => false,
  103. 'order_text' => 'follows @Table',
  104. ),
  105. 'DiscriminatorColumn' => array(
  106. 'required' => false,
  107. 'allow_multiple' => false,
  108. 'order_text' => 'follows @Table',
  109. ),
  110. 'DiscriminatorMap' => array(
  111. 'required' => false,
  112. 'allow_multiple' => false,
  113. 'order_text' => 'follows @Table',
  114. ),
  115. 'MappedSuperclass' => array(
  116. 'required' => false,
  117. 'allow_multiple' => false,
  118. 'order_text' => 'follows @Table',
  119. ),
  120. 'ListChild' => array(
  121. 'required' => false,
  122. 'allow_multiple' => true,
  123. 'order_text' => 'any place',
  124. ),
  125. );
  126. protected $reqCodeRequire = 'REQ.PHP.4.4.3';
  127. protected $reqCodePHPVersion = false;
  128. protected $reqCodeForbidden = 'REQ.PHP.4.4.7';
  129. protected $reqCodeOnlyOne = 'REQ.PHP.4.4.6';
  130. protected $docBlock = 'class';
  131. /**
  132. * Returns an array of tokens this test wants to listen for.
  133. *
  134. * @return array
  135. */
  136. public function register()
  137. {
  138. return array(
  139. T_CLASS,
  140. T_INTERFACE,
  141. );
  142. }//end register()
  143. /**
  144. * Processes this test, when one of its tokens is encountered.
  145. *
  146. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  147. * @param int $stackPtr The position of the current token
  148. * in the stack passed in $tokens.
  149. *
  150. * @return void
  151. */
  152. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  153. {
  154. $this->currentFile = $phpcsFile;
  155. $tokens = $phpcsFile->getTokens();
  156. $type = strtolower($tokens[$stackPtr]['content']);
  157. $find = array(
  158. T_ABSTRACT,
  159. T_WHITESPACE,
  160. T_FINAL,
  161. );
  162. // Extract the class comment docblock.
  163. $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
  164. if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
  165. $error = "You must use \"/**\" style comments for a $type comment";
  166. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.4.5') . $error, $stackPtr);
  167. return;
  168. } else if ($commentEnd === false
  169. || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT
  170. ) {
  171. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.4.1') . "Missing $type doc comment", $stackPtr);
  172. return;
  173. }
  174. $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
  175. $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
  176. // Distinguish file and class comment.
  177. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1));
  178. if ($prevClassToken === false) {
  179. // This is the first class token in this file, need extra checks.
  180. $prevNonComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentStart - 1), null, true);
  181. if ($prevNonComment !== false) {
  182. $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1));
  183. if ($prevComment === false) {
  184. // There is only 1 doc comment between open tag and class token.
  185. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
  186. if ($newlineToken !== false) {
  187. $newlineToken = $phpcsFile->findNext(
  188. T_WHITESPACE,
  189. ($newlineToken + 1),
  190. $stackPtr,
  191. false,
  192. $phpcsFile->eolChar
  193. );
  194. if ($newlineToken !== false) {
  195. // Blank line between the class and the doc block.
  196. // The doc block is most likely a file comment.
  197. $error = "Missing $type doc comment";
  198. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.6') . $error, ($stackPtr + 1));
  199. return;
  200. }
  201. }//end if
  202. }//end if
  203. }//end if
  204. }//end if
  205. $comment = $phpcsFile->getTokensAsString(
  206. $commentStart,
  207. ($commentEnd - $commentStart + 1)
  208. );
  209. // Parse the class comment.docblock.
  210. try {
  211. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
  212. $this->commentParser->parse();
  213. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  214. $line = ($e->getLineWithinComment() + $commentStart);
  215. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.1') . $e->getMessage(), $line);
  216. return;
  217. }
  218. $comment = $this->commentParser->getComment();
  219. if (is_null($comment) === true) {
  220. $error = ucfirst($type).' doc comment is empty';
  221. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.4.2') . $error, $commentStart);
  222. return;
  223. }
  224. // No extra newline before short description.
  225. $short = $comment->getShortComment();
  226. $newlineCount = 0;
  227. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  228. if ($short !== '' && $newlineSpan > 0) {
  229. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  230. $error = "Extra $line found before $type comment short description";
  231. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.7') . $error, ($commentStart + 1));
  232. }
  233. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  234. // Exactly one blank line between short and long description.
  235. $long = $comment->getLongComment();
  236. if (empty($long) === false) {
  237. $between = $comment->getWhiteSpaceBetween();
  238. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  239. if ($newlineBetween !== 2) {
  240. $error = "There must be exactly one blank line between descriptions in $type comments";
  241. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.18') . $error, ($commentStart + $newlineCount + 1));
  242. }
  243. $newlineCount += $newlineBetween;
  244. }
  245. // Exactly one blank line before tags.
  246. $tags = $this->commentParser->getTagOrders();
  247. if (count($tags) > 1) {
  248. $newlineSpan = $comment->getNewlineAfter();
  249. if ($newlineSpan !== 2) {
  250. $error = "There must be exactly one blank line before the tags in $type comments";
  251. if ($long !== '') {
  252. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  253. }
  254. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.18') . $error, ($commentStart + $newlineCount));
  255. $short = rtrim($short, $phpcsFile->eolChar.' ');
  256. }
  257. }
  258. // Check each tag.
  259. $this->processTags($commentStart, $commentEnd);
  260. }//end process()
  261. }//end class
  262. ?>