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

PHPCSStandards / PHP_CodeSniffer / 17174734458

23 Aug 2025 11:06AM UTC coverage: 76.88% (-2.1%) from 78.934%
17174734458

push

github

jrfnl
TEMP/TESTING PHPUnit 6331

19187 of 24957 relevant lines covered (76.88%)

60.25 hits per line

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

96.15
/src/Standards/Generic/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php
1
<?php
2
/**
3
 * Detects unconditional if- and elseif-statements.
4
 *
5
 * This rule is based on the PMD rule catalogue. The Unconditional If Statement
6
 * sniff detects statement conditions that are only set to one of the constant
7
 * values <b>true</b> or <b>false</b>
8
 *
9
 * <code>
10
 * class Foo
11
 * {
12
 *     public function close()
13
 *     {
14
 *         if (true)
15
 *         {
16
 *             // ...
17
 *         }
18
 *     }
19
 * }
20
 * </code>
21
 *
22
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
23
 * @copyright 2007-2014 Manuel Pichler. All rights reserved.
24
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
 */
26

27
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
28

29
use PHP_CodeSniffer\Files\File;
30
use PHP_CodeSniffer\Sniffs\Sniff;
31
use PHP_CodeSniffer\Util\Tokens;
32

33
class UnconditionalIfStatementSniff implements Sniff
34
{
35

36

37
    /**
38
     * Registers the tokens that this sniff wants to listen for.
39
     *
40
     * @return array<int|string>
41
     */
42
    public function register()
2✔
43
    {
44
        return [
2✔
45
            T_IF,
2✔
46
            T_ELSEIF,
2✔
47
        ];
2✔
48

49
    }//end register()
50

51

52
    /**
53
     * Processes this test, when one of its tokens is encountered.
54
     *
55
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
56
     * @param int                         $stackPtr  The position of the current token
57
     *                                               in the stack passed in $tokens.
58
     *
59
     * @return void
60
     */
61
    public function process(File $phpcsFile, $stackPtr)
2✔
62
    {
63
        $tokens = $phpcsFile->getTokens();
2✔
64
        $token  = $tokens[$stackPtr];
2✔
65

66
        // Skip if statement without body.
67
        if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
2✔
68
            return;
2✔
69
        }
70

71
        $next = ++$token['parenthesis_opener'];
2✔
72
        $end  = --$token['parenthesis_closer'];
2✔
73

74
        $goodCondition = false;
2✔
75
        for (; $next <= $end; ++$next) {
2✔
76
            $code = $tokens[$next]['code'];
2✔
77

78
            if (isset(Tokens::EMPTY_TOKENS[$code]) === true) {
2✔
79
                continue;
2✔
80
            }
81

82
            if ($code === T_NAME_FULLY_QUALIFIED) {
2✔
83
                $compareReadyKeyword = strtolower($tokens[$next]['content']);
2✔
84
                if ($compareReadyKeyword !== '\true' && $compareReadyKeyword !== '\false') {
2✔
85
                    $goodCondition = true;
×
86
                }
87
            } else if ($code !== T_TRUE && $code !== T_FALSE) {
2✔
88
                $goodCondition = true;
2✔
89
            }
90
        }
91

92
        if ($goodCondition === false) {
2✔
93
            $error = 'Avoid IF statements that are always true or false';
2✔
94
            $phpcsFile->addWarning($error, $stackPtr, 'Found');
2✔
95
        }
96

97
    }//end process()
98

99

100
}//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

© 2026 Coveralls, Inc