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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 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
    }//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)
3✔
62
    {
63
        $tokens = $phpcsFile->getTokens();
3✔
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

185
                        $phpcsFile->fixer->endChangeset();
3✔
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) {
3✔
193
            $opener = $tokens[$stackPtr]['scope_opener'];
3✔
194
            for ($next = ($opener + 1); $next < $phpcsFile->numTokens; $next++) {
3✔
195
                $code = $tokens[$next]['code'];
3✔
196

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

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

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

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

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

231
                    $phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar);
3✔
232
                    $phpcsFile->fixer->endChangeset();
3✔
233
                }
234
            }//end if
235
        } else if ($tokens[$stackPtr]['code'] === T_WHILE) {
3✔
236
            // Zero spaces after parenthesis closer, but only if followed by a semicolon.
237
            $closer       = $tokens[$stackPtr]['parenthesis_closer'];
3✔
238
            $nextNonEmpty = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($closer + 1), null, true);
3✔
239
            if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_SEMICOLON) {
3✔
240
                $found = 0;
3✔
241
                if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) {
3✔
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) {
3✔
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) {
3✔
262
            if (isset($tokens[$stackPtr]['scope_closer']) !== false) {
3✔
263
                return;
3✔
264
            }
265

266
            $closer = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
3✔
267
            if ($closer === false
3✔
268
                || $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET
3✔
269
                || $tokens[$tokens[$closer]['scope_condition']]['code'] !== T_DO
3✔
270
            ) {
271
                return;
3✔
272
            }
273
        } else if ($tokens[$stackPtr]['code'] === T_ELSE
3✔
274
            || $tokens[$stackPtr]['code'] === T_ELSEIF
3✔
275
            || $tokens[$stackPtr]['code'] === T_CATCH
3✔
276
            || $tokens[$stackPtr]['code'] === T_FINALLY
3✔
277
        ) {
278
            if (isset($tokens[$stackPtr]['scope_opener']) === true
3✔
279
                && $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON
3✔
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;
3✔
284
            }
285

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

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

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

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

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

324
    }//end process()
1✔
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

© 2025 Coveralls, Inc