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

PHPCSStandards / PHP_CodeSniffer / 12658687769

07 Jan 2025 08:19PM UTC coverage: 78.321% (+0.2%) from 78.074%
12658687769

Pull #620

github

web-flow
Merge 4e97c930c into b43e1d8e8
Pull Request #620: Fix fixer conflict: `PSR12` / `Squiz.Functions.FunctionDeclarationArgumentSpacing`

17 of 17 new or added lines in 1 file covered. (100.0%)

21 existing lines in 3 files now uncovered.

24477 of 31252 relevant lines covered (78.32%)

65.93 hits per line

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

90.78
/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
     * A list of tokenizers this sniff supports.
21
     *
22
     * @var array
23
     */
24
    public $supportedTokenizers = [
25
        'PHP',
26
        'JS',
27
    ];
28

29
    /**
30
     * If true, an error will be thrown; otherwise a warning.
31
     *
32
     * @var boolean
33
     */
34
    public $error = true;
35

36

37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return array<int|string>
41
     */
42
    public function register()
3✔
43
    {
44
        return [
1✔
45
            T_IF,
3✔
46
            T_ELSE,
3✔
47
            T_ELSEIF,
3✔
48
            T_FOREACH,
3✔
49
            T_WHILE,
3✔
50
            T_DO,
3✔
51
            T_FOR,
3✔
52
        ];
2✔
53

54
    }//end register()
55

56

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

70
        if (isset($tokens[$stackPtr]['scope_opener']) === true) {
3✔
71
            $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
3✔
72
            return;
3✔
73
        }
74

75
        // Ignore the ELSE in ELSE IF. We'll process the IF part later.
76
        if ($tokens[$stackPtr]['code'] === T_ELSE) {
3✔
77
            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
3✔
78
            if ($tokens[$next]['code'] === T_IF) {
3✔
79
                return;
3✔
80
            }
81
        }
1✔
82

83
        if ($tokens[$stackPtr]['code'] === T_WHILE || $tokens[$stackPtr]['code'] === T_FOR) {
3✔
84
            // This could be from a DO WHILE, which doesn't have an opening brace or a while/for without body.
85
            if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
3✔
86
                $afterParensCloser = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, true);
3✔
87
                if ($afterParensCloser === false) {
3✔
88
                    // Live coding.
UNCOV
89
                    return;
×
90
                }
91

92
                if ($tokens[$afterParensCloser]['code'] === T_SEMICOLON) {
3✔
93
                    $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
3✔
94
                    return;
3✔
95
                }
96
            }
1✔
97
        }//end if
1✔
98

99
        if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false
3✔
100
            && $tokens[$stackPtr]['code'] !== T_ELSE
3✔
101
        ) {
1✔
102
            if ($tokens[$stackPtr]['code'] !== T_DO) {
3✔
103
                // Live coding or parse error.
104
                return;
3✔
105
            }
106

107
            $nextWhile = $phpcsFile->findNext(T_WHILE, ($stackPtr + 1));
3✔
108
            if ($nextWhile !== false
2✔
109
                && isset($tokens[$nextWhile]['parenthesis_opener'], $tokens[$nextWhile]['parenthesis_closer']) === false
3✔
110
            ) {
1✔
111
                // Live coding or parse error.
112
                return;
3✔
113
            }
114

115
            unset($nextWhile);
3✔
116
        }
1✔
117

118
        $start = $stackPtr;
3✔
119
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
3✔
120
            $start = $tokens[$stackPtr]['parenthesis_closer'];
3✔
121
        }
1✔
122

123
        $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($start + 1), null, true);
3✔
124
        if ($nextNonEmpty === false) {
3✔
125
            // Live coding or parse error.
126
            return;
3✔
127
        }
128

129
        if ($tokens[$nextNonEmpty]['code'] === T_OPEN_CURLY_BRACKET
3✔
130
            || $tokens[$nextNonEmpty]['code'] === T_COLON
3✔
131
        ) {
1✔
132
            // T_CLOSE_CURLY_BRACKET missing, or alternative control structure with
133
            // T_END... missing. Either live coding, parse error or end
134
            // tag in short open tags and scan run with short_open_tag=Off.
135
            // Bow out completely as any further detection will be unreliable
136
            // and create incorrect fixes or cause fixer conflicts.
137
            return $phpcsFile->numTokens;
2✔
138
        }
139

140
        unset($nextNonEmpty, $start);
3✔
141

142
        // This is a control structure without an opening brace,
143
        // so it is an inline statement.
144
        if ($this->error === true) {
3✔
145
            $fix = $phpcsFile->addFixableError('Inline control structures are not allowed', $stackPtr, 'NotAllowed');
3✔
146
        } else {
1✔
UNCOV
147
            $fix = $phpcsFile->addFixableWarning('Inline control structures are discouraged', $stackPtr, 'Discouraged');
×
148
        }
149

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

152
        // Stop here if we are not fixing the error.
153
        if ($fix !== true) {
3✔
154
            return;
3✔
155
        }
156

157
        $phpcsFile->fixer->beginChangeset();
3✔
158
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
3✔
159
            $closer = $tokens[$stackPtr]['parenthesis_closer'];
3✔
160
        } else {
1✔
161
            $closer = $stackPtr;
3✔
162
        }
163

164
        if ($tokens[($closer + 1)]['code'] === T_WHITESPACE
3✔
165
            || $tokens[($closer + 1)]['code'] === T_SEMICOLON
3✔
166
        ) {
1✔
167
            $phpcsFile->fixer->addContent($closer, ' {');
3✔
168
        } else {
1✔
UNCOV
169
            $phpcsFile->fixer->addContent($closer, ' { ');
×
170
        }
171

172
        $fixableScopeOpeners = $this->register();
3✔
173

174
        $lastNonEmpty = $closer;
3✔
175
        for ($end = ($closer + 1); $end < $phpcsFile->numTokens; $end++) {
3✔
176
            if ($tokens[$end]['code'] === T_SEMICOLON) {
3✔
177
                break;
3✔
178
            }
179

180
            if ($tokens[$end]['code'] === T_CLOSE_TAG) {
3✔
181
                $end = $lastNonEmpty;
3✔
182
                break;
3✔
183
            }
184

185
            if (in_array($tokens[$end]['code'], $fixableScopeOpeners, true) === true
3✔
186
                && isset($tokens[$end]['scope_opener']) === false
3✔
187
            ) {
1✔
188
                // The best way to fix nested inline scopes is middle-out.
189
                // So skip this one. It will be detected and fixed on a future loop.
190
                $phpcsFile->fixer->rollbackChangeset();
3✔
191
                return;
3✔
192
            }
193

194
            if (isset($tokens[$end]['scope_opener']) === true) {
3✔
195
                $type = $tokens[$end]['code'];
3✔
196
                $end  = $tokens[$end]['scope_closer'];
3✔
197
                if ($type === T_DO
2✔
198
                    || $type === T_IF || $type === T_ELSEIF
3✔
199
                    || $type === T_TRY || $type === T_CATCH || $type === T_FINALLY
3✔
200
                ) {
1✔
201
                    $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
3✔
202
                    if ($next === false) {
3✔
UNCOV
203
                        break;
×
204
                    }
205

206
                    $nextType = $tokens[$next]['code'];
3✔
207

208
                    // Let additional conditions loop and find their ending.
209
                    if (($type === T_IF
2✔
210
                        || $type === T_ELSEIF)
3✔
211
                        && ($nextType === T_ELSEIF
3✔
212
                        || $nextType === T_ELSE)
3✔
213
                    ) {
1✔
214
                        continue;
3✔
215
                    }
216

217
                    // Account for TRY... CATCH/FINALLY statements.
218
                    if (($type === T_TRY
2✔
219
                        || $type === T_CATCH
3✔
220
                        || $type === T_FINALLY)
3✔
221
                        && ($nextType === T_CATCH
3✔
222
                        || $nextType === T_FINALLY)
3✔
223
                    ) {
1✔
224
                        continue;
3✔
225
                    }
226

227
                    // Account for DO... WHILE conditions.
228
                    if ($type === T_DO && $nextType === T_WHILE) {
3✔
229
                        $end = $phpcsFile->findNext(T_SEMICOLON, ($next + 1));
3✔
230
                    }
1✔
231
                } else if ($type === T_CLOSURE) {
3✔
232
                    // There should be a semicolon after the closing brace.
233
                    $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
3✔
234
                    if ($next !== false && $tokens[$next]['code'] === T_SEMICOLON) {
3✔
235
                        $end = $next;
3✔
236
                    }
1✔
237
                }//end if
1✔
238

239
                if ($tokens[$end]['code'] !== T_END_HEREDOC
3✔
240
                    && $tokens[$end]['code'] !== T_END_NOWDOC
3✔
241
                ) {
1✔
242
                    break;
3✔
243
                }
244
            }//end if
1✔
245

246
            if (isset($tokens[$end]['parenthesis_closer']) === true) {
3✔
247
                $end          = $tokens[$end]['parenthesis_closer'];
3✔
248
                $lastNonEmpty = $end;
3✔
249
                continue;
3✔
250
            }
251

252
            if ($tokens[$end]['code'] !== T_WHITESPACE) {
3✔
253
                $lastNonEmpty = $end;
3✔
254
            }
1✔
255
        }//end for
1✔
256

257
        if ($end === $phpcsFile->numTokens) {
3✔
UNCOV
258
            $end = $lastNonEmpty;
×
259
        }
260

261
        $nextContent = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
3✔
262
        if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
3✔
263
            // Looks for completely empty statements.
264
            $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
3✔
265
        } else {
1✔
266
            $next    = ($end + 1);
3✔
267
            $endLine = $end;
3✔
268
        }
269

270
        if ($next !== $end) {
3✔
271
            if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
3✔
272
                // Account for a comment on the end of the line.
273
                for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
3✔
274
                    if (isset($tokens[($endLine + 1)]) === false
3✔
275
                        || $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
3✔
276
                    ) {
1✔
277
                        break;
3✔
278
                    }
279
                }
1✔
280

281
                if (isset(Tokens::$commentTokens[$tokens[$endLine]['code']]) === false
3✔
282
                    && ($tokens[$endLine]['code'] !== T_WHITESPACE
3✔
283
                    || isset(Tokens::$commentTokens[$tokens[($endLine - 1)]['code']]) === false)
3✔
284
                ) {
1✔
285
                    $endLine = $end;
3✔
286
                }
1✔
287
            }
1✔
288

289
            if ($endLine !== $end) {
3✔
290
                $endToken     = $endLine;
3✔
291
                $addedContent = '';
3✔
292
            } else {
1✔
293
                $endToken     = $end;
3✔
294
                $addedContent = $phpcsFile->eolChar;
3✔
295

296
                if ($tokens[$end]['code'] !== T_SEMICOLON
3✔
297
                    && $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
3✔
298
                ) {
1✔
299
                    $phpcsFile->fixer->addContent($end, '; ');
3✔
300
                }
1✔
301
            }
302

303
            $next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
3✔
304
            if ($next !== false
2✔
305
                && ($tokens[$next]['code'] === T_ELSE
3✔
306
                || $tokens[$next]['code'] === T_ELSEIF)
3✔
307
            ) {
1✔
308
                $phpcsFile->fixer->addContentBefore($next, '} ');
3✔
309
            } else {
1✔
310
                $indent = '';
3✔
311
                for ($first = $stackPtr; $first > 0; $first--) {
3✔
312
                    if ($tokens[$first]['column'] === 1) {
3✔
313
                        break;
3✔
314
                    }
315
                }
1✔
316

317
                if ($tokens[$first]['code'] === T_WHITESPACE) {
3✔
318
                    $indent = $tokens[$first]['content'];
3✔
319
                } else if ($tokens[$first]['code'] === T_INLINE_HTML
3✔
320
                    || $tokens[$first]['code'] === T_OPEN_TAG
3✔
321
                ) {
1✔
322
                    $addedContent = '';
3✔
323
                }
1✔
324

325
                $addedContent .= $indent.'}';
3✔
326
                if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
3✔
327
                    $addedContent .= $phpcsFile->eolChar;
3✔
328
                }
1✔
329

330
                $phpcsFile->fixer->addContent($endToken, $addedContent);
3✔
331
            }//end if
332
        } else {
1✔
UNCOV
333
            if ($nextContent === false || $tokens[$nextContent]['line'] !== $tokens[$end]['line']) {
×
334
                // Account for a comment on the end of the line.
UNCOV
335
                for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) {
×
336
                    if (isset($tokens[($endLine + 1)]) === false
×
337
                        || $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line']
×
338
                    ) {
UNCOV
339
                        break;
×
340
                    }
341
                }
342

UNCOV
343
                if ($tokens[$endLine]['code'] !== T_COMMENT
×
344
                    && ($tokens[$endLine]['code'] !== T_WHITESPACE
×
345
                    || $tokens[($endLine - 1)]['code'] !== T_COMMENT)
×
346
                ) {
UNCOV
347
                    $endLine = $end;
×
348
                }
349
            }
350

UNCOV
351
            if ($endLine !== $end) {
×
352
                $phpcsFile->fixer->replaceToken($end, '');
×
353
                $phpcsFile->fixer->addNewlineBefore($endLine);
×
354
                $phpcsFile->fixer->addContent($endLine, '}');
×
355
            } else {
UNCOV
356
                $phpcsFile->fixer->replaceToken($end, '}');
×
357
            }
358
        }//end if
359

360
        $phpcsFile->fixer->endChangeset();
3✔
361

362
    }//end process()
2✔
363

364

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