~ubuntu-branches/ubuntu/saucy/php-codesniffer/saucy

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/Squiz/Sniffs/PHP/DisallowComparisonAssignmentSniff.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_PHP_DisallowComparisonAssignmentSniff.
 
4
 *
 
5
 * PHP version 5
 
6
 *
 
7
 * @category  PHP
 
8
 * @package   PHP_CodeSniffer
 
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
10
 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
13
 */
 
14
 
 
15
/**
 
16
 * Squiz_Sniffs_PHP_DisallowComparisonAssignmentSniff.
 
17
 *
 
18
 * Ensures that the value of a comparison is not assigned to a variable.
 
19
 *
 
20
 * @category  PHP
 
21
 * @package   PHP_CodeSniffer
 
22
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
23
 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 
24
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
25
 * @version   Release: 1.3.4
 
26
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
27
 */
 
28
class Squiz_Sniffs_PHP_DisallowComparisonAssignmentSniff implements PHP_CodeSniffer_Sniff
 
29
{
 
30
 
 
31
 
 
32
    /**
 
33
     * Returns an array of tokens this test wants to listen for.
 
34
     *
 
35
     * @return array
 
36
     */
 
37
    public function register()
 
38
    {
 
39
        return array(T_EQUAL);
 
40
 
 
41
    }//end register()
 
42
 
 
43
 
 
44
    /**
 
45
     * Processes this test, when one of its tokens is encountered.
 
46
     *
 
47
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
48
     * @param int                  $stackPtr  The position of the current token
 
49
     *                                        in the stack passed in $tokens.
 
50
     *
 
51
     * @return void
 
52
     */
 
53
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
54
    {
 
55
        $tokens = $phpcsFile->getTokens();
 
56
 
 
57
        // Ignore default value assignments in function definitions.
 
58
        $function = $phpcsFile->findPrevious(T_FUNCTION, ($stackPtr - 1));
 
59
        if ($function !== false) {
 
60
            $opener = $tokens[$function]['parenthesis_opener'];
 
61
            $closer = $tokens[$function]['parenthesis_closer'];
 
62
            if ($opener < $stackPtr && $closer > $stackPtr) {
 
63
                return;
 
64
            }
 
65
        }
 
66
 
 
67
        // Ignore values in array definitions.
 
68
        $array = $phpcsFile->findNext(
 
69
            T_ARRAY,
 
70
            ($stackPtr + 1),
 
71
            null,
 
72
            false,
 
73
            null,
 
74
            true
 
75
        );
 
76
 
 
77
        if ($array !== false) {
 
78
            return;
 
79
        }
 
80
 
 
81
        // Ignore function calls.
 
82
        $ignore = array(
 
83
                   T_STRING,
 
84
                   T_WHITESPACE,
 
85
                   T_OBJECT_OPERATOR,
 
86
                  );
 
87
 
 
88
        $next = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true);
 
89
        if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS
 
90
            && $tokens[($next - 1)]['code'] === T_STRING
 
91
        ) {
 
92
            // Code will look like: $var = myFunction(
 
93
            // and will be ignored.
 
94
            return;
 
95
        }
 
96
 
 
97
        $endStatement = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
 
98
        for ($i = ($stackPtr + 1); $i < $endStatement; $i++) {
 
99
            if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens) === true) {
 
100
                $error = 'The value of a comparison must not be assigned to a variable';
 
101
                $phpcsFile->addError($error, $stackPtr, 'AssignedComparison');
 
102
                break;
 
103
            }
 
104
 
 
105
            if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$booleanOperators) === true
 
106
                || $tokens[$i]['code'] === T_BOOLEAN_NOT
 
107
            ) {
 
108
                $error = 'The value of a boolean operation must not be assigned to a variable';
 
109
                $phpcsFile->addError($error, $stackPtr, 'AssignedBool');
 
110
                break;
 
111
            }
 
112
        }
 
113
 
 
114
    }//end process()
 
115
 
 
116
 
 
117
}//end class
 
118
 
 
119
?>