• 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

88.02
/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php
1
<?php
2
/**
3
 * Verifies that inline control statements are not present.
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\Generic\Sniffs\ControlStructures;
11

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

16
class InlineControlStructureSniff implements Sniff
17
{
18

19
    /**
20
     * If true, an error will be thrown; otherwise a warning.
21
     *
22
     * @var boolean
23
     */
24
    public $error = true;
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_ELSE,
3✔
37
            T_ELSEIF,
3✔
38
            T_FOREACH,
3✔
39
            T_WHILE,
3✔
40
            T_DO,
3✔
41
            T_FOR,
3✔
42
        ];
2✔
43

44
    }//end register()
45

46

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

60
        if (isset($tokens[$stackPtr]['scope_opener']) === true) {
3✔
61
            $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
3✔
62
            return;
3✔
63
        }
64

65
        // Ignore the ELSE in ELSE IF. We'll process the IF part later.
66
        if ($tokens[$stackPtr]['code'] === T_ELSE) {
3✔
67
            $next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
68
            if ($tokens[$next]['code'] === T_IF) {
3✔
69
                return;
3✔
70
            }
71
        }
72

73
        if ($tokens[$stackPtr]['code'] === T_WHILE || $tokens[$stackPtr]['code'] === T_FOR) {
3✔
74
            // This could be from a DO WHILE, which doesn't have an opening brace or a while/for without body.
75
            if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
3✔
76
                $afterParensCloser = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, true);
3✔
77
                if ($afterParensCloser === false) {
3✔
78
                    // Live coding.
79
                    return;
×
80
                }
81

82
                if ($tokens[$afterParensCloser]['code'] === T_SEMICOLON) {
3✔
83
                    $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
3✔
84
                    return;
3✔
85
                }
86
            }
87
        }//end if
88

89
        if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false
3✔
90
            && $tokens[$stackPtr]['code'] !== T_ELSE
3✔
91
        ) {
92
            if ($tokens[$stackPtr]['code'] !== T_DO) {
3✔
93
                // Live coding or parse error.
94
                return;
3✔
95
            }
96

97
            $nextWhile = $phpcsFile->findNext(T_WHILE, ($stackPtr + 1));
3✔
98
            if ($nextWhile !== false
3✔
99
                && isset($tokens[$nextWhile]['parenthesis_opener'], $tokens[$nextWhile]['parenthesis_closer']) === false
3✔
100
            ) {
101
                // Live coding or parse error.
102
                return;
3✔
103
            }
104

105
            unset($nextWhile);
×
106
        }
107

108
        $start = $stackPtr;
3✔
109
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
3✔
110
            $start = $tokens[$stackPtr]['parenthesis_closer'];
3✔
111
        }
112

113
        $nextNonEmpty = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($start + 1), null, true);
3✔
114
        if ($nextNonEmpty === false) {
3✔
115
            // Live coding or parse error.
116
            return;
3✔
117
        }
118

119
        if ($tokens[$nextNonEmpty]['code'] === T_OPEN_CURLY_BRACKET
3✔
120
            || $tokens[$nextNonEmpty]['code'] === T_COLON
3✔
121
        ) {
122
            // T_CLOSE_CURLY_BRACKET missing, or alternative control structure with
123
            // T_END... missing. Either live coding, parse error or end
124
            // tag in short open tags and scan run with short_open_tag=Off.
125
            // Bow out completely as any further detection will be unreliable
126
            // and create incorrect fixes or cause fixer conflicts.
127
            return $phpcsFile->numTokens;
2✔
128
        }
129

130
        unset($nextNonEmpty, $start);
3✔
131

132
        // This is a control structure without an opening brace,
133
        // so it is an inline statement.
134
        if ($this->error === true) {
3✔
135
            $fix = $phpcsFile->addFixableError('Inline control structures are not allowed', $stackPtr, 'NotAllowed');
3✔
136
        } else {
137
            $fix = $phpcsFile->addFixableWarning('Inline control structures are discouraged', $stackPtr, 'Discouraged');
×
138
        }
139

140
        $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'yes');
3✔
141

142
        // Stop here if we are not fixing the error.
143
        if ($fix !== true) {
3✔
144
            return;
3✔
145
        }
146

147
        $phpcsFile->fixer->beginChangeset();
3✔
148
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
3✔
149
            $closer = $tokens[$stackPtr]['parenthesis_closer'];
3✔
150
        } else {
151
            $closer = $stackPtr;
3✔
152
        }
153

154
        if ($tokens[($closer + 1)]['code'] === T_WHITESPACE
3✔
155
            || $tokens[($closer + 1)]['code'] === T_SEMICOLON
3✔
156
        ) {
157
            $phpcsFile->fixer->addContent($closer, ' {');
3✔
158
        } else {
159
            $phpcsFile->fixer->addContent($closer, ' { ');
×
160
        }
161

162
        $fixableScopeOpeners = $this->register();
3✔
163

164
        $lastNonEmpty = $closer;
3✔
165
        for ($end = ($closer + 1); $end < $phpcsFile->numTokens; $end++) {
3✔
166
            if ($tokens[$end]['code'] === T_SEMICOLON) {
3✔
167
                break;
3✔
168
            }
169

170
            if ($tokens[$end]['code'] === T_CLOSE_TAG) {
3✔
171
                $end = $lastNonEmpty;
3✔
172
                break;
3✔
173
            }
174

175
            if (in_array($tokens[$end]['code'], $fixableScopeOpeners, true) === true
3✔
176
                && isset($tokens[$end]['scope_opener']) === false
3✔
177
            ) {
178
                // The best way to fix nested inline scopes is middle-out.
179
                // So skip this one. It will be detected and fixed on a future loop.
180
                $phpcsFile->fixer->rollbackChangeset();
3✔
181
                return;
3✔
182
            }
183

184
            if (isset($tokens[$end]['scope_opener']) === true) {
3✔
185
                $type = $tokens[$end]['code'];
3✔
186
                $end  = $tokens[$end]['scope_closer'];
3✔
187
                if ($type === T_DO
3✔
188
                    || $type === T_IF || $type === T_ELSEIF
3✔
189
                    || $type === T_TRY || $type === T_CATCH || $type === T_FINALLY
3✔
190
                ) {
191
                    $next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($end + 1), null, true);
3✔
192
                    if ($next === false) {
3✔
193
                        break;
×
194
                    }
195

196
                    $nextType = $tokens[$next]['code'];
3✔
197

198
                    // Let additional conditions loop and find their ending.
199
                    if (($type === T_IF
3✔
200
                        || $type === T_ELSEIF)
3✔
201
                        && ($nextType === T_ELSEIF
3✔
202
                        || $nextType === T_ELSE)
3✔
203
                    ) {
204
                        continue;
3✔
205
                    }
206

207
                    // Account for TRY... CATCH/FINALLY statements.
208
                    if (($type === T_TRY
3✔
209
                        || $type === T_CATCH
3✔
210
                        || $type === T_FINALLY)
3✔
211
                        && ($nextType === T_CATCH
3✔
212
                        || $nextType === T_FINALLY)
3✔
213
                    ) {
214
                        continue;
3✔
215
                    }
216

217
                    // Account for DO... WHILE conditions.
218
                    if ($type === T_DO && $nextType === T_WHILE) {
3✔
219
                        $end = $phpcsFile->findNext(T_SEMICOLON, ($next + 1));
3✔
220
                    }
221
                } else if ($type === T_CLOSURE) {
3✔
222
                    // There should be a semicolon after the closing brace.
223
                    $next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($end + 1), null, true);
3✔
224
                    if ($next !== false && $tokens[$next]['code'] === T_SEMICOLON) {
3✔
225
                        $end = $next;
3✔
226
                    }
227
                }//end if
228

229
                if ($tokens[$end]['code'] !== T_END_HEREDOC
3✔
230
                    && $tokens[$end]['code'] !== T_END_NOWDOC
3✔
231
                ) {
232
                    break;
3✔
233
                }
234
            }//end if
235

236
            if (isset($tokens[$end]['parenthesis_closer']) === true) {
3✔
237
                $end          = $tokens[$end]['parenthesis_closer'];
3✔
238
                $lastNonEmpty = $end;
3✔
239
                continue;
3✔
240
            }
241

242
            if ($tokens[$end]['code'] !== T_WHITESPACE) {
3✔
243
                $lastNonEmpty = $end;
3✔
244
            }
245
        }//end for
246

247
        if ($end === $phpcsFile->numTokens) {
3✔
248
            $end = $lastNonEmpty;
×
249
        }
250

251
        $nextContent = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($end + 1), null, true);
3✔
252
        if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
3✔
253
            // Looks for completely empty statements.
254
            $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
3✔
255
        } else {
256
            $next    = ($end + 1);
3✔
257
            $endLine = $end;
3✔
258
        }
259

260
        if ($next !== $end) {
3✔
261
            if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
3✔
262
                // Account for a comment on the end of the line.
263
                for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
3✔
264
                    if (isset($tokens[($endLine + 1)]) === false
3✔
265
                        || $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
3✔
266
                    ) {
267
                        break;
3✔
268
                    }
269
                }
270

271
                if (isset(Tokens::COMMENT_TOKENS[$tokens[$endLine]['code']]) === false
3✔
272
                    && ($tokens[$endLine]['code'] !== T_WHITESPACE
3✔
273
                    || isset(Tokens::COMMENT_TOKENS[$tokens[($endLine - 1)]['code']]) === false)
3✔
274
                ) {
275
                    $endLine = $end;
3✔
276
                }
277
            }
278

279
            if ($endLine !== $end) {
3✔
280
                $endToken     = $endLine;
3✔
281
                $addedContent = '';
3✔
282
            } else {
283
                $endToken     = $end;
3✔
284
                $addedContent = $phpcsFile->eolChar;
3✔
285

286
                if ($tokens[$end]['code'] !== T_SEMICOLON
3✔
287
                    && $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
3✔
288
                ) {
289
                    $phpcsFile->fixer->addContent($end, '; ');
3✔
290
                }
291
            }
292

293
            $next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
3✔
294
            if ($next !== false
3✔
295
                && ($tokens[$next]['code'] === T_ELSE
3✔
296
                || $tokens[$next]['code'] === T_ELSEIF)
3✔
297
            ) {
298
                $phpcsFile->fixer->addContentBefore($next, '} ');
3✔
299
            } else {
300
                $indent = '';
3✔
301
                for ($first = $stackPtr; $first > 0; $first--) {
3✔
302
                    if ($tokens[$first]['column'] === 1) {
3✔
303
                        break;
3✔
304
                    }
305
                }
306

307
                if ($tokens[$first]['code'] === T_WHITESPACE) {
3✔
308
                    $indent = $tokens[$first]['content'];
3✔
309
                } else if ($tokens[$first]['code'] === T_INLINE_HTML
3✔
310
                    || $tokens[$first]['code'] === T_OPEN_TAG
3✔
311
                ) {
312
                    $addedContent = '';
3✔
313
                }
314

315
                $addedContent .= $indent.'}';
3✔
316
                if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
3✔
317
                    $addedContent .= $phpcsFile->eolChar;
3✔
318
                }
319

320
                $phpcsFile->fixer->addContent($endToken, $addedContent);
3✔
321
            }//end if
322
        } else {
323
            if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
×
324
                // Account for a comment on the end of the line.
325
                for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
×
326
                    if (isset($tokens[($endLine + 1)]) === false
×
327
                        || $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
×
328
                    ) {
329
                        break;
×
330
                    }
331
                }
332

333
                if ($tokens[$endLine]['code'] !== T_COMMENT
×
334
                    && ($tokens[$endLine]['code'] !== T_WHITESPACE
×
335
                    || $tokens[($endLine - 1)]['code'] !== T_COMMENT)
×
336
                ) {
337
                    $endLine = $end;
×
338
                }
339
            }
340

341
            if ($endLine !== $end) {
×
342
                $phpcsFile->fixer->replaceToken($end, '');
×
343
                $phpcsFile->fixer->addNewlineBefore($endLine);
×
344
                $phpcsFile->fixer->addContent($endLine, '}');
×
345
            } else {
346
                $phpcsFile->fixer->replaceToken($end, '}');
×
347
            }
348
        }//end if
349

350
        $phpcsFile->fixer->endChangeset();
3✔
351

352
    }//end process()
1✔
353

354

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