• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 7400492251

03 Jan 2024 05:32PM UTC coverage: 67.659% (+0.003%) from 67.656%
7400492251

Pull #226

github

web-flow
Merge cf81ed27e into da51554b7
Pull Request #226: Generic/ForLoopShouldBeWhileLoop: improve sniff code coverage

20025 of 29597 relevant lines covered (67.66%)

4.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php
1
<?php
2
/**
3
 * Detects for-loops that can be simplified to a while-loop.
4
 *
5
 * This rule is based on the PMD rule catalogue. Detects for-loops that can be
6
 * simplified as a while-loop.
7
 *
8
 * <code>
9
 * class Foo
10
 * {
11
 *     public function bar($x)
12
 *     {
13
 *         for (;true;) true; // No Init or Update part, may as well be: while (true)
14
 *     }
15
 * }
16
 * </code>
17
 *
18
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
19
 * @copyright 2007-2014 Manuel Pichler. All rights reserved.
20
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
21
 */
22

23
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
24

25
use PHP_CodeSniffer\Files\File;
26
use PHP_CodeSniffer\Sniffs\Sniff;
27
use PHP_CodeSniffer\Util\Tokens;
28

29
class ForLoopShouldBeWhileLoopSniff implements Sniff
30
{
31

32

33
    /**
34
     * Registers the tokens that this sniff wants to listen for.
35
     *
36
     * @return array<int|string>
37
     */
38
    public function register()
3✔
39
    {
40
        return [T_FOR];
3✔
41

42
    }//end register()
43

44

45
    /**
46
     * Processes this test, when one of its tokens is encountered.
47
     *
48
     * @param \PHP_CodeSniffer\Files\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(File $phpcsFile, $stackPtr)
3✔
55
    {
56
        $tokens = $phpcsFile->getTokens();
3✔
57
        $token  = $tokens[$stackPtr];
3✔
58

59
        // Skip invalid statement.
60
        if (isset($token['parenthesis_opener']) === false) {
3✔
61
            return;
3✔
62
        }
63

64
        $next = ++$token['parenthesis_opener'];
3✔
65
        $end  = --$token['parenthesis_closer'];
3✔
66

67
        $parts = [
1✔
68
            0,
3✔
69
            0,
2✔
70
            0,
2✔
71
        ];
2✔
72
        $index = 0;
3✔
73

74
        for (; $next <= $end; ++$next) {
3✔
75
            $code = $tokens[$next]['code'];
3✔
76
            if ($code === T_SEMICOLON) {
3✔
77
                ++$index;
3✔
78
            } else if (isset(Tokens::$emptyTokens[$code]) === false) {
3✔
79
                ++$parts[$index];
3✔
80
            }
1✔
81
        }
1✔
82

83
        if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
3✔
84
            $error = 'This FOR loop can be simplified to a WHILE loop';
3✔
85
            $phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
3✔
86
        }
1✔
87

88
    }//end process()
2✔
89

90

91
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc