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

PHPCSStandards / PHP_CodeSniffer / 17174734458

23 Aug 2025 11:06AM UTC coverage: 76.88% (-2.1%) from 78.934%
17174734458

push

github

jrfnl
TEMP/TESTING PHPUnit 6331

19187 of 24957 relevant lines covered (76.88%)

60.25 hits per line

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

88.68
/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
    }//end register()
32

33

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

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

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

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

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

71
        $indices = [];
16✔
72

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

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

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

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

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

101
    }//end process()
102

103

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

117
        while ($ptr < $arrayEnd) {
16✔
118
            if (isset($tokens[$ptr]['scope_closer']) === true) {
16✔
119
                $ptr = $tokens[$ptr]['scope_closer'];
6✔
120
            } else if (isset($tokens[$ptr]['parenthesis_closer']) === true) {
16✔
121
                $ptr = $tokens[$ptr]['parenthesis_closer'];
2✔
122
            } else if (isset($tokens[$ptr]['bracket_closer']) === true) {
16✔
123
                $ptr = $tokens[$ptr]['bracket_closer'];
4✔
124
            }
125

126
            if ($tokens[$ptr]['code'] === T_COMMA
16✔
127
                || $tokens[$ptr]['code'] === T_DOUBLE_ARROW
16✔
128
            ) {
129
                return $ptr;
16✔
130
            }
131

132
            ++$ptr;
16✔
133
        }
134

135
        return $ptr;
6✔
136

137
    }//end getNext()
138

139

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

155

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

171

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

© 2026 Coveralls, Inc