Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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