PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/.dev/code-sniffs/XLite/Sniffs/CSS/Commenting/FileCommentSniff.php

https://github.com/istran/core
PHP | 256 lines | 164 code | 27 blank | 65 comment | 19 complexity | ed5c6a9feae8fd7e3b86ca93a2120566 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  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: 9640f2e9a71a48788c35009e78dcd098ebc1f8c7 $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Parses and verifies the doc comments for files.
  18. *
  19. * Verifies that :
  20. * <ul>
  21. * <li>A doc comment exists.</li>
  22. * <li>There is a blank newline after the short description.</li>
  23. * <li>There is a blank newline between the long and short description.</li>
  24. * <li>There is a blank newline between the long description and tags.</li>
  25. * <li>A PHP version is specified.</li>
  26. * <li>Check the order of the tags.</li>
  27. * <li>Check the indentation of each tag.</li>
  28. * <li>Check required and optional tags and the format of their content.</li>
  29. * </ul>
  30. *
  31. * @category PHP
  32. * @package PHP_CodeSniffer
  33. * @author Greg Sherwood <gsherwood@squiz.net>
  34. * @author Marc McIntyre <mmcintyre@squiz.net>
  35. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  36. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  37. * @version Release: 1.2.0RC1
  38. * @link http://pear.php.net/package/PHP_CodeSniffer
  39. */
  40. class XLite_Sniffs_CSS_Commenting_FileCommentSniff extends XLite_TagsSniff
  41. {
  42. /**
  43. * A list of tokenizers this sniff supports.
  44. *
  45. * @var array
  46. */
  47. public $supportedTokenizers = array('CSS');
  48. /**
  49. * Tags in correct order and related info.
  50. *
  51. * @var array
  52. */
  53. protected $tags = array (
  54. 'author' => array(
  55. 'required' => true,
  56. 'allow_multiple' => false,
  57. 'order_text' => 'follows @subpackage (if used) or @package',
  58. ),
  59. 'copyright' => array(
  60. 'required' => true,
  61. 'allow_multiple' => false,
  62. 'order_text' => 'follows @author',
  63. ),
  64. 'license' => array(
  65. 'required' => true,
  66. 'allow_multiple' => false,
  67. 'order_text' => 'follows @copyright (if used) or @author',
  68. ),
  69. 'version' => array(
  70. 'required' => true,
  71. 'allow_multiple' => false,
  72. 'order_text' => 'follows @license',
  73. ),
  74. 'link' => array(
  75. 'required' => true,
  76. 'allow_multiple' => false,
  77. 'order_text' => 'follows @version',
  78. ),
  79. 'since' => array(
  80. 'required' => false,
  81. 'allow_multiple' => false,
  82. 'order_text' => 'follows @see (if used) or @link',
  83. ),
  84. );
  85. protected $reqCodesWrongFormat = array(
  86. 'author' => array(
  87. 'code' => 'REQ.CSS.4.1.13',
  88. 'function' => 'getAuthors',
  89. 'type' => 'array',
  90. ),
  91. 'copyright' => array(
  92. 'code' => 'REQ.CSS.4.1.14',
  93. 'function' => 'getCopyrights',
  94. 'type' => 'array',
  95. ),
  96. 'license' => array(
  97. 'code' => 'REQ.CSS.4.1.15',
  98. 'function' => 'getLicense',
  99. 'type' => 'single',
  100. ),
  101. 'version' => array(
  102. 'code' => 'REQ.CSS.4.1.16',
  103. 'function' => 'getVersion',
  104. 'type' => 'single',
  105. ),
  106. );
  107. protected $reqCodeRequire = 'REQ.CSS.4.1.5';
  108. protected $reqCodeIndent = 'REQ.CSS.4.1.8';
  109. /**
  110. * Processes this test, when one of its tokens is encountered.
  111. *
  112. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  113. * @param int $stackPtr The position of the current token
  114. * in the stack passed in $tokens.
  115. *
  116. * @return void
  117. */
  118. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  119. {
  120. $this->currentFile = $phpcsFile;
  121. if ($stackPtr !== 0) {
  122. if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
  123. return;
  124. }
  125. }
  126. $tokens = $phpcsFile->getTokens();
  127. // Find the next non whitespace token.
  128. $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr+1), null, true);
  129. if ($tokens[$commentStart]['code'] !== T_COMMENT) {
  130. $phpcsFile->addError(
  131. $this->getReqPrefix('REQ.CSS.4.1.1') . 'Файл должен начинаться с однострочного комментария',
  132. $commentStart
  133. );
  134. } elseif (!preg_match('/vim: set ts=\d sw=\d sts=\d et:/', $tokens[$commentStart]['content'])) {
  135. $phpcsFile->addError(
  136. $this->getReqPrefix('REQ.CSS.4.1.1') . 'Файл должен начинаться с однострочного комментария с директивой для vim',
  137. $commentStart
  138. );
  139. }
  140. $commentStart = $phpcsFile->findNext(
  141. T_WHITESPACE,
  142. ($commentStart + 1),
  143. null,
  144. true
  145. );
  146. $errorToken = ($stackPtr + 1);
  147. if (isset($tokens[$errorToken]) === false) {
  148. $errorToken--;
  149. }
  150. if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
  151. // We are only interested if this is the first open tag.
  152. return;
  153. } else if ($tokens[$commentStart]['code'] === T_COMMENT) {
  154. $error = 'You must use "/**" style comments for a file comment';
  155. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.2') . $error, $errorToken);
  156. return;
  157. } else if ($commentStart === false
  158. || $tokens[$commentStart]['code'] !== T_DOC_COMMENT
  159. ) {
  160. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.2') . 'Missing file doc comment', $errorToken);
  161. return;
  162. } else {
  163. // Extract the header comment docblock.
  164. $commentEnd = $phpcsFile->findNext(
  165. T_DOC_COMMENT,
  166. ($commentStart + 1),
  167. null,
  168. true
  169. );
  170. $commentEnd--;
  171. $comment = $phpcsFile->getTokensAsString(
  172. $commentStart,
  173. ($commentEnd - $commentStart + 1)
  174. );
  175. // Parse the header comment docblock.
  176. try {
  177. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
  178. $this->commentParser->parse();
  179. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  180. $line = ($e->getLineWithinComment() + $commentStart);
  181. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.1') . $e->getMessage(), $line);
  182. return;
  183. }
  184. $comment = $this->commentParser->getComment();
  185. if (is_null($comment) === true) {
  186. $error = 'File doc comment is empty';
  187. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.3.2') . $error, $commentStart);
  188. return;
  189. }
  190. // No extra newline before short description.
  191. $short = $comment->getShortComment();
  192. $newlineCount = 0;
  193. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  194. if ($short !== '' && $newlineSpan > 0) {
  195. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  196. $error = "Extra $line found before file comment short description";
  197. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.6') . $error, ($commentStart + 1));
  198. }
  199. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  200. // Exactly one blank line between short and long description.
  201. $long = $comment->getLongComment();
  202. if (empty($long) === false) {
  203. $between = $comment->getWhiteSpaceBetween();
  204. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  205. if ($newlineBetween !== 2) {
  206. $error = 'There must be exactly one blank line between descriptions in file comment';
  207. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.6') . $error, ($commentStart + $newlineCount + 1));
  208. }
  209. $newlineCount += $newlineBetween;
  210. }
  211. // Exactly one blank line before tags.
  212. $tags = $this->commentParser->getTagOrders();
  213. if (count($tags) > 1) {
  214. $newlineSpan = $comment->getNewlineAfter();
  215. if ($newlineSpan !== 2) {
  216. $error = 'There must be exactly one blank line before the tags in file comment';
  217. if ($long !== '') {
  218. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  219. }
  220. $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.6') . $error, ($commentStart + $newlineCount));
  221. $short = rtrim($short, $phpcsFile->eolChar.' ');
  222. }
  223. }
  224. // Check each tag.
  225. $this->processTags($commentStart, $commentEnd);
  226. }//end if
  227. }//end process()
  228. }//end class
  229. ?>