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

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

98.45
/src/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php
1
<?php
2
/**
3
 * Ensure multi-line IF conditions are defined correctly.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\ControlStructures;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class MultiLineConditionSniff implements Sniff
17
{
18

19
    /**
20
     * The number of spaces code should be indented.
21
     *
22
     * @var integer
23
     */
24
    public $indent = 4;
25

26

27
    /**
28
     * Returns an array of tokens this test wants to listen for.
29
     *
30
     * @return array<int|string>
31
     */
32
    public function register()
3✔
33
    {
34
        return [
2✔
35
            T_IF,
3✔
36
            T_ELSEIF,
3✔
37
        ];
2✔
38
    }
39

40

41
    /**
42
     * Processes this test, when one of its tokens is encountered.
43
     *
44
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
45
     * @param int                         $stackPtr  The position of the current token
46
     *                                               in the stack passed in $tokens.
47
     *
48
     * @return void
49
     */
50
    public function process(File $phpcsFile, int $stackPtr)
3✔
51
    {
52
        $tokens = $phpcsFile->getTokens();
3✔
53

54
        if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
3✔
UNCOV
55
            return;
×
56
        }
57

58
        $openBracket    = $tokens[$stackPtr]['parenthesis_opener'];
3✔
59
        $closeBracket   = $tokens[$stackPtr]['parenthesis_closer'];
3✔
60
        $spaceAfterOpen = 0;
3✔
61
        if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
3✔
62
            if (strpos($tokens[($openBracket + 1)]['content'], $phpcsFile->eolChar) !== false) {
3✔
63
                $spaceAfterOpen = 'newline';
3✔
64
            } else {
65
                $spaceAfterOpen = $tokens[($openBracket + 1)]['length'];
3✔
66
            }
67
        }
68

69
        if ($spaceAfterOpen !== 0) {
3✔
70
            $error = 'First condition of a multi-line IF statement must directly follow the opening parenthesis';
3✔
71
            $fix   = $phpcsFile->addFixableError($error, ($openBracket + 1), 'SpacingAfterOpenBrace');
3✔
72
            if ($fix === true) {
3✔
73
                if ($spaceAfterOpen === 'newline') {
3✔
74
                    $phpcsFile->fixer->replaceToken(($openBracket + 1), '');
3✔
75
                } else {
76
                    $phpcsFile->fixer->replaceToken(($openBracket + 1), '');
3✔
77
                }
78
            }
79
        }
80

81
        // We need to work out how far indented the if statement
82
        // itself is, so we can work out how far to indent conditions.
83
        $statementIndent = 0;
3✔
84
        for ($i = ($stackPtr - 1); $i >= 0; $i--) {
3✔
85
            if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
3✔
86
                $i++;
3✔
87
                break;
3✔
88
            }
89
        }
90

91
        if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) {
3✔
92
            $statementIndent = $tokens[$i]['length'];
3✔
93
        }
94

95
        // Each line between the parenthesis should be indented 4 spaces
96
        // and start with an operator, unless the line is inside a
97
        // function call, in which case it is ignored.
98
        $prevLine = $tokens[$openBracket]['line'];
3✔
99
        for ($i = ($openBracket + 1); $i <= $closeBracket; $i++) {
3✔
100
            if ($i === $closeBracket && $tokens[$openBracket]['line'] !== $tokens[$i]['line']) {
3✔
101
                $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($i - 1), null, true);
3✔
102
                if ($tokens[$prev]['line'] === $tokens[$i]['line']) {
3✔
103
                    // Closing bracket is on the same line as a condition.
104
                    $error = 'Closing parenthesis of a multi-line IF statement must be on a new line';
3✔
105
                    $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketNewLine');
3✔
106
                    if ($fix === true) {
3✔
107
                        // Account for a comment at the end of the line.
108
                        $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
3✔
109
                        if ($tokens[$next]['code'] !== T_COMMENT
3✔
110
                            && isset(Tokens::PHPCS_ANNOTATION_TOKENS[$tokens[$next]['code']]) === false
3✔
111
                        ) {
112
                            $phpcsFile->fixer->addNewlineBefore($closeBracket);
3✔
113
                        } else {
114
                            $next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($next + 1), null, true);
3✔
115
                            $phpcsFile->fixer->beginChangeset();
3✔
116
                            $phpcsFile->fixer->replaceToken($closeBracket, '');
3✔
117
                            $phpcsFile->fixer->addContentBefore($next, ')');
3✔
118
                            $phpcsFile->fixer->endChangeset();
3✔
119
                        }
120
                    }
121
                }
122
            }//end if
123

124
            if ($tokens[$i]['line'] !== $prevLine) {
3✔
125
                if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
3✔
126
                    $next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true);
3✔
127
                    if ($next !== $closeBracket) {
3✔
128
                        $expectedIndent = ($statementIndent + $this->indent);
3✔
129
                    } else {
130
                        // Closing brace needs to be indented to the same level
131
                        // as the statement.
132
                        $expectedIndent = $statementIndent;
3✔
133
                    }//end if
134
                } else {
135
                    $expectedIndent = ($statementIndent + $this->indent);
3✔
136
                }//end if
137

138
                if ($tokens[$i]['code'] === T_COMMENT
3✔
139
                    || isset(Tokens::PHPCS_ANNOTATION_TOKENS[$tokens[$i]['code']]) === true
3✔
140
                ) {
141
                    $prevLine = $tokens[$i]['line'];
3✔
142
                    continue;
3✔
143
                }
144

145
                // We changed lines, so this should be a whitespace indent token.
146
                if ($tokens[$i]['code'] !== T_WHITESPACE) {
3✔
147
                    $foundIndent = 0;
3✔
148
                } else {
149
                    $foundIndent = $tokens[$i]['length'];
3✔
150
                }
151

152
                if ($expectedIndent !== $foundIndent) {
3✔
153
                    $error = 'Multi-line IF statement not indented correctly; expected %s spaces but found %s';
3✔
154
                    $data  = [
2✔
155
                        $expectedIndent,
3✔
156
                        $foundIndent,
3✔
157
                    ];
2✔
158

159
                    $fix = $phpcsFile->addFixableError($error, $i, 'Alignment', $data);
3✔
160
                    if ($fix === true) {
3✔
161
                        $spaces = str_repeat(' ', $expectedIndent);
3✔
162
                        if ($foundIndent === 0) {
3✔
163
                            $phpcsFile->fixer->addContentBefore($i, $spaces);
3✔
164
                        } else {
165
                            $phpcsFile->fixer->replaceToken($i, $spaces);
3✔
166
                        }
167
                    }
168
                }
169

170
                $next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, $i, null, true);
3✔
171
                if ($next !== $closeBracket && $tokens[$next]['line'] === $tokens[$i]['line']) {
3✔
172
                    if (isset(Tokens::BOOLEAN_OPERATORS[$tokens[$next]['code']]) === false) {
3✔
173
                        $prev    = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($i - 1), $openBracket, true);
3✔
174
                        $fixable = true;
3✔
175
                        if (isset(Tokens::BOOLEAN_OPERATORS[$tokens[$prev]['code']]) === false
3✔
176
                            && $phpcsFile->findNext(T_WHITESPACE, ($prev + 1), $next, true) !== false
3✔
177
                        ) {
178
                            // Condition spread over multi-lines interspersed with comments.
179
                            $fixable = false;
3✔
180
                        }
181

182
                        $error = 'Each line in a multi-line IF statement must begin with a boolean operator';
3✔
183
                        if ($fixable === false) {
3✔
184
                            $phpcsFile->addError($error, $next, 'StartWithBoolean');
3✔
185
                        } else {
186
                            $fix = $phpcsFile->addFixableError($error, $next, 'StartWithBoolean');
3✔
187
                            if ($fix === true) {
3✔
188
                                if (isset(Tokens::BOOLEAN_OPERATORS[$tokens[$prev]['code']]) === true) {
3✔
189
                                    $phpcsFile->fixer->beginChangeset();
3✔
190
                                    $phpcsFile->fixer->replaceToken($prev, '');
3✔
191
                                    $phpcsFile->fixer->addContentBefore($next, $tokens[$prev]['content'] . ' ');
3✔
192
                                    $phpcsFile->fixer->endChangeset();
3✔
193
                                } else {
194
                                    for ($x = ($prev + 1); $x < $next; $x++) {
3✔
195
                                        $phpcsFile->fixer->replaceToken($x, '');
3✔
196
                                    }
197
                                }
198
                            }
199
                        }
200
                    }//end if
201
                }//end if
202

203
                $prevLine = $tokens[$i]['line'];
3✔
204
            }//end if
205

206
            if (isset(Tokens::NAME_TOKENS[$tokens[$i]['code']]) === true) {
3✔
207
                $next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
3✔
208
                if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
3✔
209
                    // This is a function call, so skip to the end as they
210
                    // have their own indentation rules.
211
                    $i        = $tokens[$next]['parenthesis_closer'];
3✔
212
                    $prevLine = $tokens[$i]['line'];
3✔
213
                    continue;
3✔
214
                }
215
            }
216
        }//end for
217

218
        // From here on, we are checking the spacing of the opening and closing
219
        // braces. If this IF statement does not use braces, we end here.
220
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
3✔
221
            return;
3✔
222
        }
223

224
        // The opening brace needs to be one space away from the closing parenthesis.
225
        $openBrace = $tokens[$stackPtr]['scope_opener'];
3✔
226
        $next      = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), $openBrace, true);
3✔
227
        if ($next !== false) {
3✔
228
            // Probably comments in between tokens, so don't check.
229
            return;
3✔
230
        }
231

232
        if ($tokens[$openBrace]['line'] > $tokens[$closeBracket]['line']) {
3✔
233
            $length = -1;
3✔
234
        } elseif ($openBrace === ($closeBracket + 1)) {
3✔
235
            $length = 0;
3✔
236
        } elseif ($openBrace === ($closeBracket + 2)
3✔
237
            && $tokens[($closeBracket + 1)]['code'] === T_WHITESPACE
3✔
238
        ) {
239
            $length = $tokens[($closeBracket + 1)]['length'];
3✔
240
        } else {
241
            // Confused, so don't check.
UNCOV
242
            $length = 1;
×
243
        }
244

245
        if ($length === 1) {
3✔
246
            return;
3✔
247
        }
248

249
        $data = [$length];
3✔
250
        $code = 'SpaceBeforeOpenBrace';
3✔
251

252
        $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found ';
3✔
253
        if ($length === -1) {
3✔
254
            $error .= 'newline';
3✔
255
            $code   = 'NewlineBeforeOpenBrace';
3✔
256
        } else {
257
            $error .= '%s spaces';
3✔
258
        }
259

260
        $fix = $phpcsFile->addFixableError($error, ($closeBracket + 1), $code, $data);
3✔
261
        if ($fix === true) {
3✔
262
            if ($length === 0) {
3✔
263
                $phpcsFile->fixer->addContent($closeBracket, ' ');
3✔
264
            } else {
265
                $phpcsFile->fixer->replaceToken(($closeBracket + 1), ' ');
3✔
266
            }
267
        }
268
    }
1✔
269
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc