• 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.12
/src/Standards/Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php
1
<?php
2
/**
3
 * Checks alignment of assignments.
4
 *
5
 * If there are multiple adjacent assignments, it will check that the equals signs of
6
 * each assignment are aligned. It will display a warning to advise that the signs should be aligned.
7
 *
8
 * @author    Greg Sherwood <gsherwood@squiz.net>
9
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
10
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
11
 */
12

13
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting;
14

15
use PHP_CodeSniffer\Files\File;
16
use PHP_CodeSniffer\Sniffs\Sniff;
17
use PHP_CodeSniffer\Util\Tokens;
18

19
class MultipleStatementAlignmentSniff implements Sniff
20
{
21

22
    /**
23
     * The maximum amount of padding before the alignment is ignored.
24
     *
25
     * If the amount of padding required to align this assignment with the
26
     * surrounding assignments exceeds this number, the assignment will be
27
     * ignored and no errors or warnings will be thrown.
28
     *
29
     * @var integer
30
     */
31
    public $maxPadding = 1000;
32

33
    /**
34
     * Controls which side of the assignment token is used for alignment.
35
     *
36
     * @var boolean
37
     */
38
    public $alignAtEnd = true;
39

40

41
    /**
42
     * Returns an array of tokens this test wants to listen for.
43
     *
44
     * @return array<int|string>
45
     */
46
    public function register()
3✔
47
    {
48
        $tokens = Tokens::ASSIGNMENT_TOKENS;
3✔
49
        unset($tokens[T_DOUBLE_ARROW]);
3✔
50
        return $tokens;
3✔
51

52
    }//end register()
53

54

55
    /**
56
     * Processes this test, when one of its tokens is encountered.
57
     *
58
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
59
     * @param int                         $stackPtr  The position of the current token
60
     *                                               in the stack passed in $tokens.
61
     *
62
     * @return int
63
     */
64
    public function process(File $phpcsFile, $stackPtr)
3✔
65
    {
66
        $lastAssign = $this->checkAlignment($phpcsFile, $stackPtr);
3✔
67
        return ($lastAssign + 1);
3✔
68

69
    }//end process()
70

71

72
    /**
73
     * Processes this test, when one of its tokens is encountered.
74
     *
75
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
76
     * @param int                         $stackPtr  The position of the current token
77
     *                                               in the stack passed in $tokens.
78
     * @param int                         $end       The token where checking should end.
79
     *                                               If NULL, the entire file will be checked.
80
     *
81
     * @return int
82
     */
83
    public function checkAlignment($phpcsFile, $stackPtr, $end=null)
3✔
84
    {
85
        $tokens = $phpcsFile->getTokens();
3✔
86

87
        // Ignore assignments used in a condition, like an IF or FOR or closure param defaults.
88
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
3✔
89
            // If the parenthesis is on the same line as the assignment,
90
            // then it should be ignored as it is specifically being grouped.
91
            $parens    = $tokens[$stackPtr]['nested_parenthesis'];
3✔
92
            $lastParen = array_pop($parens);
3✔
93
            if ($tokens[$lastParen]['line'] === $tokens[$stackPtr]['line']) {
3✔
94
                return $stackPtr;
3✔
95
            }
96

97
            foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
3✔
98
                if (isset($tokens[$start]['parenthesis_owner']) === true) {
3✔
99
                    return $stackPtr;
×
100
                }
101
            }
102
        }
103

104
        $assignments = [];
3✔
105
        $prevAssign  = null;
3✔
106
        $lastLine    = $tokens[$stackPtr]['line'];
3✔
107
        $maxPadding  = null;
3✔
108
        $stopped     = null;
3✔
109
        $lastCode    = $stackPtr;
3✔
110
        $lastSemi    = null;
3✔
111
        $arrayEnd    = null;
3✔
112

113
        if ($end === null) {
3✔
114
            $end = $phpcsFile->numTokens;
3✔
115
        }
116

117
        $find = Tokens::ASSIGNMENT_TOKENS;
3✔
118
        unset($find[T_DOUBLE_ARROW]);
3✔
119

120
        $scopes = Tokens::SCOPE_OPENERS;
3✔
121
        unset($scopes[T_CLOSURE]);
3✔
122
        unset($scopes[T_ANON_CLASS]);
3✔
123

124
        for ($assign = $stackPtr; $assign < $end; $assign++) {
3✔
125
            if ($tokens[$assign]['level'] < $tokens[$stackPtr]['level']) {
3✔
126
                // Statement is in a different context, so the block is over.
127
                break;
3✔
128
            }
129

130
            if (isset($tokens[$assign]['scope_opener']) === true
3✔
131
                && $tokens[$assign]['level'] === $tokens[$stackPtr]['level']
3✔
132
            ) {
133
                if (isset($scopes[$tokens[$assign]['code']]) === true) {
3✔
134
                    // This type of scope indicates that the assignment block is over.
135
                    break;
3✔
136
                }
137

138
                // Skip over the scope block because it is seen as part of the assignment block,
139
                // but also process any assignment blocks that are inside as well.
140
                $nextAssign = $phpcsFile->findNext($find, ($assign + 1), ($tokens[$assign]['scope_closer'] - 1));
3✔
141
                if ($nextAssign !== false) {
3✔
142
                    $assign = $this->checkAlignment($phpcsFile, $nextAssign);
3✔
143
                } else {
144
                    $assign = $tokens[$assign]['scope_closer'];
3✔
145
                }
146

147
                $lastCode = $assign;
3✔
148
                continue;
3✔
149
            }
150

151
            if ($assign === $arrayEnd) {
3✔
152
                $arrayEnd = null;
3✔
153
            }
154

155
            if (isset($find[$tokens[$assign]['code']]) === false) {
3✔
156
                // A blank line indicates that the assignment block has ended.
157
                if (isset(Tokens::EMPTY_TOKENS[$tokens[$assign]['code']]) === false
3✔
158
                    && ($tokens[$assign]['line'] - $tokens[$lastCode]['line']) > 1
3✔
159
                    && $tokens[$assign]['level'] === $tokens[$stackPtr]['level']
3✔
160
                    && $arrayEnd === null
3✔
161
                ) {
162
                    break;
3✔
163
                }
164

165
                if ($tokens[$assign]['code'] === T_CLOSE_TAG) {
3✔
166
                    // Breaking out of PHP ends the assignment block.
167
                    break;
3✔
168
                }
169

170
                if ($tokens[$assign]['code'] === T_OPEN_SHORT_ARRAY
3✔
171
                    && isset($tokens[$assign]['bracket_closer']) === true
3✔
172
                ) {
173
                    $arrayEnd = $tokens[$assign]['bracket_closer'];
3✔
174
                }
175

176
                if ($tokens[$assign]['code'] === T_ARRAY
3✔
177
                    && isset($tokens[$assign]['parenthesis_opener']) === true
3✔
178
                    && isset($tokens[$tokens[$assign]['parenthesis_opener']]['parenthesis_closer']) === true
3✔
179
                ) {
180
                    $arrayEnd = $tokens[$tokens[$assign]['parenthesis_opener']]['parenthesis_closer'];
3✔
181
                }
182

183
                if (isset(Tokens::EMPTY_TOKENS[$tokens[$assign]['code']]) === false) {
3✔
184
                    $lastCode = $assign;
3✔
185

186
                    if ($tokens[$assign]['code'] === T_SEMICOLON) {
3✔
187
                        if ($tokens[$assign]['conditions'] === $tokens[$stackPtr]['conditions']) {
3✔
188
                            if ($lastSemi !== null && $prevAssign !== null && $lastSemi > $prevAssign) {
3✔
189
                                // This statement did not have an assignment operator in it.
190
                                break;
3✔
191
                            } else {
192
                                $lastSemi = $assign;
3✔
193
                            }
194
                        } else if ($tokens[$assign]['level'] < $tokens[$stackPtr]['level']) {
3✔
195
                            // Statement is in a different context, so the block is over.
196
                            break;
×
197
                        }
198
                    }
199
                }//end if
200

201
                continue;
3✔
202
            } else if ($assign !== $stackPtr && $tokens[$assign]['line'] === $lastLine) {
3✔
203
                // Skip multiple assignments on the same line. We only need to
204
                // try and align the first assignment.
205
                continue;
3✔
206
            }//end if
207

208
            if ($assign !== $stackPtr) {
3✔
209
                if ($tokens[$assign]['level'] > $tokens[$stackPtr]['level']) {
3✔
210
                    // Has to be nested inside the same conditions as the first assignment.
211
                    // We've gone one level down, so process this new block.
212
                    $assign   = $this->checkAlignment($phpcsFile, $assign);
3✔
213
                    $lastCode = $assign;
3✔
214
                    continue;
3✔
215
                } else if ($tokens[$assign]['level'] < $tokens[$stackPtr]['level']) {
3✔
216
                    // We've gone one level up, so the block we are processing is done.
217
                    break;
×
218
                } else if ($arrayEnd !== null) {
3✔
219
                    // Assignments inside arrays are not part of
220
                    // the original block, so process this new block.
221
                    $assign   = ($this->checkAlignment($phpcsFile, $assign, $arrayEnd) - 1);
3✔
222
                    $arrayEnd = null;
3✔
223
                    $lastCode = $assign;
3✔
224
                    continue;
3✔
225
                }
226

227
                // Make sure it is not assigned inside a condition (eg. IF, FOR).
228
                if (isset($tokens[$assign]['nested_parenthesis']) === true) {
3✔
229
                    // If the parenthesis is on the same line as the assignment,
230
                    // then it should be ignored as it is specifically being grouped.
231
                    $parens    = $tokens[$assign]['nested_parenthesis'];
3✔
232
                    $lastParen = array_pop($parens);
3✔
233
                    if ($tokens[$lastParen]['line'] === $tokens[$assign]['line']) {
3✔
234
                        break;
3✔
235
                    }
236

237
                    foreach ($tokens[$assign]['nested_parenthesis'] as $start => $end) {
3✔
238
                        if (isset($tokens[$start]['parenthesis_owner']) === true) {
3✔
239
                            break(2);
×
240
                        }
241
                    }
242
                }
243
            }//end if
244

245
            $var = $phpcsFile->findPrevious(
3✔
246
                Tokens::EMPTY_TOKENS,
3✔
247
                ($assign - 1),
3✔
248
                null,
3✔
249
                true
3✔
250
            );
2✔
251

252
            // Make sure we wouldn't break our max padding length if we
253
            // aligned with this statement, or they wouldn't break the max
254
            // padding length if they aligned with us.
255
            $varEnd    = $tokens[($var + 1)]['column'];
3✔
256
            $assignLen = $tokens[$assign]['length'];
3✔
257
            if ($this->alignAtEnd !== true) {
3✔
258
                $assignLen = 1;
3✔
259
            }
260

261
            if ($assign !== $stackPtr) {
3✔
262
                if ($prevAssign === null) {
3✔
263
                    // Processing an inner block but no assignments found.
264
                    break;
×
265
                }
266

267
                if (($varEnd + 1) > $assignments[$prevAssign]['assign_col']) {
3✔
268
                    $padding      = 1;
3✔
269
                    $assignColumn = ($varEnd + 1);
3✔
270
                } else {
271
                    $padding = ($assignments[$prevAssign]['assign_col'] - $varEnd + $assignments[$prevAssign]['assign_len'] - $assignLen);
3✔
272
                    if ($padding <= 0) {
3✔
273
                        $padding = 1;
3✔
274
                    }
275

276
                    if ($padding > $this->maxPadding) {
3✔
277
                        $stopped = $assign;
×
278
                        break;
×
279
                    }
280

281
                    $assignColumn = ($varEnd + $padding);
3✔
282
                }//end if
283

284
                if (($assignColumn + $assignLen) > ($assignments[$maxPadding]['assign_col'] + $assignments[$maxPadding]['assign_len'])) {
3✔
285
                    $newPadding = ($varEnd - $assignments[$maxPadding]['var_end'] + $assignLen - $assignments[$maxPadding]['assign_len'] + 1);
3✔
286
                    if ($newPadding > $this->maxPadding) {
3✔
287
                        $stopped = $assign;
3✔
288
                        break;
3✔
289
                    } else {
290
                        // New alignment settings for previous assignments.
291
                        foreach ($assignments as $i => $data) {
3✔
292
                            if ($i === $assign) {
3✔
293
                                break;
×
294
                            }
295

296
                            $newPadding = ($varEnd - $data['var_end'] + $assignLen - $data['assign_len'] + 1);
3✔
297
                            $assignments[$i]['expected']   = $newPadding;
3✔
298
                            $assignments[$i]['assign_col'] = ($data['var_end'] + $newPadding);
3✔
299
                        }
300

301
                        $padding      = 1;
3✔
302
                        $assignColumn = ($varEnd + 1);
3✔
303
                    }
304
                } else if ($padding > $assignments[$maxPadding]['expected']) {
3✔
305
                    $maxPadding = $assign;
3✔
306
                }//end if
307
            } else {
308
                $padding      = 1;
3✔
309
                $assignColumn = ($varEnd + 1);
3✔
310
                $maxPadding   = $assign;
3✔
311
            }//end if
312

313
            $found = 0;
3✔
314
            if ($tokens[($var + 1)]['code'] === T_WHITESPACE) {
3✔
315
                $found = $tokens[($var + 1)]['length'];
3✔
316
                if ($found === 0) {
3✔
317
                    // This means a newline was found.
318
                    $found = 1;
×
319
                }
320
            }
321

322
            $assignments[$assign] = [
3✔
323
                'var_end'    => $varEnd,
3✔
324
                'assign_len' => $assignLen,
3✔
325
                'assign_col' => $assignColumn,
3✔
326
                'expected'   => $padding,
3✔
327
                'found'      => $found,
3✔
328
            ];
2✔
329

330
            $lastLine   = $tokens[$assign]['line'];
3✔
331
            $prevAssign = $assign;
3✔
332
        }//end for
333

334
        if (empty($assignments) === true) {
3✔
335
            return $stackPtr;
×
336
        }
337

338
        $numAssignments = count($assignments);
3✔
339

340
        $errorGenerated = false;
3✔
341
        foreach ($assignments as $assignment => $data) {
3✔
342
            if ($data['found'] === $data['expected']) {
3✔
343
                continue;
3✔
344
            }
345

346
            $expectedText = $data['expected'].' space';
3✔
347
            if ($data['expected'] !== 1) {
3✔
348
                $expectedText .= 's';
3✔
349
            }
350

351
            if ($data['found'] === null) {
3✔
352
                $foundText = 'a new line';
×
353
            } else {
354
                $foundText = $data['found'].' space';
3✔
355
                if ($data['found'] !== 1) {
3✔
356
                    $foundText .= 's';
3✔
357
                }
358
            }
359

360
            if ($numAssignments === 1) {
3✔
361
                $type  = 'Incorrect';
3✔
362
                $error = 'Equals sign not aligned correctly; expected %s but found %s';
3✔
363
            } else {
364
                $type  = 'NotSame';
3✔
365
                $error = 'Equals sign not aligned with surrounding assignments; expected %s but found %s';
3✔
366
            }
367

368
            $errorData = [
2✔
369
                $expectedText,
3✔
370
                $foundText,
3✔
371
            ];
2✔
372

373
            $fix = $phpcsFile->addFixableWarning($error, $assignment, $type, $errorData);
3✔
374

375
            $errorGenerated = true;
3✔
376

377
            if ($fix === true && $data['found'] !== null) {
3✔
378
                $newContent = str_repeat(' ', $data['expected']);
3✔
379
                if ($data['found'] === 0) {
3✔
380
                    $phpcsFile->fixer->addContentBefore($assignment, $newContent);
3✔
381
                } else {
382
                    $phpcsFile->fixer->replaceToken(($assignment - 1), $newContent);
3✔
383
                }
384
            }
385
        }//end foreach
386

387
        if ($numAssignments > 1) {
3✔
388
            if ($errorGenerated === true) {
3✔
389
                $phpcsFile->recordMetric($stackPtr, 'Adjacent assignments aligned', 'no');
3✔
390
            } else {
391
                $phpcsFile->recordMetric($stackPtr, 'Adjacent assignments aligned', 'yes');
3✔
392
            }
393
        }
394

395
        if ($stopped !== null) {
3✔
396
            return $this->checkAlignment($phpcsFile, $stopped);
3✔
397
        } else {
398
            return $assign;
3✔
399
        }
400

401
    }//end checkAlignment()
402

403

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