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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 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
    }//end register()
40

41

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

261
        $fix = $phpcsFile->addFixableError($error, ($closeBracket + 1), $code, $data);
3✔
262
        if ($fix === true) {
3✔
263
            if ($length === 0) {
3✔
264
                $phpcsFile->fixer->addContent($closeBracket, ' ');
3✔
265
            } else {
266
                $phpcsFile->fixer->replaceToken(($closeBracket + 1), ' ');
3✔
267
            }
268
        }
269

270
    }//end process()
1✔
271

272

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