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

PHPCSStandards / PHP_CodeSniffer / 20855665414

09 Jan 2026 02:49PM UTC coverage: 78.904% (+0.001%) from 78.903%
20855665414

Pull #1358

github

web-flow
Merge 78af3f544 into cb3ecb950
Pull Request #1358: PSR2 WrongOpenercase with colon and bracket is unclear

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

6 existing lines in 1 file now uncovered.

19827 of 25128 relevant lines covered (78.9%)

98.96 hits per line

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

96.05
/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
                    // Probably a case/default statement with colon + curly braces.
201
                    $error = sprintf('%s statements must not use a braced block after the colon', strtoupper($type));
3✔
202
                    $phpcsFile->addError($error, $nextCase, 'CaseWithBlockScope');
3✔
203
                }
204
            }
205

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

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

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

232

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

254
            break;
3✔
255
        }
256

257
        return $stackPtr;
3✔
258
    }
259

260

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

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

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

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

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

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

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

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

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

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

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

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

368
                        $opener = $tokens[$nextCase]['scope_opener'];
3✔
369

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

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

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

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

393
                    return $hasTerminator;
3✔
394
                } else {
UNCOV
395
                    return false;
×
396
                }
397
            } while ($currentCloser !== false && $tokens[$currentCloser]['code'] === T_CLOSE_CURLY_BRACKET);
3✔
398

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

409
        return false;
3✔
410
    }
411
}
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