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_DisallowSizeFunctionsInLoopsSniff.
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: DisallowSizeFunctionsInLoopsSniff.php 270281 2008-12-02 02:38:34Z squiz $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Squiz_Sniffs_PHP_DisallowSizeFunctionsInLoopsSniff.
18
 *
19
 * Bans the use of size-based functions in loop conditions.
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_DisallowSizeFunctionsInLoopsSniff implements PHP_CodeSniffer_Sniff
30
{
31
 
32
    /**
33
     * A list of tokenizers this sniff supports.
34
     *
35
     * @var array
36
     */
37
    public $supportedTokenizers = array(
38
                                   'PHP',
39
                                   'JS',
40
                                  );
41
 
42
    /**
43
     * An array of functions we don't want in the condition of loops.
44
     *
45
     * @return array
46
     */
47
    protected $forbiddenFunctions = array(
48
                                     'PHP' => array(
49
                                               'sizeof',
50
                                               'strlen',
51
                                               'count',
52
                                              ),
53
                                     'JS'  => array(
54
                                               'length',
55
                                              ),
56
                                    );
57
 
58
 
59
    /**
60
     * Returns an array of tokens this test wants to listen for.
61
     *
62
     * @return array
63
     */
64
    public function register()
65
    {
66
        return array(T_WHILE, T_FOR);
67
 
68
    }//end register()
69
 
70
 
71
    /**
72
     * Processes this test, when one of its tokens is encountered.
73
     *
74
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
75
     * @param int                  $stackPtr  The position of the current token
76
     *                                        in the stack passed in $tokens.
77
     *
78
     * @return void
79
     */
80
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
81
    {
82
        $tokens       = $phpcsFile->getTokens();
83
        $tokenizer    = $phpcsFile->tokenizerType;
84
        $openBracket  = $tokens[$stackPtr]['parenthesis_opener'];
85
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
86
 
87
        if ($tokens[$stackPtr]['code'] === T_FOR) {
88
            // We only want to check the condition in FOR loops.
89
            $start = $phpcsFile->findNext(T_SEMICOLON, ($openBracket + 1));
90
            $end   = $phpcsFile->findPrevious(T_SEMICOLON, ($closeBracket - 1));
91
        } else {
92
            $start = $openBracket;
93
            $end   = $closeBracket;
94
        }
95
 
96
        for ($i = ($start + 1); $i < $end; $i++) {
97
            if ($tokens[$i]['code'] === T_STRING && in_array($tokens[$i]['content'], $this->forbiddenFunctions[$tokenizer])) {
98
                $functionName = $tokens[$i]['content'];
99
                if ($tokenizer === 'JS') {
100
                    // Needs to be in the form object.function to be valid.
101
                    $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true);
102
                    if ($prev === false || $tokens[$prev]['code'] !== T_OBJECT_OPERATOR) {
103
                        continue;
104
                    }
105
 
106
                    $functionName = 'object.'.$functionName;
107
                } else {
108
                    $functionName .= '()';
109
                }
110
 
111
                $error = 'The use of '.$functionName.' inside a loop condition is not allowed. Assign the return value of '.$functionName.' to a variable and use the variable in the loop condition instead.';
112
                $phpcsFile->addError($error, $i);
113
            }//end if
114
        }//end for
115
 
116
    }//end process()
117
 
118
 
119
}//end class
120
 
121
?>