Subversion Repositories Applications.framework

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
11
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
12
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
34 aurelien 13
 * @version   CVS: $Id: ForLoopWithTestFunctionCallSniff.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Detects for-loops that use a function call in the test expression.
19
 *
20
 * This rule is based on the PMD rule catalog. Detects for-loops that use a
21
 * function call in the test expression.
22
 *
23
 * <code>
24
 * class Foo
25
 * {
26
 *     public function bar($x)
27
 *     {
28
 *         $a = array(1, 2, 3, 4);
29
 *         for ($i = 0; $i < count($a); $i++) {
30
 *              $a[$i] *= $i;
31
 *         }
32
 *     }
33
 * }
34
 * </code>
35
 *
36
 * @category  PHP
37
 * @package   PHP_CodeSniffer
38
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
39
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
40
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
41
 * @version   Release: 1.2.0RC1
42
 * @link      http://pear.php.net/package/PHP_CodeSniffer
43
 */
44
class Generic_Sniffs_CodeAnalysis_ForLoopWithTestFunctionCallSniff implements PHP_CodeSniffer_Sniff
45
{
46
 
47
 
48
    /**
49
     * Registers the tokens that this sniff wants to listen for.
50
     *
51
     * @return array(integer)
52
     */
53
    public function register()
54
    {
55
        return array(T_FOR);
56
 
57
    }//end register()
58
 
59
 
60
    /**
61
     * Processes this test, when one of its tokens is encountered.
62
     *
63
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
64
     * @param int                  $stackPtr  The position of the current token
65
     *                                        in the stack passed in $tokens.
66
     *
67
     * @return void
68
     */
69
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
70
    {
71
        $tokens = $phpcsFile->getTokens();
72
        $token  = $tokens[$stackPtr];
73
 
74
        // Skip invalid statement.
75
        if (isset($token['parenthesis_opener']) === false) {
76
            return;
77
        }
78
 
79
        $next = ++$token['parenthesis_opener'];
80
        $end  = --$token['parenthesis_closer'];
81
 
82
        $position = 0;
83
 
84
        for (; $next <= $end; ++$next) {
85
            $code = $tokens[$next]['code'];
86
            if ($code === T_SEMICOLON) {
87
                ++$position;
88
            }
89
 
90
            if ($position < 1) {
91
                continue;
92
            } else if ($position > 1) {
93
                break;
94
            } else if ($code !== T_VARIABLE && $code !== T_STRING) {
95
                continue;
96
            }
97
 
98
            // Find next non empty token, if it is a open curly brace we have a
99
            // function call.
100
            $index = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
101
 
102
            if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) {
103
                $error = 'Avoid function calls in a FOR loop test part';
104
                $phpcsFile->addWarning($error, $stackPtr);
105
                break;
106
            }
107
        }//end for
108
 
109
    }//end process()
110
 
111
 
112
}//end class
113
 
114
?>