PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php

https://gitlab.com/trang1104/portable_project
PHP | 204 lines | 90 code | 30 blank | 84 comment | 28 complexity | 9a3612dd653e0ba95cf9d2cbd93eed9a MD5 | raw file
  1<?php
  2/**
  3 * Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff.
  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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 12 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 13 * @link      http://pear.php.net/package/PHP_CodeSniffer
 14 */
 15
 16/**
 17 * Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff.
 18 *
 19 * Checks that no whitespace proceeds the first content of the file, exists
 20 * after the last content of the file, resides after content on any line, or
 21 * are two empty lines in functions.
 22 *
 23 * @category  PHP
 24 * @package   PHP_CodeSniffer
 25 * @author    Greg Sherwood <gsherwood@squiz.net>
 26 * @author    Marc McIntyre <mmcintyre@squiz.net>
 27 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 28 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 29 * @version   Release: 1.3.3
 30 * @link      http://pear.php.net/package/PHP_CodeSniffer
 31 */
 32class Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffer_Sniff
 33{
 34
 35    /**
 36     * A list of tokenizers this sniff supports.
 37     *
 38     * @var array
 39     */
 40    public $supportedTokenizers = array(
 41                                   'PHP',
 42                                   'JS',
 43                                   'CSS',
 44                                  );
 45
 46    /**
 47     * Returns an array of tokens this test wants to listen for.
 48     *
 49     * @return array
 50     */
 51    public function register()
 52    {
 53        return array(
 54                T_OPEN_TAG,
 55                T_CLOSE_TAG,
 56                T_WHITESPACE,
 57                T_COMMENT,
 58               );
 59
 60    }//end register()
 61
 62
 63    /**
 64     * Processes this sniff, when one of its tokens is encountered.
 65     *
 66     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 67     * @param int                  $stackPtr  The position of the current token in the
 68     *                                        stack passed in $tokens.
 69     *
 70     * @return void
 71     */
 72    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 73    {
 74        $tokens = $phpcsFile->getTokens();
 75
 76        if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
 77
 78            /*
 79                Check for start of file whitespace.
 80            */
 81
 82            if ($phpcsFile->tokenizerType !== 'PHP') {
 83                // The first token is always the open tag inserted when tokenizsed
 84                // and the second token is always the first piece of content in
 85                // the file. If the second token is whitespace, there was
 86                // whitespace at the start of the file.
 87                if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
 88                    return;
 89                }
 90            } else {
 91                // If its the first token, then there is no space.
 92                if ($stackPtr === 0) {
 93                    return;
 94                }
 95
 96                for ($i = ($stackPtr - 1); $i >= 0; $i--) {
 97                    // If we find something that isn't inline html then there is something previous in the file.
 98                    if ($tokens[$i]['type'] !== 'T_INLINE_HTML') {
 99                        return;
100                    }
101
102                    // If we have ended up with inline html make sure it isn't just whitespace.
103                    $tokenContent = trim($tokens[$i]['content']);
104                    if ($tokenContent !== '') {
105                        return;
106                    }
107                }
108            }//end if
109
110            $phpcsFile->addError('Additional whitespace found at start of file', $stackPtr, 'StartFile');
111
112        } else if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
113
114            /*
115                Check for end of file whitespace.
116            */
117
118            if ($phpcsFile->tokenizerType === 'JS') {
119                // The last token is always the close tag inserted when tokenizsed
120                // and the second last token is always the last piece of content in
121                // the file. If the second last token is whitespace, there was
122                // whitespace at the end of the file.
123                if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
124                    return;
125                }
126            } else if ($phpcsFile->tokenizerType === 'CSS') {
127                // The last two tokens are always the close tag and whitespace
128                // inserted when tokenizsed and the third last token is always the
129                // last piece of content in the file. If the third last token is
130                // whitespace, there was whitespace at the end of the file.
131                if ($tokens[($stackPtr - 3)]['code'] !== T_WHITESPACE) {
132                    return;
133                }
134
135                // Adjust the pointer to give the correct line number for the error.
136                $stackPtr -= 2;
137            } else {
138                if (isset($tokens[($stackPtr + 1)]) === false) {
139                    // The close PHP token is the last in the file.
140                    return;
141                }
142
143                for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
144                    // If we find something that isn't inline html then there
145                    // is more to the file.
146                    if ($tokens[$i]['type'] !== 'T_INLINE_HTML') {
147                        return;
148                    }
149
150                    // If we have ended up with inline html make sure it
151                    // isn't just whitespace.
152                    $tokenContent = trim($tokens[$i]['content']);
153                    if (empty($tokenContent) === false) {
154                        return;
155                    }
156                }
157            }
158
159            $phpcsFile->addError('Additional whitespace found at end of file', $stackPtr, 'EndFile');
160
161        } else {
162
163            /*
164                Check for end of line whitespace.
165            */
166
167            if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) {
168                return;
169            }
170
171            $tokenContent = rtrim($tokens[$stackPtr]['content'], $phpcsFile->eolChar);
172            if (empty($tokenContent) === false) {
173                if (preg_match('|^.*\s+$|', $tokenContent) !== 0) {
174                    $phpcsFile->addError('Whitespace found at end of line', $stackPtr, 'EndLine');
175                }
176            }
177
178            /*
179                Check for multiple blanks lines in a function.
180            */
181
182            if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) {
183                if ($tokens[($stackPtr - 1)]['line'] < $tokens[$stackPtr]['line'] && $tokens[($stackPtr - 2)]['line'] === $tokens[($stackPtr - 1)]['line']) {
184                    // This is an empty line and the line before this one is not
185                    //  empty, so this could be the start of a multiple empty
186                    // line block.
187                    $next  = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);
188                    $lines = $tokens[$next]['line'] - $tokens[$stackPtr]['line'];
189                    if ($lines > 1) {
190                        $error = 'Functions must not contain multiple empty lines in a row; found %s empty lines';
191                        $data  = array($lines);
192                        $phpcsFile->addError($error, $stackPtr, 'EmptyLines', $data);
193                    }
194                }
195            }
196
197        }//end if
198
199    }//end process()
200
201
202}//end class
203
204?>