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

PHPCSStandards / PHP_CodeSniffer / 17174734458

23 Aug 2025 11:06AM UTC coverage: 76.88% (-2.1%) from 78.934%
17174734458

push

github

jrfnl
TEMP/TESTING PHPUnit 6331

19187 of 24957 relevant lines covered (76.88%)

60.25 hits per line

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

94.05
/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()
2✔
33
    {
34
        return [
2✔
35
            T_TRY,
2✔
36
            T_CATCH,
2✔
37
            T_FINALLY,
2✔
38
            T_DO,
2✔
39
            T_WHILE,
2✔
40
            T_FOR,
2✔
41
            T_IF,
2✔
42
            T_FOREACH,
2✔
43
            T_ELSE,
2✔
44
            T_ELSEIF,
2✔
45
            T_SWITCH,
2✔
46
            T_MATCH,
2✔
47
        ];
2✔
48

49
    }//end register()
50

51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

314
            $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseBrace', $data);
2✔
315
            if ($fix === true) {
2✔
316
                if ($found === 0) {
2✔
317
                    $phpcsFile->fixer->addContent($closer, ' ');
2✔
318
                } else {
319
                    $phpcsFile->fixer->replaceToken(($closer + 1), ' ');
2✔
320
                }
321
            }
322
        }
323

324
    }//end process()
325

326

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