~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.1.0/CodeSniffer/Standards/Squiz/Sniffs/Formatting/OperatorBracketSniff.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_Formatting_OperationBracketSniff.
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: OperatorBracketSniff.php,v 1.10 2008/03/17 03:52:43 squiz Exp $
14
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 
 */
16
 
 
17
 
/**
18
 
 * Squiz_Sniffs_Formatting_OperationBracketSniff.
19
 
 *
20
 
 * Tests that all arithmetic operations are bracketed.
21
 
 *
22
 
 * @category  PHP
23
 
 * @package   PHP_CodeSniffer
24
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
25
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
26
 
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
 
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
 
 * @version   Release: 1.1.0
29
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 
 */
31
 
class Squiz_Sniffs_Formatting_OperatorBracketSniff implements PHP_CodeSniffer_Sniff
32
 
{
33
 
 
34
 
    /**
35
 
     * A list of tokenizers this sniff supports.
36
 
     *
37
 
     * @var array
38
 
     */
39
 
    public $supportedTokenizers = array(
40
 
                                   'PHP',
41
 
                                   'JS',
42
 
                                  );
43
 
 
44
 
 
45
 
    /**
46
 
     * Returns an array of tokens this test wants to listen for.
47
 
     *
48
 
     * @return array
49
 
     */
50
 
    public function register()
51
 
    {
52
 
        return PHP_CodeSniffer_Tokens::$operators;
53
 
 
54
 
    }//end register()
55
 
 
56
 
 
57
 
    /**
58
 
     * Processes this test, when one of its tokens is encountered.
59
 
     *
60
 
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
 
     * @param int                  $stackPtr  The position of the current token in the
62
 
     *                                        stack passed in $tokens.
63
 
     *
64
 
     * @return void
65
 
     */
66
 
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
 
    {
68
 
        $tokens = $phpcsFile->getTokens();
69
 
 
70
 
        // If the & is a reference, then we don't want to check for brackets.
71
 
        if ($tokens[$stackPtr]['code'] === T_BITWISE_AND && $phpcsFile->isReference($stackPtr) === true) {
72
 
            return;
73
 
        }
74
 
 
75
 
        // There is one instance where brackets aren't needed, which involves
76
 
        // the minus sign being used to assign a negative number to a variable.
77
 
        if ($tokens[$stackPtr]['code'] === T_MINUS) {
78
 
            // Check to see if we are trying to return -n.
79
 
            $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
80
 
            if ($tokens[$prev]['code'] === T_RETURN) {
81
 
                return;
82
 
            }
83
 
 
84
 
            $number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
85
 
            if ($tokens[$number]['code'] === T_LNUMBER || $tokens[$number]['code'] === T_DNUMBER) {
86
 
                $previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
87
 
                if ($previous !== false) {
88
 
                    $isAssignment = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens);
89
 
                    $isEquality   = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$equalityTokens);
90
 
                    $isComparison = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens);
91
 
                    if ($isAssignment === true || $isEquality === true || $isComparison === true) {
92
 
                        // This is a negative assignment or comparion.
93
 
                        // We need to check that the minus and the number are
94
 
                        // adjacent.
95
 
                        if (($number - $stackPtr) !== 1) {
96
 
                            $error = 'No space allowed between minus sign and number';
97
 
                            $phpcsFile->addError($error, $stackPtr);
98
 
                        }
99
 
 
100
 
                        return;
101
 
                    }
102
 
                }
103
 
            }
104
 
        }//end if
105
 
 
106
 
        $lastBracket = false;
107
 
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
108
 
            $parenthesis = array_reverse($tokens[$stackPtr]['nested_parenthesis'], true);
109
 
            foreach ($parenthesis as $bracket => $endBracket) {
110
 
                $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($bracket - 1), null, true);
111
 
                $prevCode  = $tokens[$prevToken]['code'];
112
 
 
113
 
                if ($prevCode === T_ISSET) {
114
 
                    // This operation is inside an isset() call, but has
115
 
                    // no bracket of it's own.
116
 
                    break;
117
 
                }
118
 
 
119
 
                if ($prevCode === T_STRING) {
120
 
                    // We allow very simple operations to not be bracketed:
121
 
                    // ceil($one / $two);
122
 
                    $allowed = array(
123
 
                                T_VARIABLE,
124
 
                                T_LNUMBER,
125
 
                                T_DNUMBER,
126
 
                                T_STRING,
127
 
                                T_WHITESPACE,
128
 
                               );
129
 
 
130
 
                    for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
131
 
                        if (in_array($tokens[$prev]['code'], $allowed) === true) {
132
 
                            continue;
133
 
                        }
134
 
 
135
 
                        if ($tokens[$prev]['code'] === T_CLOSE_PARENTHESIS) {
136
 
                            $prev = $tokens[$prev]['parenthesis_opener'];
137
 
                        } else {
138
 
                            break;
139
 
                        }
140
 
                    }
141
 
 
142
 
                    if ($prev !== $bracket) {
143
 
                        break;
144
 
                    }
145
 
 
146
 
                    for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
147
 
                        if (in_array($tokens[$next]['code'], $allowed) === true) {
148
 
                            continue;
149
 
                        }
150
 
 
151
 
                        if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
152
 
                            $next = $tokens[$next]['parenthesis_closer'];
153
 
                        } else {
154
 
                            break;
155
 
                        }
156
 
                    }
157
 
 
158
 
                    if ($next !== $endBracket) {
159
 
                        break;
160
 
                    }
161
 
                }//end if
162
 
 
163
 
                if (in_array($prevCode, PHP_CodeSniffer_Tokens::$scopeOpeners) === true) {
164
 
                    // This operation is inside an a control structure like FOREACH
165
 
                    // or IF, but has no bracket of it's own.
166
 
                    break;
167
 
                }
168
 
 
169
 
                if ($prevCode === T_OPEN_PARENTHESIS) {
170
 
                    // These are two open parenthesis in a row. If the current
171
 
                    // one doesn't enclose the operator, go to the previous one.
172
 
                    if ($endBracket < $stackPtr) {
173
 
                        continue;
174
 
                    }
175
 
                }
176
 
 
177
 
                $lastBracket = $bracket;
178
 
                break;
179
 
            }//end foreach
180
 
        }//end if
181
 
 
182
 
        if ($lastBracket === false) {
183
 
            // It is not in a bracketed statement at all.
184
 
            $previousToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true, null, true);
185
 
            if ($previousToken !== false) {
186
 
                // A list of tokens that indicate that the token is not
187
 
                // part of an arithmetic operation.
188
 
                $invalidTokens = array(
189
 
                                  T_COMMA,
190
 
                                  T_OPEN_PARENTHESIS,
191
 
                                  T_OPEN_SQUARE_BRACKET,
192
 
                                 );
193
 
 
194
 
                if (in_array($tokens[$previousToken]['code'], $invalidTokens) === false) {
195
 
                    $error = 'Arithmetic operation must be bracketed';
196
 
                    $phpcsFile->addError($error, $stackPtr);
197
 
                }
198
 
 
199
 
                return;
200
 
            }
201
 
        } else if ($tokens[$lastBracket]['parenthesis_closer'] < $stackPtr) {
202
 
            // There are a set of brackets in front of it that don't include it.
203
 
            $error = 'Arithmetic operation must be bracketed';
204
 
            $phpcsFile->addError($error, $stackPtr);
205
 
            return;
206
 
        } else {
207
 
            // We are enclosed in a set of bracket, so the last thing to
208
 
            // check is that we are not also enclosed in square brackets
209
 
            // like this: ($array[$index + 1]), which is invalid.
210
 
            $brackets = array(
211
 
                         T_OPEN_SQUARE_BRACKET,
212
 
                         T_CLOSE_SQUARE_BRACKET,
213
 
                        );
214
 
 
215
 
            $squareBracket = $phpcsFile->findPrevious($brackets, ($stackPtr - 1), $lastBracket);
216
 
            if ($squareBracket !== false && $tokens[$squareBracket]['code'] === T_OPEN_SQUARE_BRACKET) {
217
 
                $closeSquareBracket = $phpcsFile->findNext($brackets, ($stackPtr + 1));
218
 
                if ($closeSquareBracket !== false && $tokens[$closeSquareBracket]['code'] === T_CLOSE_SQUARE_BRACKET) {
219
 
                    $error = 'Arithmetic operation must be bracketed';
220
 
                    $phpcsFile->addError($error, $stackPtr);
221
 
                }
222
 
            }
223
 
 
224
 
            return;
225
 
        }//end if
226
 
 
227
 
        $lastAssignment = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$assignmentTokens, $stackPtr, null, false, null, true);
228
 
        if ($lastAssignment !== false && $lastAssignment > $lastBracket) {
229
 
            $error = 'Arithmetic operation must be bracketed';
230
 
            $phpcsFile->addError($error, $stackPtr);
231
 
        }
232
 
 
233
 
    }//end process()
234
 
 
235
 
 
236
 
}//end class
237
 
 
238
 
?>