-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
UseDeclarationSniff.php
293 lines (245 loc) · 11.9 KB
/
UseDeclarationSniff.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Ensures USE blocks are declared correctly.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Namespaces;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class UseDeclarationSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [T_USE];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($this->shouldIgnoreUse($phpcsFile, $stackPtr) === true) {
return;
}
$tokens = $phpcsFile->getTokens();
// One space after the use keyword.
if ($tokens[($stackPtr + 1)]['content'] !== ' ') {
$error = 'There must be a single space after the USE keyword';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse');
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
}
}
// Only one USE declaration allowed per statement.
$next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
if ($next !== false
&& $tokens[$next]['code'] !== T_SEMICOLON
&& $tokens[$next]['code'] !== T_CLOSE_TAG
) {
$error = 'There must be one USE keyword per declaration';
if ($tokens[$next]['code'] === T_COMMA) {
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations');
if ($fix === true) {
switch ($tokens[($stackPtr + 2)]['content']) {
case 'const':
$baseUse = 'use const';
break;
case 'function':
$baseUse = 'use function';
break;
default:
$baseUse = 'use';
}
$phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.$baseUse);
}
} else {
$closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));
if ($closingCurly === false) {
// Parse error or live coding. Not auto-fixable.
$phpcsFile->addError($error, $stackPtr, 'MultipleDeclarations');
} else {
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations');
if ($fix === true) {
$baseUse = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)));
$lastNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingCurly - 1), null, true);
$phpcsFile->fixer->beginChangeset();
// Remove base use statement.
for ($i = $stackPtr; $i <= $next; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
if (preg_match('`^[\r\n]+$`', $tokens[($next + 1)]['content']) === 1) {
$phpcsFile->fixer->replaceToken(($next + 1), '');
}
// Convert grouped use statements into full use statements.
do {
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
if ($next === false) {
// Group use statement with trailing comma after last item.
break;
}
$nonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true);
for ($i = ($nonWhitespace + 1); $i < $next; $i++) {
if (preg_match('`^[\r\n]+$`', $tokens[$i]['content']) === 1) {
// Preserve new lines.
continue;
}
$phpcsFile->fixer->replaceToken($i, '');
}
if ($tokens[$next]['content'] === 'const' || $tokens[$next]['content'] === 'function') {
$phpcsFile->fixer->addContentBefore($next, 'use ');
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
$phpcsFile->fixer->addContentBefore($next, str_replace('use ', '', $baseUse));
} else {
$phpcsFile->fixer->addContentBefore($next, $baseUse);
}
$next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly);
if ($next !== false) {
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['line'] === $tokens[$next]['line']) {
$prevNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($nextNonEmpty - 1), $next, true);
if ($prevNonWhitespace === $next) {
$phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar);
} else {
$phpcsFile->fixer->replaceToken($next, ';');
$phpcsFile->fixer->addNewline($prevNonWhitespace);
}
} else {
// Last item with trailing comma or next item already on new line.
$phpcsFile->fixer->replaceToken($next, ';');
}
} else {
// Last item without trailing comma.
$phpcsFile->fixer->addContent($lastNonWhitespace, ';');
}
} while ($next !== false);
// Remove closing curly,semi-colon and any whitespace between last child and closing curly.
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($closingCurly + 1), null, true);
if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
// Parse error, forgotten semi-colon.
$next = $closingCurly;
}
for ($i = ($lastNonWhitespace + 1); $i <= $next; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}//end if
}//end if
}//end if
}//end if
// Make sure this USE comes after the first namespace declaration.
$prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
if ($prev === false) {
$next = $phpcsFile->findNext(T_NAMESPACE, ($stackPtr + 1));
if ($next !== false) {
$error = 'USE declarations must go after the namespace declaration';
$phpcsFile->addError($error, $stackPtr, 'UseBeforeNamespace');
}
}
// Only interested in the last USE statement from here onwards.
$nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1));
while ($this->shouldIgnoreUse($phpcsFile, $nextUse) === true) {
$nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1));
if ($nextUse === false) {
break;
}
}
if ($nextUse !== false) {
return;
}
$end = $phpcsFile->findNext([T_SEMICOLON, T_CLOSE_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
if ($end === false) {
return;
}
if ($tokens[$end]['code'] === T_CLOSE_USE_GROUP) {
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
if ($tokens[$nextNonEmpty]['code'] === T_SEMICOLON) {
$end = $nextNonEmpty;
}
}
// Find either the start of the next line or the beginning of the next statement,
// whichever comes first.
for ($end = ++$end; $end < $phpcsFile->numTokens; $end++) {
if (isset(Tokens::$emptyTokens[$tokens[$end]['code']]) === false) {
break;
}
if ($tokens[$end]['column'] === 1) {
// Reached the next line.
break;
}
}
--$end;
if (($tokens[$end]['code'] === T_COMMENT
|| isset(Tokens::$phpcsCommentTokens[$tokens[$end]['code']]) === true)
&& substr($tokens[$end]['content'], 0, 2) === '/*'
&& substr($tokens[$end]['content'], -2) !== '*/'
) {
// Multi-line block comments are not allowed as trailing comment after a use statement.
--$end;
}
$next = $phpcsFile->findNext(T_WHITESPACE, ($end + 1), null, true);
if ($next === false || $tokens[$next]['code'] === T_CLOSE_TAG) {
return;
}
$diff = ($tokens[$next]['line'] - $tokens[$end]['line'] - 1);
if ($diff !== 1) {
if ($diff < 0) {
$diff = 0;
}
$error = 'There must be one blank line after the last USE statement; %s found;';
$data = [$diff];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterLastUse', $data);
if ($fix === true) {
if ($diff === 0) {
$phpcsFile->fixer->addNewline($end);
} else {
$phpcsFile->fixer->beginChangeset();
for ($i = ($end + 1); $i < $next; $i++) {
if ($tokens[$i]['line'] === $tokens[$next]['line']) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addNewline($end);
$phpcsFile->fixer->endChangeset();
}
}
}//end if
}//end process()
/**
* Check if this use statement is part of the namespace block.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return bool
*/
private function shouldIgnoreUse($phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Ignore USE keywords inside closures and during live coding.
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($next === false || $tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
return true;
}
// Ignore USE keywords for traits.
if ($phpcsFile->hasCondition($stackPtr, [T_CLASS, T_TRAIT]) === true) {
return true;
}
return false;
}//end shouldIgnoreUse()
}//end class