~ubuntu-branches/ubuntu/wily/php-codesniffer/wily

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.1.0/CodeSniffer/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2012-05-31 16:37:24 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120531163724-u6aiaubu8ks5dh5z
Tags: 1.3.4-0.1
* Non-maintainer upload.
* New upstream release (Closes: #599617, #634825).
* Swtiched debian/copyright to format 1.0 (rewrite was needed anyway, as the
upstream license changed).
* Switched package to pkg-php-tools and debhelper 8 sequencer.
* Now running unit tests at build time (so depends on phpunit (>= 3.6)).
* Section is now PHP.
* Added missing Build-Depends-Indep: php-pear.
* Added missing ${misc:Depends}.
* Added Vcs fields.
* Added homepage field.
* Reviewed short and long description.
* Added dependency on php-timer.
* Standards-Version: is now 3.9.3 (lots of changes, see above...).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,v 1.6 2008/02/28 22:40:50 squiz Exp $
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.1.0
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
 
                $operator = $tokens[$phpcsFile->findNext(array(T_PLUS, T_MINUS), ($stackPtr + 1), $statementEnd)]['content'];
196
 
            } else {
197
 
                $operator = substr($tokens[$stackPtr]['content'], 0, 1);
198
 
            }
199
 
 
200
 
            // If we are adding or subtracting negative value, the operator
201
 
            // needs to be reversed.
202
 
            if ($tokens[$stackPtr]['code'] !== T_EQUAL) {
203
 
                $negative = $phpcsFile->findPrevious(T_MINUS, ($nextNumber - 1), $stackPtr);
204
 
                if ($negative !== false) {
205
 
                    $operator = ($operator === '+') ? '-' : '+';
206
 
                }
207
 
            }
208
 
 
209
 
            $expected = $tokens[$assignedVar]['content'].$operator.$operator;
210
 
            $found    = $phpcsFile->getTokensAsString($assignedVar, ($statementEnd - $assignedVar + 1));
211
 
 
212
 
            if ($operator === '+') {
213
 
                $error = 'Increment';
214
 
            } else {
215
 
                $error = 'Decrement';
216
 
            }
217
 
 
218
 
            $error .= " operators should be used where possible; found \"$found\" but expected \"$expected\"";
219
 
            $phpcsFile->addError($error, $stackPtr);
220
 
        }
221
 
 
222
 
    }//end processAssignment()
223
 
 
224
 
 
225
 
}//end class
226
 
 
227
 
?>