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

PHPCSStandards / PHP_CodeSniffer / 20858887818

09 Jan 2026 04:44PM UTC coverage: 78.899% (-0.004%) from 78.903%
20858887818

Pull #1358

github

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

3 of 5 new or added lines in 1 file covered. (60.0%)

6 existing lines in 1 file now uncovered.

19828 of 25131 relevant lines covered (78.9%)

98.95 hits per line

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

95.0
/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
                    // Only raise error if colon is followed by a curly brace.
201
                    $nextCode = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), $nextCloser, true);
3✔
202
                    if ($tokens[$nextCode]['code'] === T_OPEN_CURLY_BRACKET) {
3✔
NEW
203
                        $error = sprintf('%s statements must not use a braced block after the colon', strtoupper($type));
×
NEW
204
                        $phpcsFile->addError($error, $nextCase, 'CaseWithBlockScope');
×
205
                    } else {
206
                        $phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type);
3✔
207
                    }
208
                }
209
            }
210

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

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

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

237

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

259
            break;
3✔
260
        }
261

262
        return $stackPtr;
3✔
263
    }
264

265

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

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

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

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

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

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

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

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

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

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

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

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

373
                        $opener = $tokens[$nextCase]['scope_opener'];
3✔
374

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

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

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

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

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

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

414
        return false;
3✔
415
    }
416
}
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