~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.5/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

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