PageRenderTime 111ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/istran/core
PHP | 310 lines | 206 code | 37 blank | 67 comment | 24 complexity | 244675eaaa5d41ddfa5c8077c162ea3e 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: fc8e74ee8c1e42ce060817653579cd5219024aed $
  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.0RC1
  41. * @link http://pear.php.net/package/PHP_CodeSniffer
  42. */
  43. class XLite_Sniffs_PHP_Commenting_FileCommentSniff extends XLite_TagsSniff
  44. {
  45. /**
  46. * Tags in correct order and related info.
  47. *
  48. * @var array
  49. */
  50. protected $tags = array(
  51. 'category' => array(
  52. 'required' => true,
  53. 'allow_multiple' => false,
  54. 'order_text' => 'precedes @package',
  55. ),
  56. 'package' => array(
  57. 'required' => true,
  58. 'allow_multiple' => false,
  59. 'order_text' => 'follows @category',
  60. ),
  61. 'subpackage' => array(
  62. 'required' => false,
  63. 'allow_multiple' => false,
  64. 'order_text' => 'follows @package',
  65. ),
  66. 'author' => array(
  67. 'required' => true,
  68. 'allow_multiple' => false,
  69. 'order_text' => 'follows @subpackage (if used) or @package',
  70. ),
  71. 'copyright' => array(
  72. 'required' => true,
  73. 'allow_multiple' => false,
  74. 'order_text' => 'follows @author',
  75. ),
  76. 'license' => array(
  77. 'required' => true,
  78. 'allow_multiple' => false,
  79. 'order_text' => 'follows @copyright (if used) or @author',
  80. ),
  81. 'version' => array(
  82. 'required' => true,
  83. 'allow_multiple' => false,
  84. 'order_text' => 'follows @license',
  85. ),
  86. 'link' => array(
  87. 'required' => true,
  88. 'allow_multiple' => false,
  89. 'order_text' => 'follows @version',
  90. ),
  91. 'see' => array(
  92. 'required' => false,
  93. 'allow_multiple' => false,
  94. 'order_text' => 'follows @link',
  95. ),
  96. 'since' => array(
  97. 'required' => false,
  98. 'allow_multiple' => false,
  99. 'order_text' => 'follows @see (if used) or @link',
  100. ),
  101. 'deprecated' => array(
  102. 'required' => false,
  103. 'allow_multiple' => false,
  104. 'order_text' => 'follows @since (if used) or @see (if used) or @link',
  105. ),
  106. );
  107. protected $reqCodeRequire = 'REQ.PHP.4.3.4';
  108. protected $reqCodePHPVersion = 'REQ.PHP.4.3.3';
  109. protected $reqCodeForbidden = 'REQ.PHP.4.3.9';
  110. protected $reqCodeOnlyOne = 'REQ.PHP.4.3.8';
  111. protected $docBlock = 'file';
  112. /**
  113. * Processes this test, when one of its tokens is encountered.
  114. *
  115. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  116. * @param int $stackPtr The position of the current token
  117. * in the stack passed in $tokens.
  118. *
  119. * @return void
  120. */
  121. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  122. {
  123. $this->currentFile = $phpcsFile;
  124. // We are only interested if this is the first open tag.
  125. if ($stackPtr !== 0) {
  126. if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
  127. return;
  128. }
  129. }
  130. $tokens = $phpcsFile->getTokens();
  131. // Find the next non whitespace token.
  132. $commentStart
  133. = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  134. if ($tokens[$commentStart]['code'] !== T_COMMENT) {
  135. $phpcsFile->addError(
  136. $this->getReqPrefix('REQ.PHP.4.3.5') . 'Файл должен начинаться с однострочного комментария',
  137. $commentStart
  138. );
  139. } elseif (!preg_match('/vim: set ts=\d sw=\d sts=\d et:/', $tokens[$commentStart]['content'])) {
  140. $phpcsFile->addError(
  141. $this->getReqPrefix('REQ.PHP.4.3.5') . 'Файл должен начинаться с однострочного комментария с директивой для vim',
  142. $commentStart
  143. );
  144. }
  145. $commentStart = $phpcsFile->findNext(
  146. T_WHITESPACE,
  147. ($commentStart + 1),
  148. null,
  149. true
  150. );
  151. $errorToken = ($stackPtr + 1);
  152. if (isset($tokens[$errorToken]) === false) {
  153. $errorToken--;
  154. }
  155. if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
  156. // We are only interested if this is the first open tag.
  157. return;
  158. } else if ($tokens[$commentStart]['code'] === T_COMMENT) {
  159. $error = 'You must use "/**" style comments for a file comment';
  160. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.3.7') . $error, $errorToken);
  161. return;
  162. } else if ($commentStart === false
  163. || $tokens[$commentStart]['code'] !== T_DOC_COMMENT
  164. ) {
  165. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.3.1') . 'Missing file doc comment', $errorToken);
  166. return;
  167. } else {
  168. // Extract the header comment docblock.
  169. $commentEnd = $phpcsFile->findNext(
  170. T_DOC_COMMENT,
  171. ($commentStart + 1),
  172. null,
  173. true
  174. );
  175. $commentEnd--;
  176. // Check if there is only 1 doc comment between the
  177. // open tag and class token.
  178. $nextToken = array(
  179. T_ABSTRACT,
  180. T_CLASS,
  181. T_FUNCTION,
  182. T_DOC_COMMENT,
  183. );
  184. $commentNext = $phpcsFile->findNext($nextToken, ($commentEnd + 1));
  185. if ($commentNext !== false
  186. && $tokens[$commentNext]['code'] !== T_DOC_COMMENT
  187. ) {
  188. // Found a class token right after comment doc block.
  189. $newlineToken = $phpcsFile->findNext(
  190. T_WHITESPACE,
  191. ($commentEnd + 1),
  192. $commentNext,
  193. false,
  194. $phpcsFile->eolChar
  195. );
  196. if ($newlineToken !== false) {
  197. $newlineToken = $phpcsFile->findNext(
  198. T_WHITESPACE,
  199. ($newlineToken + 1),
  200. $commentNext,
  201. false,
  202. $phpcsFile->eolChar
  203. );
  204. if ($newlineToken === false) {
  205. // No blank line between the class token and the doc block.
  206. // The doc block is most likely a class comment.
  207. $error = 'Missing file doc comment';
  208. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.6') . $error, $errorToken);
  209. return;
  210. }
  211. }
  212. }//end if
  213. $comment = $phpcsFile->getTokensAsString(
  214. $commentStart,
  215. ($commentEnd - $commentStart + 1)
  216. );
  217. // Parse the header comment docblock.
  218. try {
  219. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
  220. $this->commentParser->parse();
  221. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  222. $line = ($e->getLineWithinComment() + $commentStart);
  223. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.1') . $e->getMessage(), $line);
  224. return;
  225. }
  226. $comment = $this->commentParser->getComment();
  227. if (is_null($comment) === true) {
  228. $error = 'File doc comment is empty';
  229. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.3.2') . $error, $commentStart);
  230. return;
  231. }
  232. // No extra newline before short description.
  233. $short = $comment->getShortComment();
  234. $newlineCount = 0;
  235. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  236. if ($short !== '' && $newlineSpan > 0) {
  237. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  238. $error = "Extra $line found before file comment short description";
  239. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.7') . $error, ($commentStart + 1));
  240. }
  241. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  242. // Exactly one blank line between short and long description.
  243. $long = $comment->getLongComment();
  244. if (empty($long) === false) {
  245. $between = $comment->getWhiteSpaceBetween();
  246. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  247. if ($newlineBetween !== 2) {
  248. $error = 'There must be exactly one blank line between descriptions in file comment';
  249. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.19') . $error, ($commentStart + $newlineCount + 1));
  250. }
  251. $newlineCount += $newlineBetween;
  252. }
  253. // Exactly one blank line before tags.
  254. $tags = $this->commentParser->getTagOrders();
  255. if (count($tags) > 1) {
  256. $newlineSpan = $comment->getNewlineAfter();
  257. if ($newlineSpan !== 2) {
  258. $error = 'There must be exactly one blank line before the tags in file comment';
  259. if ($long !== '') {
  260. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  261. }
  262. $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.18') . $error, ($commentStart + $newlineCount));
  263. $short = rtrim($short, $phpcsFile->eolChar.' ');
  264. }
  265. }
  266. // Check the PHP Version.
  267. $this->processPHPVersion($commentStart, $commentEnd, $long);
  268. // Check each tag.
  269. $this->processTags($commentStart, $commentEnd);
  270. }//end if
  271. }//end process()
  272. }//end class
  273. ?>