• 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

98.97
/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php
1
<?php
2
/**
3
 * Checks that control structures have the correct spacing.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2019 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\PSR12\Sniffs\ControlStructures;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14
use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff as PSR2ControlStructureSpacing;
15
use PHP_CodeSniffer\Util\Tokens;
16

17
class ControlStructureSpacingSniff implements Sniff
18
{
19

20
    /**
21
     * The number of spaces code should be indented.
22
     *
23
     * @var integer
24
     */
25
    public $indent = 4;
26

27
    /**
28
     * Instance of the PSR2 ControlStructureSpacingSniff sniff.
29
     *
30
     * @var \PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff
31
     */
32
    private $psr2ControlStructureSpacing;
33

34

35
    /**
36
     * Constructor.
37
     */
38
    public function __construct()
3✔
39
    {
40
        $this->psr2ControlStructureSpacing = new PSR2ControlStructureSpacing();
3✔
41
    }
1✔
42

43

44
    /**
45
     * Returns an array of tokens this test wants to listen for.
46
     *
47
     * @return array<int|string>
48
     */
49
    public function register()
3✔
50
    {
51
        return [
2✔
52
            T_IF,
3✔
53
            T_WHILE,
3✔
54
            T_FOREACH,
3✔
55
            T_FOR,
3✔
56
            T_SWITCH,
3✔
57
            T_ELSEIF,
3✔
58
            T_CATCH,
3✔
59
            T_MATCH,
3✔
60
        ];
2✔
61
    }
62

63

64
    /**
65
     * Processes this test, when one of its tokens is encountered.
66
     *
67
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
68
     * @param int                         $stackPtr  The position of the current token
69
     *                                               in the stack passed in $tokens.
70
     *
71
     * @return void
72
     */
73
    public function process(File $phpcsFile, int $stackPtr)
3✔
74
    {
75
        $tokens = $phpcsFile->getTokens();
3✔
76

77
        if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
3✔
78
            || isset($tokens[$stackPtr]['parenthesis_closer']) === false
3✔
79
        ) {
UNCOV
80
            return;
×
81
        }
82

83
        $parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
3✔
84
        $parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
3✔
85

86
        if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) {
3✔
87
            // Conditions are all on the same line, so follow PSR2.
88
            $this->psr2ControlStructureSpacing->process($phpcsFile, $stackPtr);
3✔
89
            return;
3✔
90
        }
91

92
        $next = $phpcsFile->findNext(T_WHITESPACE, ($parenOpener + 1), $parenCloser, true);
3✔
93
        if ($next === false) {
3✔
94
            // No conditions; parse error.
95
            return;
3✔
96
        }
97

98
        // Check the first expression.
99
        if ($tokens[$next]['line'] !== ($tokens[$parenOpener]['line'] + 1)) {
3✔
100
            $error = 'The first expression of a multi-line control structure must be on the line after the opening parenthesis';
3✔
101
            $fix   = $phpcsFile->addFixableError($error, $next, 'FirstExpressionLine');
3✔
102
            if ($fix === true) {
3✔
103
                $phpcsFile->fixer->beginChangeset();
3✔
104
                if ($tokens[$next]['line'] > ($tokens[$parenOpener]['line'] + 1)) {
3✔
105
                    for ($i = ($parenOpener + 1); $i < $next; $i++) {
3✔
106
                        if ($tokens[$next]['line'] === $tokens[$i]['line']) {
3✔
107
                            break;
3✔
108
                        }
109

110
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
111
                    }
112
                }
113

114
                $phpcsFile->fixer->addNewline($parenOpener);
3✔
115
                $phpcsFile->fixer->endChangeset();
3✔
116
            }
117
        }
118

119
        // Check the indent of each line.
120
        $first          = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
3✔
121
        $requiredIndent = ($tokens[$first]['column'] + $this->indent - 1);
3✔
122
        for ($i = $parenOpener; $i < $parenCloser; $i++) {
3✔
123
            if ($tokens[$i]['column'] !== 1
3✔
124
                || $tokens[($i + 1)]['line'] > $tokens[$i]['line']
3✔
125
                || isset(Tokens::COMMENT_TOKENS[$tokens[$i]['code']]) === true
3✔
126
            ) {
127
                continue;
3✔
128
            }
129

130
            if (($i + 1) === $parenCloser) {
3✔
131
                break;
3✔
132
            }
133

134
            // Leave indentation inside multi-line strings.
135
            if (isset(Tokens::TEXT_STRING_TOKENS[$tokens[$i]['code']]) === true
3✔
136
                || isset(Tokens::HEREDOC_TOKENS[$tokens[$i]['code']]) === true
3✔
137
            ) {
138
                continue;
3✔
139
            }
140

141
            if ($tokens[$i]['code'] !== T_WHITESPACE) {
3✔
142
                $foundIndent = 0;
3✔
143
            } else {
144
                $foundIndent = $tokens[$i]['length'];
3✔
145
            }
146

147
            if ($foundIndent < $requiredIndent) {
3✔
148
                $error = 'Each line in a multi-line control structure must be indented at least once; expected at least %s spaces, but found %s';
3✔
149
                $data  = [
2✔
150
                    $requiredIndent,
3✔
151
                    $foundIndent,
3✔
152
                ];
2✔
153
                $fix   = $phpcsFile->addFixableError($error, $i, 'LineIndent', $data);
3✔
154
                if ($fix === true) {
3✔
155
                    $padding = str_repeat(' ', $requiredIndent);
3✔
156
                    if ($foundIndent === 0) {
3✔
157
                        $phpcsFile->fixer->addContentBefore($i, $padding);
3✔
158
                    } else {
159
                        $phpcsFile->fixer->replaceToken($i, $padding);
3✔
160
                    }
161
                }
162
            }
163
        }//end for
164

165
        // Check the closing parenthesis.
166
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($parenCloser - 1), $parenOpener, true);
3✔
167
        if ($tokens[$parenCloser]['line'] !== ($tokens[$prev]['line'] + 1)) {
3✔
168
            $error = 'The closing parenthesis of a multi-line control structure must be on the line after the last expression';
3✔
169
            $fix   = $phpcsFile->addFixableError($error, $parenCloser, 'CloseParenthesisLine');
3✔
170
            if ($fix === true) {
3✔
171
                if ($tokens[$parenCloser]['line'] === $tokens[$prev]['line']) {
3✔
172
                    $phpcsFile->fixer->addNewlineBefore($parenCloser);
3✔
173
                } else {
174
                    $phpcsFile->fixer->beginChangeset();
3✔
175
                    for ($i = ($prev + 1); $i < $parenCloser; $i++) {
3✔
176
                        // Maintain existing newline.
177
                        if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
3✔
178
                            continue;
3✔
179
                        }
180

181
                        // Maintain existing indent.
182
                        if ($tokens[$i]['line'] === $tokens[$parenCloser]['line']) {
3✔
183
                            break;
3✔
184
                        }
185

186
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
187
                    }
188

189
                    $phpcsFile->fixer->endChangeset();
3✔
190
                }
191
            }//end if
192
        }//end if
193

194
        if ($tokens[$parenCloser]['line'] !== $tokens[$prev]['line']) {
3✔
195
            $requiredIndent = ($tokens[$first]['column'] - 1);
3✔
196
            $foundIndent    = ($tokens[$parenCloser]['column'] - 1);
3✔
197
            if ($foundIndent !== $requiredIndent) {
3✔
198
                $error = 'The closing parenthesis of a multi-line control structure must be indented to the same level as start of the control structure; expected %s spaces but found %s';
3✔
199
                $data  = [
2✔
200
                    $requiredIndent,
3✔
201
                    $foundIndent,
3✔
202
                ];
2✔
203
                $fix   = $phpcsFile->addFixableError($error, $parenCloser, 'CloseParenthesisIndent', $data);
3✔
204
                if ($fix === true) {
3✔
205
                    $padding = str_repeat(' ', $requiredIndent);
3✔
206
                    if ($foundIndent === 0) {
3✔
207
                        $phpcsFile->fixer->addContentBefore($parenCloser, $padding);
3✔
208
                    } else {
209
                        $phpcsFile->fixer->replaceToken(($parenCloser - 1), $padding);
3✔
210
                    }
211
                }
212
            }
213
        }
214
    }
1✔
215
}
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