/.dev/code-sniffs/XLite/Sniffs/CSS/Commenting/FileCommentSniff.php
PHP | 256 lines | 164 code | 27 blank | 65 comment | 19 complexity | ed5c6a9feae8fd7e3b86ca93a2120566 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?php
- /**
- * Parses and verifies the doc comments for files.
- *
- * PHP version 5
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @author Marc McIntyre <mmcintyre@squiz.net>
- * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
- * @version CVS: $Id: 9640f2e9a71a48788c35009e78dcd098ebc1f8c7 $
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
- /**
- * Parses and verifies the doc comments for files.
- *
- * Verifies that :
- * <ul>
- * <li>A doc comment exists.</li>
- * <li>There is a blank newline after the short description.</li>
- * <li>There is a blank newline between the long and short description.</li>
- * <li>There is a blank newline between the long description and tags.</li>
- * <li>A PHP version is specified.</li>
- * <li>Check the order of the tags.</li>
- * <li>Check the indentation of each tag.</li>
- * <li>Check required and optional tags and the format of their content.</li>
- * </ul>
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @author Marc McIntyre <mmcintyre@squiz.net>
- * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
- * @version Release: 1.2.0RC1
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
- class XLite_Sniffs_CSS_Commenting_FileCommentSniff extends XLite_TagsSniff
- {
- /**
- * A list of tokenizers this sniff supports.
- *
- * @var array
- */
- public $supportedTokenizers = array('CSS');
- /**
- * Tags in correct order and related info.
- *
- * @var array
- */
- protected $tags = array (
- 'author' => array(
- 'required' => true,
- 'allow_multiple' => false,
- 'order_text' => 'follows @subpackage (if used) or @package',
- ),
- 'copyright' => array(
- 'required' => true,
- 'allow_multiple' => false,
- 'order_text' => 'follows @author',
- ),
- 'license' => array(
- 'required' => true,
- 'allow_multiple' => false,
- 'order_text' => 'follows @copyright (if used) or @author',
- ),
- 'version' => array(
- 'required' => true,
- 'allow_multiple' => false,
- 'order_text' => 'follows @license',
- ),
- 'link' => array(
- 'required' => true,
- 'allow_multiple' => false,
- 'order_text' => 'follows @version',
- ),
- 'since' => array(
- 'required' => false,
- 'allow_multiple' => false,
- 'order_text' => 'follows @see (if used) or @link',
- ),
- );
- protected $reqCodesWrongFormat = array(
- 'author' => array(
- 'code' => 'REQ.CSS.4.1.13',
- 'function' => 'getAuthors',
- 'type' => 'array',
- ),
- 'copyright' => array(
- 'code' => 'REQ.CSS.4.1.14',
- 'function' => 'getCopyrights',
- 'type' => 'array',
- ),
- 'license' => array(
- 'code' => 'REQ.CSS.4.1.15',
- 'function' => 'getLicense',
- 'type' => 'single',
- ),
- 'version' => array(
- 'code' => 'REQ.CSS.4.1.16',
- 'function' => 'getVersion',
- 'type' => 'single',
- ),
- );
- protected $reqCodeRequire = 'REQ.CSS.4.1.5';
- protected $reqCodeIndent = 'REQ.CSS.4.1.8';
- /**
- * Processes this test, when one of its tokens is encountered.
- *
- * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
- * @param int $stackPtr The position of the current token
- * in the stack passed in $tokens.
- *
- * @return void
- */
- public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
- {
- $this->currentFile = $phpcsFile;
- if ($stackPtr !== 0) {
- if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
- return;
- }
- }
- $tokens = $phpcsFile->getTokens();
- // Find the next non whitespace token.
- $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr+1), null, true);
- if ($tokens[$commentStart]['code'] !== T_COMMENT) {
- $phpcsFile->addError(
- $this->getReqPrefix('REQ.CSS.4.1.1') . 'Файл должен начинаться с однострочного комментария',
- $commentStart
- );
- } elseif (!preg_match('/vim: set ts=\d sw=\d sts=\d et:/', $tokens[$commentStart]['content'])) {
- $phpcsFile->addError(
- $this->getReqPrefix('REQ.CSS.4.1.1') . 'Файл должен начинаться с однострочного комментария с директивой для vim',
- $commentStart
- );
- }
- $commentStart = $phpcsFile->findNext(
- T_WHITESPACE,
- ($commentStart + 1),
- null,
- true
- );
- $errorToken = ($stackPtr + 1);
- if (isset($tokens[$errorToken]) === false) {
- $errorToken--;
- }
- if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
- // We are only interested if this is the first open tag.
- return;
- } else if ($tokens[$commentStart]['code'] === T_COMMENT) {
- $error = 'You must use "/**" style comments for a file comment';
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.2') . $error, $errorToken);
- return;
- } else if ($commentStart === false
- || $tokens[$commentStart]['code'] !== T_DOC_COMMENT
- ) {
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.2') . 'Missing file doc comment', $errorToken);
- return;
- } else {
- // Extract the header comment docblock.
- $commentEnd = $phpcsFile->findNext(
- T_DOC_COMMENT,
- ($commentStart + 1),
- null,
- true
- );
- $commentEnd--;
- $comment = $phpcsFile->getTokensAsString(
- $commentStart,
- ($commentEnd - $commentStart + 1)
- );
- // Parse the header comment docblock.
- try {
- $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
- $this->commentParser->parse();
- } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
- $line = ($e->getLineWithinComment() + $commentStart);
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.1') . $e->getMessage(), $line);
- return;
- }
- $comment = $this->commentParser->getComment();
- if (is_null($comment) === true) {
- $error = 'File doc comment is empty';
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.3.2') . $error, $commentStart);
- return;
- }
- // No extra newline before short description.
- $short = $comment->getShortComment();
- $newlineCount = 0;
- $newlineSpan = strspn($short, $phpcsFile->eolChar);
- if ($short !== '' && $newlineSpan > 0) {
- $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
- $error = "Extra $line found before file comment short description";
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.6') . $error, ($commentStart + 1));
- }
- $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
- // Exactly one blank line between short and long description.
- $long = $comment->getLongComment();
- if (empty($long) === false) {
- $between = $comment->getWhiteSpaceBetween();
- $newlineBetween = substr_count($between, $phpcsFile->eolChar);
- if ($newlineBetween !== 2) {
- $error = 'There must be exactly one blank line between descriptions in file comment';
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.6') . $error, ($commentStart + $newlineCount + 1));
- }
- $newlineCount += $newlineBetween;
- }
- // Exactly one blank line before tags.
- $tags = $this->commentParser->getTagOrders();
- if (count($tags) > 1) {
- $newlineSpan = $comment->getNewlineAfter();
- if ($newlineSpan !== 2) {
- $error = 'There must be exactly one blank line before the tags in file comment';
- if ($long !== '') {
- $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
- }
- $phpcsFile->addError($this->getReqPrefix('REQ.CSS.4.1.6') . $error, ($commentStart + $newlineCount));
- $short = rtrim($short, $phpcsFile->eolChar.' ');
- }
- }
- // Check each tag.
- $this->processTags($commentStart, $commentEnd);
- }//end if
- }//end process()
- }//end class
- ?>