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: ForLoopShouldBeWhileLoopSniff.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 can be simplified to a while-loop.
19
 *
20
 * This rule is based on the PMD rule catalog. Detects for-loops that can be
21
 * simplified as a while-loop.
22
 *
23
 * <code>
24
 * class Foo
25
 * {
26
 *     public function bar($x)
27
 *     {
28
 *         for (;true;) true; // No Init or Update part, may as well be: while (true)
29
 *     }
30
 * }
31
 * </code>
32
 *
33
 * @category  PHP
34
 * @package   PHP_CodeSniffer
35
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
36
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
37
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
38
 * @version   Release: 1.2.0RC1
39
 * @link      http://pear.php.net/package/PHP_CodeSniffer
40
 */
41
class Generic_Sniffs_CodeAnalysis_ForLoopShouldBeWhileLoopSniff implements PHP_CodeSniffer_Sniff
42
{
43
 
44
 
45
    /**
46
     * Registers the tokens that this sniff wants to listen for.
47
     *
48
     * @return array(integer)
49
     */
50
    public function register()
51
    {
52
        return array(T_FOR);
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
62
     *                                        in the stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        $tokens = $phpcsFile->getTokens();
69
        $token  = $tokens[$stackPtr];
70
 
71
        // Skip invalid statement.
72
        if (isset($token['parenthesis_opener']) === false) {
73
            return;
74
        }
75
 
76
        $next = ++$token['parenthesis_opener'];
77
        $end  = --$token['parenthesis_closer'];
78
 
79
        $parts = array(0, 0, 0);
80
        $index = 0;
81
 
82
        for (; $next <= $end; ++$next) {
83
            $code = $tokens[$next]['code'];
84
            if ($code === T_SEMICOLON) {
85
                ++$index;
86
            } else if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
87
                ++$parts[$index];
88
            }
89
        }
90
 
91
        if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
92
            $error = 'This FOR loop can be simplified to a WHILE loop';
93
            $phpcsFile->addWarning($error, $stackPtr);
94
        }
95
 
96
    }//end process()
97
 
98
 
99
}//end class
100
 
101
?>