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

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

https://github.com/istran/core
PHP | 300 lines | 189 code | 36 blank | 75 comment | 22 complexity | 2aed577fb633aba9c52b5597f0157e7d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Parses and verifies the doc comments for functions.
  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: ee3d08ebdd16c8ebc0275671310b819af7348a28 $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_CommentParser_FunctionCommentParser', true) === false) {
  17. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_FunctionCommentParser not found');
  18. }
  19. /**
  20. * Parses and verifies the doc comments for functions.
  21. *
  22. * Verifies that :
  23. * <ul>
  24. * <li>A 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>Parameter names represent those in the method.</li>
  29. * <li>Parameter comments are in the correct order</li>
  30. * <li>Parameter comments are complete</li>
  31. * <li>A space is present before the first and after the last parameter</li>
  32. * <li>A return type exists</li>
  33. * <li>There must be one blank line between body and headline comments.</li>
  34. * <li>Any throw tag must have an exception class.</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.2.0RC1
  44. * @link http://pear.php.net/package/PHP_CodeSniffer
  45. */
  46. class XLite_Sniffs_PHP_Commenting_ClassVarCommentSniff extends XLite_TagsSniff
  47. {
  48. /**
  49. * The function comment parser for the current method.
  50. *
  51. * @var PHP_CodeSniffer_Comment_Parser_FunctionCommentParser
  52. */
  53. protected $commentParser = null;
  54. /**
  55. * The current PHP_CodeSniffer_File object we are processing.
  56. *
  57. * @var PHP_CodeSniffer_File
  58. */
  59. protected $currentFile = null;
  60. protected $tags = array(
  61. 'var' => array(
  62. 'required' => true,
  63. 'allow_multiple' => false,
  64. 'order_text' => 'precedes @access',
  65. ),
  66. 'access' => array(
  67. 'required' => true,
  68. 'allow_multiple' => false,
  69. 'order_text' => 'follows @var',
  70. ),
  71. 'see' => array(
  72. 'required' => false,
  73. 'allow_multiple' => false,
  74. 'order_text' => 'follows @since',
  75. ),
  76. 'since' => array(
  77. 'required' => true,
  78. 'allow_multiple' => false,
  79. 'order_text' => 'follows @access',
  80. ),
  81. 'Column' => array(
  82. 'required' => false,
  83. 'allow_multiple' => false,
  84. 'order_text' => 'follows @since',
  85. ),
  86. 'OneToMany' => array(
  87. 'required' => false,
  88. 'allow_multiple' => false,
  89. 'order_text' => 'follows @Column',
  90. ),
  91. 'OneToOne' => array(
  92. 'required' => false,
  93. 'allow_multiple' => false,
  94. 'order_text' => 'follows @Column',
  95. ),
  96. 'ManyToMany' => array(
  97. 'required' => false,
  98. 'allow_multiple' => false,
  99. 'order_text' => 'follows @Column',
  100. ),
  101. 'ManyToOne' => array(
  102. 'required' => false,
  103. 'allow_multiple' => false,
  104. 'order_text' => 'follows @Column',
  105. ),
  106. 'Id' => array(
  107. 'required' => false,
  108. 'allow_multiple' => false,
  109. 'order_text' => 'follows @Column',
  110. ),
  111. 'GeneratedValue' => array(
  112. 'required' => false,
  113. 'allow_multiple' => false,
  114. 'order_text' => 'follows @Column',
  115. ),
  116. 'JoinColumn' => array(
  117. 'required' => false,
  118. 'allow_multiple' => false,
  119. 'order_text' => 'follows @Column',
  120. ),
  121. 'OrderBy' => array(
  122. 'required' => false,
  123. 'allow_multiple' => false,
  124. 'order_text' => 'follows @Column',
  125. ),
  126. );
  127. protected $reqCodeRequire = array('REQ.PHP.4.6.3');
  128. protected $reqCodePHPVersion = false;
  129. protected $reqCodeForbidden = 'REQ.PHP.4.6.3';
  130. protected $reqCodeOnlyOne = 'REQ.PHP.4.6.5';
  131. protected $docBlock = 'variable';
  132. /**
  133. * Returns an array of tokens this test wants to listen for.
  134. *
  135. * @return array
  136. */
  137. public function register()
  138. {
  139. return array(T_VARIABLE);
  140. }//end register()
  141. /**
  142. * Processes this test, when one of its tokens is encountered.
  143. *
  144. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  145. * @param int $stackPtr The position of the current token
  146. * in the stack passed in $tokens.
  147. *
  148. * @return void
  149. */
  150. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  151. {
  152. $find = array(
  153. T_COMMENT,
  154. T_DOC_COMMENT,
  155. T_CLASS,
  156. T_FUNCTION,
  157. T_OPEN_TAG,
  158. );
  159. $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1));
  160. if ($commentEnd === false) {
  161. return;
  162. }
  163. $this->currentFile = $phpcsFile;
  164. $tokens = $phpcsFile->getTokens();
  165. if (isset($tokens[$stackPtr]['nested_parenthesis'])) {
  166. return;
  167. }
  168. if (!isset($tokens[$stackPtr]['conditions'])) {
  169. return;
  170. }
  171. end($tokens[$stackPtr]['conditions']);
  172. $ckey = key($tokens[$stackPtr]['conditions']);
  173. if (!isset($tokens[$ckey]) || ($tokens[$ckey]['code'] !== T_CLASS && $tokens[$ckey]['code'] !== T_INTERFACE)) {
  174. return;
  175. }
  176. // If the token that we found was a class or a function, then this
  177. // function has no doc comment.
  178. $code = $tokens[$commentEnd]['code'];
  179. if ($code === T_COMMENT) {
  180. $error = 'You must use "/**" style comments for a variable comment';
  181. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.4') . $error, $stackPtr);
  182. return;
  183. } else if ($code !== T_DOC_COMMENT) {
  184. $error = 'Missing variable doc comment';
  185. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.1') . $error, $stackPtr);
  186. return;
  187. }
  188. // If there is any code between the function keyword and the doc block
  189. // then the doc block is not for us.
  190. $ignore = PHP_CodeSniffer_Tokens::$scopeModifiers;
  191. $ignore[] = T_STATIC;
  192. $ignore[] = T_WHITESPACE;
  193. $ignore[] = T_ABSTRACT;
  194. $ignore[] = T_FINAL;
  195. $prevToken = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
  196. if ($prevToken !== $commentEnd) {
  197. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.1') . 'Missing function doc comment', $stackPtr);
  198. return;
  199. }
  200. // If the first T_OPEN_TAG is right before the comment, it is probably
  201. // a file comment.
  202. $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
  203. $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
  204. if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
  205. // Is this the first open tag?
  206. if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, ($prevToken - 1)) === false) {
  207. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.1') . 'Missing function doc comment', $stackPtr);
  208. return;
  209. }
  210. }
  211. $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  212. try {
  213. $this->commentParser = new XLite_FunctionCommentParser($comment, $phpcsFile);
  214. $this->commentParser->parse();
  215. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  216. $line = ($e->getLineWithinComment() + $commentStart);
  217. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.1') . $e->getMessage(), $line);
  218. return;
  219. }
  220. $comment = $this->commentParser->getComment();
  221. if (is_null($comment) === true) {
  222. $error = 'Variable doc comment is empty';
  223. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.6.2') . $error, $commentStart);
  224. return;
  225. }
  226. $this->checkAccess($stackPtr, $commentStart, $commentEnd);
  227. $this->processTags($commentStart, $commentEnd);
  228. // No extra newline before short description.
  229. $short = $comment->getShortComment();
  230. $newlineCount = 0;
  231. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  232. if ($short !== '' && $newlineSpan > 0) {
  233. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  234. $error = "Extra $line found before variable comment short description";
  235. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.7') . $error, ($commentStart + 1));
  236. }
  237. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  238. // Exactly one blank line between short and long description.
  239. $long = $comment->getLongComment();
  240. if (empty($long) === false) {
  241. $between = $comment->getWhiteSpaceBetween();
  242. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  243. if ($newlineBetween !== 2) {
  244. $error = 'There must be exactly one blank line between descriptions in variable comment';
  245. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.19') . $error, ($commentStart + $newlineCount + 1));
  246. }
  247. $newlineCount += $newlineBetween;
  248. }
  249. // Exactly one blank line before tags.
  250. $params = $this->commentParser->getTagOrders();
  251. if (count($params) > 1) {
  252. $newlineSpan = $comment->getNewlineAfter();
  253. if ($newlineSpan !== 2) {
  254. $error = 'There must be exactly one blank line before the tags in function comment';
  255. if ($long !== '') {
  256. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  257. }
  258. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.18') . $error, ($commentStart + $newlineCount));
  259. $short = rtrim($short, $phpcsFile->eolChar.' ');
  260. }
  261. }
  262. }//end process()
  263. }//end class
  264. ?>