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

PHPCSStandards / PHP_CodeSniffer / 7326289291

26 Dec 2023 03:26AM UTC coverage: 64.019% (+0.003%) from 64.016%
7326289291

push

github

jrfnl
Runner::processFile(): remove stray passed params

The `Reporter::cacheFileReport()` method only takes one parameter.

0 of 2 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

15127 of 23629 relevant lines covered (64.02%)

5.38 hits per line

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

98.88
/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

42
    }//end __construct()
1✔
43

44

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

63
    }//end register()
64

65

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

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

85
        $parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
3✔
86
        $parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
3✔
87

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

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

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

108
        // Check the indent of each line.
109
        $first          = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
3✔
110
        $requiredIndent = ($tokens[$first]['column'] + $this->indent - 1);
3✔
111
        for ($i = $parenOpener; $i < $parenCloser; $i++) {
3✔
112
            if ($tokens[$i]['column'] !== 1
3✔
113
                || $tokens[($i + 1)]['line'] > $tokens[$i]['line']
3✔
114
                || isset(Tokens::$commentTokens[$tokens[$i]['code']]) === true
3✔
115
            ) {
116
                continue;
3✔
117
            }
118

119
            if (($i + 1) === $parenCloser) {
3✔
120
                break;
3✔
121
            }
122

123
            // Leave indentation inside multi-line strings.
124
            if (isset(Tokens::$textStringTokens[$tokens[$i]['code']]) === true
3✔
125
                || isset(Tokens::$heredocTokens[$tokens[$i]['code']]) === true
3✔
126
            ) {
127
                continue;
3✔
128
            }
129

130
            if ($tokens[$i]['code'] !== T_WHITESPACE) {
3✔
131
                $foundIndent = 0;
3✔
132
            } else {
133
                $foundIndent = $tokens[$i]['length'];
3✔
134
            }
135

136
            if ($foundIndent < $requiredIndent) {
3✔
137
                $error = 'Each line in a multi-line control structure must be indented at least once; expected at least %s spaces, but found %s';
3✔
138
                $data  = [
2✔
139
                    $requiredIndent,
3✔
140
                    $foundIndent,
3✔
141
                ];
2✔
142
                $fix   = $phpcsFile->addFixableError($error, $i, 'LineIndent', $data);
3✔
143
                if ($fix === true) {
3✔
144
                    $padding = str_repeat(' ', $requiredIndent);
3✔
145
                    if ($foundIndent === 0) {
3✔
146
                        $phpcsFile->fixer->addContentBefore($i, $padding);
3✔
147
                    } else {
148
                        $phpcsFile->fixer->replaceToken($i, $padding);
3✔
149
                    }
150
                }
151
            }
152
        }//end for
153

154
        // Check the closing parenthesis.
155
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($parenCloser - 1), $parenOpener, true);
3✔
156
        if ($tokens[$parenCloser]['line'] !== ($tokens[$prev]['line'] + 1)) {
3✔
157
            $error = 'The closing parenthesis of a multi-line control structure must be on the line after the last expression';
3✔
158
            $fix   = $phpcsFile->addFixableError($error, $parenCloser, 'CloseParenthesisLine');
3✔
159
            if ($fix === true) {
3✔
160
                if ($tokens[$parenCloser]['line'] === $tokens[$prev]['line']) {
3✔
161
                    $phpcsFile->fixer->addNewlineBefore($parenCloser);
3✔
162
                } else {
163
                    $phpcsFile->fixer->beginChangeset();
3✔
164
                    for ($i = ($prev + 1); $i < $parenCloser; $i++) {
3✔
165
                        // Maintain existing newline.
166
                        if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
3✔
167
                            continue;
3✔
168
                        }
169

170
                        // Maintain existing indent.
171
                        if ($tokens[$i]['line'] === $tokens[$parenCloser]['line']) {
3✔
172
                            break;
3✔
173
                        }
174

175
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
176
                    }
177

178
                    $phpcsFile->fixer->endChangeset();
3✔
179
                }
180
            }//end if
181
        }//end if
182

183
        if ($tokens[$parenCloser]['line'] !== $tokens[$prev]['line']) {
3✔
184
            $requiredIndent = ($tokens[$first]['column'] - 1);
3✔
185
            $foundIndent    = ($tokens[$parenCloser]['column'] - 1);
3✔
186
            if ($foundIndent !== $requiredIndent) {
3✔
187
                $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✔
188
                $data  = [
2✔
189
                    $requiredIndent,
3✔
190
                    $foundIndent,
3✔
191
                ];
2✔
192
                $fix   = $phpcsFile->addFixableError($error, $parenCloser, 'CloseParenthesisIndent', $data);
3✔
193
                if ($fix === true) {
3✔
194
                    $padding = str_repeat(' ', $requiredIndent);
3✔
195
                    if ($foundIndent === 0) {
3✔
196
                        $phpcsFile->fixer->addContentBefore($parenCloser, $padding);
3✔
197
                    } else {
198
                        $phpcsFile->fixer->replaceToken(($parenCloser - 1), $padding);
3✔
199
                    }
200
                }
201
            }
202
        }
203

204
    }//end process()
1✔
205

206

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