-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ArrayIndentSniff.php
195 lines (164 loc) · 7.23 KB
/
ArrayIndentSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Ensures that array are indented one tab stop.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays;
use PHP_CodeSniffer\Sniffs\AbstractArraySniff;
use PHP_CodeSniffer\Util\Tokens;
class ArrayIndentSniff extends AbstractArraySniff
{
/**
* The number of spaces each array key should be indented.
*
* @var integer
*/
public $indent = 4;
/**
* Processes a single-line array definition.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $arrayStart The token that starts the array definition.
* @param int $arrayEnd The token that ends the array definition.
* @param array $indices An array of token positions for the array keys,
* double arrows, and values.
*
* @return void
*/
public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices)
{
}//end processSingleLineArray()
/**
* Processes a multi-line array definition.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $arrayStart The token that starts the array definition.
* @param int $arrayEnd The token that ends the array definition.
* @param array $indices An array of token positions for the array keys,
* double arrows, and values.
*
* @return void
*/
public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices)
{
$tokens = $phpcsFile->getTokens();
// Determine how far indented the entire array declaration should be.
$ignore = Tokens::$emptyTokens;
$ignore[] = T_DOUBLE_ARROW;
$ignore[] = T_COMMA;
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
$start = $phpcsFile->findStartOfStatement($prev);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $start, true);
$baseIndent = ($tokens[$first]['column'] - 1);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
$startIndent = ($tokens[$first]['column'] - 1);
// If the open brace is not indented to at least to the level of the start
// of the statement, the sniff will conflict with other sniffs trying to
// check indent levels because it's not valid. But we don't enforce exactly
// how far indented it should be.
if ($startIndent < $baseIndent) {
$pluralizeSpace = 's';
if ($baseIndent === 1) {
$pluralizeSpace = '';
}
$error = 'Array open brace not indented correctly; expected at least %s space%s but found %s';
$data = [
$baseIndent,
$pluralizeSpace,
$startIndent,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenBraceIncorrect', $data);
if ($fix === true) {
$padding = str_repeat(' ', $baseIndent);
if ($startIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else {
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
}
}
return;
}//end if
$expectedIndent = ($startIndent + $this->indent);
foreach ($indices as $index) {
if (isset($index['index_start']) === true) {
$start = $index['index_start'];
} else {
$start = $index['value_start'];
}
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($start - 1), null, true);
if ($tokens[$prev]['line'] === $tokens[$start]['line']) {
// This index isn't the only content on the line
// so we can't check indent rules.
continue;
}
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $start, true);
$foundIndent = ($tokens[$first]['column'] - 1);
if ($foundIndent === $expectedIndent) {
continue;
}
$pluralizeSpace = 's';
if ($expectedIndent === 1) {
$pluralizeSpace = '';
}
$error = 'Array key not indented correctly; expected %s space%s but found %s';
$data = [
$expectedIndent,
$pluralizeSpace,
$foundIndent,
];
$fix = $phpcsFile->addFixableError($error, $first, 'KeyIncorrect', $data);
if ($fix === false) {
continue;
}
$padding = str_repeat(' ', $expectedIndent);
if ($foundIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else {
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
}
}//end foreach
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($arrayEnd - 1), null, true);
if ($tokens[$prev]['line'] === $tokens[$arrayEnd]['line']) {
$error = 'Closing brace of array declaration must be on a new line';
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotNewLine');
if ($fix === true) {
$padding = $phpcsFile->eolChar.str_repeat(' ', $expectedIndent);
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
}
return;
}
// The close brace must be indented one stop less.
$expectedIndent -= $this->indent;
$foundIndent = ($tokens[$arrayEnd]['column'] - 1);
if ($foundIndent === $expectedIndent) {
return;
}
$pluralizeSpace = 's';
if ($expectedIndent === 1) {
$pluralizeSpace = '';
}
$error = 'Array close brace not indented correctly; expected %s space%s but found %s';
$data = [
$expectedIndent,
$pluralizeSpace,
$foundIndent,
];
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceIncorrect', $data);
if ($fix === false) {
return;
}
$padding = str_repeat(' ', $expectedIndent);
if ($foundIndent === 0) {
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
} else {
$phpcsFile->fixer->replaceToken(($arrayEnd - 1), $padding);
}
}//end processMultiLineArray()
}//end class