• 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

92.31
/src/Sniffs/AbstractArraySniff.php
1
<?php
2
/**
3
 * Processes single and multi-line arrays.
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\Sniffs;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Util\Tokens;
14

15
abstract class AbstractArraySniff implements Sniff
16
{
17

18

19
    /**
20
     * Returns an array of tokens this test wants to listen for.
21
     *
22
     * @return array<int|string>
23
     */
24
    final public function register()
×
25
    {
26
        return [
27
            T_ARRAY,
×
28
            T_OPEN_SHORT_ARRAY,
×
29
        ];
30
    }
31

32

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

46
        if ($tokens[$stackPtr]['code'] === T_ARRAY) {
24✔
47
            $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
9✔
48

49
            $arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
9✔
50
            if (isset($tokens[$arrayStart]['parenthesis_closer']) === false) {
9✔
51
                // Incomplete array.
UNCOV
52
                return;
×
53
            }
54

55
            $arrayEnd = $tokens[$arrayStart]['parenthesis_closer'];
9✔
56
        } else {
57
            $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
15✔
58
            $arrayStart = $stackPtr;
15✔
59
            $arrayEnd   = $tokens[$stackPtr]['bracket_closer'];
15✔
60
        }
61

62
        $lastContent = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($arrayEnd - 1), null, true);
24✔
63
        if ($tokens[$lastContent]['code'] === T_COMMA) {
24✔
64
            // Last array item ends with a comma.
65
            $phpcsFile->recordMetric($stackPtr, 'Array end comma', 'yes');
15✔
66
        } else {
67
            $phpcsFile->recordMetric($stackPtr, 'Array end comma', 'no');
9✔
68
        }
69

70
        $indices = [];
24✔
71

72
        $current = $arrayStart;
24✔
73
        while (($next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($current + 1), $arrayEnd, true)) !== false) {
24✔
74
            $end = $this->getNext($phpcsFile, $next, $arrayEnd);
24✔
75

76
            if ($tokens[$end]['code'] === T_DOUBLE_ARROW) {
24✔
77
                $indexEnd   = $phpcsFile->findPrevious(T_WHITESPACE, ($end - 1), null, true);
18✔
78
                $valueStart = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($end + 1), null, true);
18✔
79

80
                $indices[] = [
18✔
81
                    'index_start' => $next,
18✔
82
                    'index_end'   => $indexEnd,
18✔
83
                    'arrow'       => $end,
18✔
84
                    'value_start' => $valueStart,
18✔
85
                ];
12✔
86
            } else {
87
                $valueStart = $next;
12✔
88
                $indices[]  = ['value_start' => $valueStart];
12✔
89
            }
90

91
            $current = $this->getNext($phpcsFile, $valueStart, $arrayEnd);
24✔
92
        }
93

94
        if ($tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']) {
24✔
95
            $this->processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices);
9✔
96
        } else {
97
            $this->processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices);
15✔
98
        }
99
    }
8✔
100

101

102
    /**
103
     * Find next separator in array - either: comma or double arrow.
104
     *
105
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
106
     * @param int                         $ptr       The position of current token.
107
     * @param int                         $arrayEnd  The token that ends the array definition.
108
     *
109
     * @return int
110
     */
111
    private function getNext(File $phpcsFile, int $ptr, int $arrayEnd)
24✔
112
    {
113
        $tokens = $phpcsFile->getTokens();
24✔
114

115
        while ($ptr < $arrayEnd) {
24✔
116
            if (isset($tokens[$ptr]['scope_closer']) === true) {
24✔
117
                $ptr = $tokens[$ptr]['scope_closer'];
9✔
118
            } elseif (isset($tokens[$ptr]['parenthesis_closer']) === true) {
24✔
119
                $ptr = $tokens[$ptr]['parenthesis_closer'];
3✔
120
            } elseif (isset($tokens[$ptr]['bracket_closer']) === true) {
24✔
121
                $ptr = $tokens[$ptr]['bracket_closer'];
6✔
122
            }
123

124
            if ($tokens[$ptr]['code'] === T_COMMA
24✔
125
                || $tokens[$ptr]['code'] === T_DOUBLE_ARROW
24✔
126
            ) {
127
                return $ptr;
24✔
128
            }
129

130
            ++$ptr;
24✔
131
        }
132

133
        return $ptr;
9✔
134
    }
135

136

137
    /**
138
     * Processes a single-line array definition.
139
     *
140
     * @param \PHP_CodeSniffer\Files\File $phpcsFile  The current file being checked.
141
     * @param int                         $stackPtr   The position of the current token
142
     *                                                in the stack passed in $tokens.
143
     * @param int                         $arrayStart The token that starts the array definition.
144
     * @param int                         $arrayEnd   The token that ends the array definition.
145
     * @param array                       $indices    An array of token positions for the array keys,
146
     *                                                double arrows, and values.
147
     *
148
     * @return void
149
     */
150
    abstract protected function processSingleLineArray(File $phpcsFile, int $stackPtr, int $arrayStart, int $arrayEnd, array $indices);
151

152

153
    /**
154
     * Processes a multi-line array definition.
155
     *
156
     * @param \PHP_CodeSniffer\Files\File $phpcsFile  The current file being checked.
157
     * @param int                         $stackPtr   The position of the current token
158
     *                                                in the stack passed in $tokens.
159
     * @param int                         $arrayStart The token that starts the array definition.
160
     * @param int                         $arrayEnd   The token that ends the array definition.
161
     * @param array                       $indices    An array of token positions for the array keys,
162
     *                                                double arrows, and values.
163
     *
164
     * @return void
165
     */
166
    abstract protected function processMultiLineArray(File $phpcsFile, int $stackPtr, int $arrayStart, int $arrayEnd, array $indices);
167
}
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