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

PHPCSStandards / PHP_CodeSniffer / 21049087423

15 Jan 2026 10:49PM UTC coverage: 78.836% (-0.07%) from 78.903%
21049087423

Pull #1292

github

web-flow
Merge 494e04bb1 into 44c338e38
Pull Request #1292: Define polyfilled T_* constants from Tokenizer as int

3 of 24 new or added lines in 1 file covered. (12.5%)

6 existing lines in 1 file now uncovered.

19828 of 25151 relevant lines covered (78.84%)

98.87 hits per line

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

95.53
/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php
1
<?php
2
/**
3
 * Ensures all switch statements are defined correctly.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @copyright 2023 PHPCSStandards and contributors
8
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
9
 */
10

11
namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures;
12

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

17
class SwitchDeclarationSniff implements Sniff
18
{
19

20
    /**
21
     * Tokens which can terminate a "case".
22
     *
23
     * @var array<int|string, int|string>
24
     */
25
    private const CASE_TERMINATING_TOKENS = [
26
        T_RETURN   => T_RETURN,
27
        T_BREAK    => T_BREAK,
28
        T_CONTINUE => T_CONTINUE,
29
        T_THROW    => T_THROW,
30
        T_EXIT     => T_EXIT,
31
        T_GOTO     => T_GOTO,
32
    ];
33

34
    /**
35
     * The number of spaces code should be indented.
36
     *
37
     * @var integer
38
     */
39
    public $indent = 4;
40

41

42
    /**
43
     * Returns an array of tokens this test wants to listen for.
44
     *
45
     * @return array<int|string>
46
     */
47
    public function register()
3✔
48
    {
49
        return [T_SWITCH];
3✔
50
    }
51

52

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

66
        // We can't process SWITCH statements unless we know where they start and end.
67
        if (isset($tokens[$stackPtr]['scope_opener']) === false
3✔
68
            || isset($tokens[$stackPtr]['scope_closer']) === false
3✔
69
        ) {
70
            return;
×
71
        }
72

73
        $switch        = $tokens[$stackPtr];
3✔
74
        $nextCase      = $stackPtr;
3✔
75
        $caseAlignment = ($switch['column'] + $this->indent);
3✔
76

77
        while (($nextCase = $this->findNextCase($phpcsFile, ($nextCase + 1), $switch['scope_closer'])) !== false) {
3✔
78
            if ($tokens[$nextCase]['code'] === T_DEFAULT) {
3✔
79
                $type = 'default';
3✔
80
            } else {
81
                $type = 'case';
3✔
82
            }
83

84
            if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) {
3✔
85
                $expected = strtolower($tokens[$nextCase]['content']);
3✔
86
                $error    = strtoupper($type) . ' keyword must be lowercase; expected "%s" but found "%s"';
3✔
87
                $data     = [
2✔
88
                    $expected,
3✔
89
                    $tokens[$nextCase]['content'],
3✔
90
                ];
2✔
91

92
                $fix = $phpcsFile->addFixableError($error, $nextCase, $type . 'NotLower', $data);
3✔
93
                if ($fix === true) {
3✔
94
                    $phpcsFile->fixer->replaceToken($nextCase, $expected);
3✔
95
                }
96
            }
97

98
            if ($type === 'case'
3✔
99
                && ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE
3✔
100
                || $tokens[($nextCase + 1)]['content'] !== ' ')
3✔
101
            ) {
102
                $error = 'CASE keyword must be followed by a single space';
3✔
103
                $fix   = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase');
3✔
104
                if ($fix === true) {
3✔
105
                    if ($tokens[($nextCase + 1)]['code'] !== T_WHITESPACE) {
3✔
106
                        $phpcsFile->fixer->addContent($nextCase, ' ');
3✔
107
                    } else {
108
                        $phpcsFile->fixer->replaceToken(($nextCase + 1), ' ');
3✔
109
                    }
110
                }
111
            }
112

113
            $opener     = $tokens[$nextCase]['scope_opener'];
3✔
114
            $nextCloser = $tokens[$nextCase]['scope_closer'];
3✔
115
            if ($tokens[$opener]['code'] === T_COLON) {
3✔
116
                if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) {
3✔
117
                    $error = 'There must be no space before the colon in a ' . strtoupper($type) . ' statement';
3✔
118
                    $fix   = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon' . strtoupper($type));
3✔
119
                    if ($fix === true) {
3✔
120
                        $phpcsFile->fixer->replaceToken(($opener - 1), '');
3✔
121
                    }
122
                }
123

124
                for ($next = ($opener + 1); $next < $nextCloser; $next++) {
3✔
125
                    if (isset(Tokens::EMPTY_TOKENS[$tokens[$next]['code']]) === false
3✔
126
                        || (isset(Tokens::COMMENT_TOKENS[$tokens[$next]['code']]) === true
3✔
127
                        && $tokens[$next]['line'] !== $tokens[$opener]['line'])
3✔
128
                    ) {
129
                        break;
3✔
130
                    }
131
                }
132

133
                if ($tokens[$next]['line'] !== ($tokens[$opener]['line'] + 1)) {
3✔
134
                    $error = 'The ' . strtoupper($type) . ' body must start on the line following the statement';
3✔
135
                    $fix   = $phpcsFile->addFixableError($error, $nextCase, 'BodyOnNextLine' . strtoupper($type));
3✔
136
                    if ($fix === true) {
3✔
137
                        if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
3✔
138
                            $padding = str_repeat(' ', ($caseAlignment + $this->indent - 1));
3✔
139
                            $phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar . $padding);
3✔
140
                        } else {
141
                            $phpcsFile->fixer->beginChangeset();
3✔
142
                            for ($i = ($opener + 1); $i < $next; $i++) {
3✔
143
                                if ($tokens[$i]['line'] === $tokens[$opener]['line']) {
3✔
144
                                    // Ignore trailing comments.
145
                                    continue;
3✔
146
                                }
147

148
                                if ($tokens[$i]['line'] === $tokens[$next]['line']) {
3✔
149
                                    break;
3✔
150
                                }
151

152
                                $phpcsFile->fixer->replaceToken($i, '');
3✔
153
                            }
154

155
                            $phpcsFile->fixer->endChangeset();
3✔
156
                        }
157
                    }
158
                }
159

160
                if ($tokens[$nextCloser]['scope_condition'] === $nextCase) {
3✔
161
                    // Only need to check some things once, even if the
162
                    // closer is shared between multiple case statements, or even
163
                    // the default case.
164
                    $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCloser - 1), $nextCase, true);
3✔
165
                    if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) {
3✔
166
                        $error = 'Terminating statement must be on a line by itself';
3✔
167
                        $fix   = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine');
3✔
168
                        if ($fix === true) {
3✔
169
                            $phpcsFile->fixer->addNewline($prev);
3✔
170
                            $phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content']));
3✔
171
                        }
172
                    } else {
173
                        $diff = ($tokens[$nextCase]['column'] + $this->indent - $tokens[$nextCloser]['column']);
3✔
174
                        if ($diff !== 0) {
3✔
175
                            $error = 'Terminating statement must be indented to the same level as the CASE body';
3✔
176
                            $fix   = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');
3✔
177
                            if ($fix === true) {
3✔
178
                                if ($diff > 0) {
3✔
179
                                    $phpcsFile->fixer->addContentBefore($nextCloser, str_repeat(' ', $diff));
3✔
180
                                } else {
181
                                    $phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff);
3✔
182
                                }
183
                            }
184
                        }
185
                    }
186
                }
187
            } else {
188
                $error = strtoupper($type) . ' statements must be defined using a colon';
3✔
189
                if ($tokens[$opener]['code'] === T_SEMICOLON || $tokens[$opener]['code'] === T_CLOSE_TAG) {
3✔
190
                    $fix = $phpcsFile->addFixableError($error, $nextCase, 'WrongOpener' . $type);
3✔
191
                    if ($fix === true) {
3✔
192
                        if ($tokens[$opener]['code'] === T_SEMICOLON) {
3✔
193
                            $phpcsFile->fixer->replaceToken($opener, ':');
3✔
194
                        } else {
195
                            $prevNonEmpty = $phpcsFile->findPrevious(T_WHITESPACE, ($opener - 1), null, true);
3✔
196
                            $phpcsFile->fixer->addContent($prevNonEmpty, ':');
3✔
197
                        }
198
                    }
199
                } else {
200
                    if ($tokens[$opener]['code'] === T_OPEN_CURLY_BRACKET) {
3✔
201
                        $error = '%s statements must not use a braced block after the colon';
3✔
202
                        $phpcsFile->addError($error, $nextCase, 'WrongOpener', [strtoupper($type)]);
3✔
203
                    } else {
UNCOV
204
                        $phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type);
×
205
                    }
206
                }
207
            }
208

209
            // We only want cases from here on in.
210
            if ($type !== 'case') {
3✔
211
                continue;
3✔
212
            }
213

214
            $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), $nextCloser, true);
3✔
215

216
            if ($tokens[$nextCode]['code'] !== T_CASE && $tokens[$nextCode]['code'] !== T_DEFAULT) {
3✔
217
                // This case statement has content. If the next case or default comes
218
                // before the closer, it means we don't have an obvious terminating
219
                // statement and need to make some more effort to find one. If we
220
                // don't, we do need a comment.
221
                $nextCode = $this->findNextCase($phpcsFile, ($opener + 1), $nextCloser);
3✔
222
                if ($nextCode !== false) {
3✔
223
                    $prevCode = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCode - 1), $nextCase, true);
3✔
224
                    if (isset(Tokens::COMMENT_TOKENS[$tokens[$prevCode]['code']]) === false
3✔
225
                        && $this->findNestedTerminator($phpcsFile, ($opener + 1), $nextCode) === false
3✔
226
                    ) {
227
                        $error = 'There must be a comment when fall-through is intentional in a non-empty case body';
3✔
228
                        $phpcsFile->addError($error, $nextCase, 'TerminatingComment');
3✔
229
                    }
230
                }
231
            }
232
        }
233
    }
1✔
234

235

236
    /**
237
     * Find the next CASE or DEFAULT statement from a point in the file.
238
     *
239
     * Note that nested switches are ignored.
240
     *
241
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
242
     * @param int                         $stackPtr  The position to start looking at.
243
     * @param int                         $end       The position to stop looking at.
244
     *
245
     * @return int|false
246
     */
247
    private function findNextCase(File $phpcsFile, int $stackPtr, int $end)
3✔
248
    {
249
        $tokens = $phpcsFile->getTokens();
3✔
250
        while (($stackPtr = $phpcsFile->findNext([T_CASE, T_DEFAULT, T_SWITCH], $stackPtr, $end)) !== false) {
3✔
251
            // Skip nested SWITCH statements; they are handled on their own.
252
            if ($tokens[$stackPtr]['code'] === T_SWITCH) {
3✔
253
                $stackPtr = $tokens[$stackPtr]['scope_closer'];
3✔
254
                continue;
3✔
255
            }
256

257
            break;
3✔
258
        }
259

260
        return $stackPtr;
3✔
261
    }
262

263

264
    /**
265
     * Returns the position of the nested terminating statement.
266
     *
267
     * Returns false if no terminating statement was found.
268
     *
269
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
270
     * @param int                         $stackPtr  The position to start looking at.
271
     * @param int                         $end       The position to stop looking at.
272
     *
273
     * @return int|bool
274
     */
275
    private function findNestedTerminator(File $phpcsFile, int $stackPtr, int $end)
3✔
276
    {
277
        $tokens = $phpcsFile->getTokens();
3✔
278

279
        $lastToken = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($end - 1), $stackPtr, true);
3✔
280
        if ($lastToken === false) {
3✔
UNCOV
281
            return false;
×
282
        }
283

284
        if ($tokens[$lastToken]['code'] === T_CLOSE_CURLY_BRACKET) {
3✔
285
            // We found a closing curly bracket and want to check if its block
286
            // belongs to a SWITCH, IF, ELSEIF or ELSE, TRY, CATCH OR FINALLY clause.
287
            // If yes, we continue searching for a terminating statement within that
288
            // block. Note that we have to make sure that every block of
289
            // the entire if/else/switch statement has a terminating statement.
290
            // For a try/catch/finally statement, either the finally block has
291
            // to have a terminating statement or every try/catch block has to have one.
292
            $currentCloser = $lastToken;
3✔
293
            $hasElseBlock  = false;
3✔
294
            $hasCatchWithoutTerminator = false;
3✔
295
            do {
296
                $scopeOpener = $tokens[$currentCloser]['scope_opener'];
3✔
297
                $scopeCloser = $tokens[$currentCloser]['scope_closer'];
3✔
298

299
                $prevToken = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($scopeOpener - 1), $stackPtr, true);
3✔
300
                if ($prevToken === false) {
3✔
UNCOV
301
                    return false;
×
302
                }
303

304
                // SWITCH, IF, ELSEIF, CATCH clauses possess a condition we have to account for.
305
                if ($tokens[$prevToken]['code'] === T_CLOSE_PARENTHESIS) {
3✔
306
                    $prevToken = $tokens[$prevToken]['parenthesis_owner'];
3✔
307
                }
308

309
                if ($tokens[$prevToken]['code'] === T_IF) {
3✔
310
                    // If we have not encountered an ELSE clause by now, we cannot
311
                    // be sure that the whole statement terminates in every case.
312
                    if ($hasElseBlock === false) {
3✔
313
                        return false;
3✔
314
                    }
315

316
                    return $this->findNestedTerminator($phpcsFile, ($scopeOpener + 1), $scopeCloser);
3✔
317
                } elseif ($tokens[$prevToken]['code'] === T_ELSEIF
3✔
318
                    || $tokens[$prevToken]['code'] === T_ELSE
3✔
319
                ) {
320
                    // If we find a terminating statement within this block,
321
                    // we continue with the previous ELSEIF or IF clause.
322
                    $hasTerminator = $this->findNestedTerminator($phpcsFile, ($scopeOpener + 1), $scopeCloser);
3✔
323
                    if ($hasTerminator === false) {
3✔
324
                        return false;
3✔
325
                    }
326

327
                    $currentCloser = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($prevToken - 1), $stackPtr, true);
3✔
328
                    if ($tokens[$prevToken]['code'] === T_ELSE) {
3✔
329
                        $hasElseBlock = true;
3✔
330
                    }
331
                } elseif ($tokens[$prevToken]['code'] === T_FINALLY) {
3✔
332
                    // If we find a terminating statement within this block,
333
                    // the whole try/catch/finally statement is covered.
334
                    $hasTerminator = $this->findNestedTerminator($phpcsFile, ($scopeOpener + 1), $scopeCloser);
3✔
335
                    if ($hasTerminator !== false) {
3✔
336
                        return $hasTerminator;
3✔
337
                    }
338

339
                    // Otherwise, we continue with the previous TRY or CATCH clause.
340
                    $currentCloser = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($prevToken - 1), $stackPtr, true);
3✔
341
                } elseif ($tokens[$prevToken]['code'] === T_TRY) {
3✔
342
                    // If we've seen CATCH blocks without terminator statement and
343
                    // have not seen a FINALLY *with* a terminator statement, we
344
                    // don't even need to bother checking the TRY.
345
                    if ($hasCatchWithoutTerminator === true) {
3✔
346
                        return false;
3✔
347
                    }
348

349
                    return $this->findNestedTerminator($phpcsFile, ($scopeOpener + 1), $scopeCloser);
3✔
350
                } elseif ($tokens[$prevToken]['code'] === T_CATCH) {
3✔
351
                    // Keep track of seen catch statements without terminating statement,
352
                    // but don't bow out yet as there may still be a FINALLY clause
353
                    // with a terminating statement before the CATCH.
354
                    $hasTerminator = $this->findNestedTerminator($phpcsFile, ($scopeOpener + 1), $scopeCloser);
3✔
355
                    if ($hasTerminator === false) {
3✔
356
                        $hasCatchWithoutTerminator = true;
3✔
357
                    }
358

359
                    $currentCloser = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($prevToken - 1), $stackPtr, true);
3✔
360
                } elseif ($tokens[$prevToken]['code'] === T_SWITCH) {
3✔
361
                    $hasDefaultBlock = false;
3✔
362
                    $endOfSwitch     = $tokens[$prevToken]['scope_closer'];
3✔
363
                    $nextCase        = $prevToken;
3✔
364

365
                    // We look for a terminating statement within every blocks.
366
                    while (($nextCase = $this->findNextCase($phpcsFile, ($nextCase + 1), $endOfSwitch)) !== false) {
3✔
367
                        if ($tokens[$nextCase]['code'] === T_DEFAULT) {
3✔
368
                            $hasDefaultBlock = true;
3✔
369
                        }
370

371
                        $opener = $tokens[$nextCase]['scope_opener'];
3✔
372

373
                        $nextCode = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($opener + 1), $endOfSwitch, true);
3✔
374
                        if ($tokens[$nextCode]['code'] === T_CASE || $tokens[$nextCode]['code'] === T_DEFAULT) {
3✔
375
                            // This case statement has no content, so skip it.
UNCOV
376
                            continue;
×
377
                        }
378

379
                        $endOfCase = $this->findNextCase($phpcsFile, ($opener + 1), $endOfSwitch);
3✔
380
                        if ($endOfCase === false) {
3✔
381
                            $endOfCase = $endOfSwitch;
3✔
382
                        }
383

384
                        $hasTerminator = $this->findNestedTerminator($phpcsFile, ($opener + 1), $endOfCase);
3✔
385
                        if ($hasTerminator === false) {
3✔
386
                            return false;
3✔
387
                        }
388
                    }
389

390
                    // If we have not encountered a DEFAULT block by now, we cannot
391
                    // be sure that the whole statement terminates in every case.
392
                    if ($hasDefaultBlock === false) {
3✔
UNCOV
393
                        return false;
×
394
                    }
395

396
                    return $hasTerminator;
3✔
397
                } else {
398
                    return false;
×
399
                }
400
            } while ($currentCloser !== false && $tokens[$currentCloser]['code'] === T_CLOSE_CURLY_BRACKET);
3✔
401

UNCOV
402
            return true;
×
403
        } elseif ($tokens[$lastToken]['code'] === T_SEMICOLON) {
3✔
404
            // We found the last statement of the CASE. Now we want to
405
            // check whether it is a terminating one.
406
            $terminator = $phpcsFile->findStartOfStatement(($lastToken - 1));
3✔
407
            if (isset(self::CASE_TERMINATING_TOKENS[$tokens[$terminator]['code']]) === true) {
3✔
408
                return $terminator;
3✔
409
            }
410
        }
411

412
        return false;
3✔
413
    }
414
}
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