• 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

91.43
/src/Standards/PEAR/Sniffs/Formatting/MultiLineAssignmentSniff.php
1
<?php
2
/**
3
 * If an assignment goes over two lines, ensure the equal sign is indented.
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\PEAR\Sniffs\Formatting;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14

15
class MultiLineAssignmentSniff implements Sniff
16
{
17

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

25

26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array<int|string>
30
     */
31
    public function register()
3✔
32
    {
33
        return [T_EQUAL];
3✔
34
    }
35

36

37
    /**
38
     * Processes this test, when one of its tokens is encountered.
39
     *
40
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
41
     * @param int                         $stackPtr  The position of the current token
42
     *                                               in the stack passed in $tokens.
43
     *
44
     * @return void
45
     */
46
    public function process(File $phpcsFile, int $stackPtr)
3✔
47
    {
48
        $tokens = $phpcsFile->getTokens();
3✔
49

50
        // Equal sign can't be the last thing on the line.
51
        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
3✔
52
        if ($next === false) {
3✔
53
            // Bad assignment.
UNCOV
54
            return;
×
55
        }
56

57
        if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
3✔
58
            $error = 'Multi-line assignments must have the equal sign on the second line';
3✔
59
            $phpcsFile->addError($error, $stackPtr, 'EqualSignLine');
3✔
60
            return;
3✔
61
        }
62

63
        // Make sure it is the first thing on the line, otherwise we ignore it.
64
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), false, true);
3✔
65
        if ($prev === false) {
3✔
66
            // Bad assignment.
UNCOV
67
            return;
×
68
        }
69

70
        if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
3✔
71
            return;
3✔
72
        }
73

74
        // Find the required indent based on the ident of the previous line.
75
        $assignmentIndent = 0;
3✔
76
        $prevLine         = $tokens[$prev]['line'];
3✔
77
        for ($i = ($prev - 1); $i >= 0; $i--) {
3✔
78
            if ($tokens[$i]['line'] !== $prevLine) {
3✔
79
                $i++;
3✔
80
                break;
3✔
81
            }
82
        }
83

84
        if ($tokens[$i]['code'] === T_WHITESPACE) {
3✔
UNCOV
85
            $assignmentIndent = $tokens[$i]['length'];
×
86
        }
87

88
        // Find the actual indent.
89
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1));
3✔
90

91
        $expectedIndent = ($assignmentIndent + $this->indent);
3✔
92
        $foundIndent    = $tokens[$prev]['length'];
3✔
93
        if ($foundIndent !== $expectedIndent) {
3✔
94
            $error = 'Multi-line assignment not indented correctly; expected %s spaces but found %s';
3✔
95
            $data  = [
2✔
96
                $expectedIndent,
3✔
97
                $foundIndent,
3✔
98
            ];
2✔
99
            $phpcsFile->addError($error, $stackPtr, 'Indent', $data);
3✔
100
        }
101
    }
1✔
102
}
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