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_Operators_IncrementDecrementUsageSniff.
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: IncrementDecrementUsageSniff.php 279955 2009-05-05 06:28:10Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Squiz_Sniffs_Operators_IncrementDecrementUsageSniff.
19
 *
20
 * Tests that the ++ operators are used when possible and not
21
 * used when it makes the code confusing.
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 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.2.1
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
31
 */
32
class Squiz_Sniffs_Operators_IncrementDecrementUsageSniff implements PHP_CodeSniffer_Sniff
33
{
34
 
35
 
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(
44
                T_EQUAL,
45
                T_PLUS_EQUAL,
46
                T_MINUS_EQUAL,
47
                T_INC,
48
                T_DEC,
49
               );
50
 
51
    }//end register()
52
 
53
 
54
    /**
55
     * Processes this test, when one of its tokens is encountered.
56
     *
57
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
58
     * @param int                  $stackPtr  The position of the current token
59
     *                                        in the stack passed in $tokens.
60
     *
61
     * @return void
62
     */
63
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
64
    {
65
        $tokens = $phpcsFile->getTokens();
66
 
67
        if ($tokens[$stackPtr]['code'] === T_INC || $tokens[$stackPtr]['code'] === T_DEC) {
68
            $this->processIncDec($phpcsFile, $stackPtr);
69
        } else {
70
            $this->processAssignment($phpcsFile, $stackPtr);
71
        }
72
 
73
    }//end process()
74
 
75
 
76
    /**
77
     * Checks to ensure increment and decrement operators are not confusing.
78
     *
79
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
80
     * @param int                  $stackPtr  The position of the current token
81
     *                                        in the stack passed in $tokens.
82
     *
83
     * @return void
84
     */
85
    protected function processIncDec(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
86
    {
87
        $tokens = $phpcsFile->getTokens();
88
 
89
        // Work out where the variable is so we know where to
90
        // start looking for other operators.
91
        if ($tokens[($stackPtr - 1)]['code'] === T_VARIABLE) {
92
            $start = ($stackPtr + 1);
93
        } else {
94
            $start = ($stackPtr + 2);
95
        }
96
 
97
        $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $start, null, true);
98
        if ($next === false) {
99
            return;
100
        }
101
 
102
        if (in_array($tokens[$next]['code'], PHP_CodeSniffer_Tokens::$arithmeticTokens) === true) {
103
            $error = 'Increment and decrement operators cannot be used in an arithmetic operation';
104
            $phpcsFile->addError($error, $stackPtr);
105
            return;
106
        }
107
 
108
        $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($start - 3), null, true);
109
        if ($prev === false) {
110
            return;
111
        }
112
 
113
        // Check if this is in a string concat.
114
        if ($tokens[$next]['code'] === T_STRING_CONCAT || $tokens[$prev]['code'] === T_STRING_CONCAT) {
115
            $error = 'Increment and decrement operators must be bracketed when used in string concatenation';
116
            $phpcsFile->addError($error, $stackPtr);
117
        }
118
 
119
    }//end processIncDec()
120
 
121
 
122
    /**
123
     * Checks to ensure increment and decrement operators are used.
124
     *
125
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
126
     * @param int                  $stackPtr  The position of the current token
127
     *                                        in the stack passed in $tokens.
128
     *
129
     * @return void
130
     */
131
    protected function processAssignment(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
132
    {
133
        $tokens = $phpcsFile->getTokens();
134
 
135
        $assignedVar = $phpcsFile->findPrevious(array(T_VARIABLE), ($stackPtr - 1), null, false);
136
        // Not an assignment, return.
137
        if ($assignedVar === false) {
138
            return;
139
        }
140
 
141
        $statementEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_CLOSE_PARENTHESIS, T_CLOSE_SQUARE_BRACKET, T_CLOSE_CURLY_BRACKET), $stackPtr);
142
 
143
        // If there is anything other than variables, numbers, spaces or operators we need to return.
144
        $noiseTokens = $phpcsFile->findNext(array(T_LNUMBER, T_VARIABLE, T_WHITESPACE, T_PLUS, T_MINUS, T_OPEN_PARENTHESIS), ($stackPtr + 1), $statementEnd, true);
145
 
146
        if ($noiseTokens !== false) {
147
            return;
148
        }
149
 
150
        // If we are already using += or -=, we need to ignore
151
        // the statement if a variable is being used.
152
        if ($tokens[$stackPtr]['code'] !== T_EQUAL) {
153
            $nextVar = $phpcsFile->findNext(T_VARIABLE, ($stackPtr + 1), $statementEnd);
154
            if ($nextVar !== false) {
155
                return;
156
            }
157
        }
158
 
159
        if ($tokens[$stackPtr]['code'] === T_EQUAL) {
160
            $nextVar          = ($stackPtr + 1);
161
            $previousVariable = ($stackPtr + 1);
162
            $variableCount    = 0;
163
            while (($nextVar = $phpcsFile->findNext(T_VARIABLE, ($nextVar + 1), $statementEnd)) !== false) {
164
                $previousVariable = $nextVar;
165
                $variableCount++;
166
            }
167
 
168
            if ($variableCount !== 1) {
169
                return;
170
            }
171
 
172
            $nextVar = $previousVariable;
173
            if ($tokens[$nextVar]['content'] !== $tokens[$assignedVar]['content']) {
174
                return;
175
            }
176
        }
177
 
178
        // We have only one variable, and it's the same as what is being assigned,
179
        // so we need to check what is being added or subtracted.
180
        $nextNumber     = ($stackPtr + 1);
181
        $previousNumber = ($stackPtr + 1);
182
        $numberCount    = 0;
183
        while (($nextNumber = $phpcsFile->findNext(array(T_LNUMBER), ($nextNumber + 1), $statementEnd, false)) !== false) {
184
            $previousNumber = $nextNumber;
185
            $numberCount++;
186
        }
187
 
188
        if ($numberCount !== 1) {
189
            return;
190
        }
191
 
192
        $nextNumber = $previousNumber;
193
        if ($tokens[$nextNumber]['content'] === '1') {
194
            if ($tokens[$stackPtr]['code'] === T_EQUAL) {
195
                $opToken = $phpcsFile->findNext(array(T_PLUS, T_MINUS), ($nextVar + 1), $statementEnd);
196
                if ($opToken === false) {
197
                    // Operator was before the varaible, like:
198
                    // $var = 1 + $var;
199
                    // So we ignore it.
200
                    return;
201
                }
202
 
203
                $operator = $tokens[$opToken]['content'];
204
            } else {
205
                $operator = substr($tokens[$stackPtr]['content'], 0, 1);
206
            }
207
 
208
            // If we are adding or subtracting negative value, the operator
209
            // needs to be reversed.
210
            if ($tokens[$stackPtr]['code'] !== T_EQUAL) {
211
                $negative = $phpcsFile->findPrevious(T_MINUS, ($nextNumber - 1), $stackPtr);
212
                if ($negative !== false) {
213
                    $operator = ($operator === '+') ? '-' : '+';
214
                }
215
            }
216
 
217
            $expected = $tokens[$assignedVar]['content'].$operator.$operator;
218
            $found    = $phpcsFile->getTokensAsString($assignedVar, ($statementEnd - $assignedVar + 1));
219
 
220
            if ($operator === '+') {
221
                $error = 'Increment';
222
            } else {
223
                $error = 'Decrement';
224
            }
225
 
226
            $error .= " operators should be used where possible; found \"$found\" but expected \"$expected\"";
227
            $phpcsFile->addError($error, $stackPtr);
228
        }//end if
229
 
230
    }//end processAssignment()
231
 
232
 
233
}//end class
234
 
235
?>