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