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