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

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

94.67
/src/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php
1
<?php
2
/**
3
 * Verifies that control statements conform to their coding standards.
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\Squiz\Sniffs\ControlStructures;
11

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

16
class ControlSignatureSniff implements Sniff
17
{
18

19
    /**
20
     * How many spaces should precede the colon if using alternative syntax.
21
     *
22
     * @var integer
23
     */
24
    public $requiredSpacesBeforeColon = 1;
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_TRY,
3✔
36
            T_CATCH,
3✔
37
            T_FINALLY,
3✔
38
            T_DO,
3✔
39
            T_WHILE,
3✔
40
            T_FOR,
3✔
41
            T_IF,
3✔
42
            T_FOREACH,
3✔
43
            T_ELSE,
3✔
44
            T_ELSEIF,
3✔
45
            T_SWITCH,
3✔
46
            T_MATCH,
3✔
47
        ];
2✔
48
    }
49

50

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

64
        $nextNonEmpty = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
65
        if ($nextNonEmpty === false) {
3✔
66
            return;
3✔
67
        }
68

69
        $isAlternative = false;
3✔
70
        if (isset($tokens[$stackPtr]['scope_opener']) === true
3✔
71
            && $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON
3✔
72
        ) {
73
            $isAlternative = true;
3✔
74
        }
75

76
        // Single space after the keyword.
77
        $expected = 1;
3✔
78
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === false && $isAlternative === true) {
3✔
79
            // Catching cases like:
80
            // if (condition) : ... else: ... endif
81
            // where there is no condition.
82
            $expected = (int) $this->requiredSpacesBeforeColon;
3✔
83
        }
84

85
        $found = 1;
3✔
86
        if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
3✔
87
            $found = 0;
3✔
88
        } elseif ($tokens[($stackPtr + 1)]['content'] !== ' ') {
3✔
89
            if (strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) !== false) {
3✔
90
                $found = 'newline';
3✔
91
            } else {
92
                $found = $tokens[($stackPtr + 1)]['length'];
3✔
93
            }
94
        }
95

96
        if ($found !== $expected) {
3✔
97
            $pluralizeSpace = 's';
3✔
98
            if ($expected === 1) {
3✔
99
                $pluralizeSpace = '';
3✔
100
            }
101

102
            $error = 'Expected %s space%s after %s keyword; %s found';
3✔
103
            $data  = [
2✔
104
                $expected,
3✔
105
                $pluralizeSpace,
3✔
106
                strtoupper($tokens[$stackPtr]['content']),
3✔
107
                $found,
3✔
108
            ];
2✔
109

110
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword', $data);
3✔
111
            if ($fix === true) {
3✔
112
                if ($found === 0) {
3✔
113
                    $phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $expected));
3✔
114
                } else {
115
                    $phpcsFile->fixer->replaceToken(($stackPtr + 1), str_repeat(' ', $expected));
3✔
116
                }
117
            }
118
        }//end if
119

120
        // Single space after closing parenthesis.
121
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === true
3✔
122
            && isset($tokens[$stackPtr]['scope_opener']) === true
3✔
123
        ) {
124
            $expected = 1;
3✔
125
            if ($isAlternative === true) {
3✔
126
                $expected = (int) $this->requiredSpacesBeforeColon;
3✔
127
            }
128

129
            $closer  = $tokens[$stackPtr]['parenthesis_closer'];
3✔
130
            $opener  = $tokens[$stackPtr]['scope_opener'];
3✔
131
            $content = $phpcsFile->getTokensAsString(($closer + 1), ($opener - $closer - 1));
3✔
132

133
            if (trim($content) === '') {
3✔
134
                if (strpos($content, $phpcsFile->eolChar) !== false) {
3✔
135
                    $found = 'newline';
3✔
136
                } else {
137
                    $found = strlen($content);
3✔
138
                }
139
            } else {
140
                $found = '"' . str_replace($phpcsFile->eolChar, '\n', $content) . '"';
3✔
141
            }
142

143
            if ($found !== $expected) {
3✔
144
                $pluralizeSpace = 's';
3✔
145
                if ($expected === 1) {
3✔
146
                    $pluralizeSpace = '';
3✔
147
                }
148

149
                $error = 'Expected %s space%s after closing parenthesis; found %s';
3✔
150
                $data  = [
2✔
151
                    $expected,
3✔
152
                    $pluralizeSpace,
3✔
153
                    $found,
3✔
154
                ];
2✔
155

156
                $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseParenthesis', $data);
3✔
157
                if ($fix === true) {
3✔
158
                    $padding = str_repeat(' ', $expected);
3✔
159
                    if ($closer === ($opener - 1)) {
3✔
160
                        $phpcsFile->fixer->addContent($closer, $padding);
3✔
161
                    } else {
162
                        $phpcsFile->fixer->beginChangeset();
3✔
163
                        if (trim($content) === '') {
3✔
164
                            $phpcsFile->fixer->addContent($closer, $padding);
3✔
165
                            if ($found !== 0) {
3✔
166
                                for ($i = ($closer + 1); $i < $opener; $i++) {
3✔
167
                                    $phpcsFile->fixer->replaceToken($i, '');
3✔
168
                                }
169
                            }
170
                        } else {
171
                            $phpcsFile->fixer->addContent($closer, $padding . $tokens[$opener]['content']);
3✔
172
                            $phpcsFile->fixer->replaceToken($opener, '');
3✔
173

174
                            if ($tokens[$opener]['line'] !== $tokens[$closer]['line']) {
3✔
175
                                $next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true);
3✔
176
                                if ($tokens[$next]['line'] !== $tokens[$opener]['line']) {
3✔
177
                                    for ($i = ($opener + 1); $i < $next; $i++) {
3✔
178
                                        $phpcsFile->fixer->replaceToken($i, '');
3✔
179
                                    }
180
                                }
181
                            }
182
                        }
183

184
                        $phpcsFile->fixer->endChangeset();
3✔
185
                    }//end if
186
                }//end if
187
            }//end if
188
        }//end if
189

190
        // Single newline after opening brace.
191
        if (isset($tokens[$stackPtr]['scope_opener']) === true) {
3✔
192
            $opener = $tokens[$stackPtr]['scope_opener'];
3✔
193
            for ($next = ($opener + 1); $next < $phpcsFile->numTokens; $next++) {
3✔
194
                $code = $tokens[$next]['code'];
3✔
195

196
                if ($code === T_WHITESPACE
3✔
197
                    || ($code === T_INLINE_HTML
3✔
198
                    && trim($tokens[$next]['content']) === '')
3✔
199
                ) {
200
                    continue;
3✔
201
                }
202

203
                // Skip all empty tokens on the same line as the opener.
204
                if ($tokens[$next]['line'] === $tokens[$opener]['line']
3✔
205
                    && (isset(Tokens::EMPTY_TOKENS[$code]) === true
3✔
206
                    || $code === T_CLOSE_TAG)
3✔
207
                ) {
208
                    continue;
3✔
209
                }
210

211
                // We found the first bit of a code, or a comment on the
212
                // following line.
213
                break;
3✔
214
            }//end for
215

216
            if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
3✔
217
                $error = 'Newline required after opening brace';
3✔
218
                $fix   = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace');
3✔
219
                if ($fix === true) {
3✔
220
                    $phpcsFile->fixer->beginChangeset();
3✔
221
                    for ($i = ($opener + 1); $i < $next; $i++) {
3✔
222
                        if (trim($tokens[$i]['content']) !== '') {
3✔
223
                            break;
3✔
224
                        }
225

226
                        // Remove whitespace.
227
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
228
                    }
229

230
                    $phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar);
3✔
231
                    $phpcsFile->fixer->endChangeset();
3✔
232
                }
233
            }//end if
234
        } elseif ($tokens[$stackPtr]['code'] === T_WHILE) {
3✔
235
            // Zero spaces after parenthesis closer, but only if followed by a semicolon.
236
            $closer       = $tokens[$stackPtr]['parenthesis_closer'];
3✔
237
            $nextNonEmpty = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($closer + 1), null, true);
3✔
238
            if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_SEMICOLON) {
3✔
239
                $found = 0;
3✔
240
                if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
3✔
UNCOV
241
                    if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) {
×
242
                        $found = 'newline';
×
243
                    } else {
UNCOV
244
                        $found = $tokens[($closer + 1)]['length'];
×
245
                    }
246
                }
247

248
                if ($found !== 0) {
3✔
UNCOV
249
                    $error = 'Expected 0 spaces before semicolon; %s found';
×
250
                    $data  = [$found];
×
251
                    $fix   = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data);
×
252
                    if ($fix === true) {
×
253
                        $phpcsFile->fixer->replaceToken(($closer + 1), '');
×
254
                    }
255
                }
256
            }
257
        }//end if
258

259
        // Only want to check multi-keyword structures from here on.
260
        if ($tokens[$stackPtr]['code'] === T_WHILE) {
3✔
261
            if (isset($tokens[$stackPtr]['scope_closer']) !== false) {
3✔
262
                return;
3✔
263
            }
264

265
            $closer = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
3✔
266
            if ($closer === false
3✔
267
                || $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET
3✔
268
                || $tokens[$tokens[$closer]['scope_condition']]['code'] !== T_DO
3✔
269
            ) {
270
                return;
3✔
271
            }
272
        } elseif ($tokens[$stackPtr]['code'] === T_ELSE
3✔
273
            || $tokens[$stackPtr]['code'] === T_ELSEIF
3✔
274
            || $tokens[$stackPtr]['code'] === T_CATCH
3✔
275
            || $tokens[$stackPtr]['code'] === T_FINALLY
3✔
276
        ) {
277
            if (isset($tokens[$stackPtr]['scope_opener']) === true
3✔
278
                && $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON
3✔
279
            ) {
280
                // Special case for alternate syntax, where this token is actually
281
                // the closer for the previous block, so there is no spacing to check.
282
                return;
3✔
283
            }
284

285
            $closer = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
3✔
286
            if ($closer === false || $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET) {
3✔
287
                return;
1✔
288
            }
289
        } else {
290
            return;
3✔
291
        }//end if
292

293
        // Single space after closing brace.
294
        $found = 1;
3✔
295
        if ($tokens[($closer + 1)]['code'] !== T_WHITESPACE) {
3✔
296
            $found = 0;
3✔
297
        } elseif ($tokens[$closer]['line'] !== $tokens[$stackPtr]['line']) {
3✔
298
            $found = 'newline';
3✔
299
        } elseif ($tokens[($closer + 1)]['content'] !== ' ') {
3✔
UNCOV
300
            $found = $tokens[($closer + 1)]['length'];
×
301
        }
302

303
        if ($found !== 1) {
3✔
304
            $error = 'Expected 1 space after closing brace; %s found';
3✔
305
            $data  = [$found];
3✔
306

307
            if ($phpcsFile->findNext(Tokens::COMMENT_TOKENS, ($closer + 1), $stackPtr) !== false) {
3✔
308
                // Comment found between closing brace and keyword, don't auto-fix.
309
                $phpcsFile->addError($error, $closer, 'SpaceAfterCloseBrace', $data);
3✔
310
                return;
3✔
311
            }
312

313
            $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseBrace', $data);
3✔
314
            if ($fix === true) {
3✔
315
                if ($found === 0) {
3✔
316
                    $phpcsFile->fixer->addContent($closer, ' ');
3✔
317
                } else {
318
                    $phpcsFile->fixer->replaceToken(($closer + 1), ' ');
3✔
319
                }
320
            }
321
        }
322
    }
1✔
323
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc