-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSideEffectsSniff.php
301 lines (257 loc) · 11 KB
/
SideEffectsSniff.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
294
295
296
297
298
299
300
301
<?php
/**
* Ensures a file declares new symbols and causes no other side effects, or executes logic with side effects, but not both.
*
* @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\PSR1\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class SideEffectsSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [T_OPEN_TAG];
}//end register()
/**
* Processes this sniff, 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 token stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$result = $this->searchForConflict($phpcsFile, 0, ($phpcsFile->numTokens - 1), $tokens);
if ($result['symbol'] !== null && $result['effect'] !== null) {
$error = 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line %s and the first side effect is on line %s.';
$data = [
$tokens[$result['symbol']]['line'],
$tokens[$result['effect']]['line'],
];
$phpcsFile->addWarning($error, 0, 'FoundWithSymbols', $data);
$phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'yes');
} else {
$phpcsFile->recordMetric($stackPtr, 'Declarations and side effects mixed', 'no');
}
// Ignore the rest of the file.
return ($phpcsFile->numTokens + 1);
}//end process()
/**
* Searches for symbol declarations and side effects.
*
* Returns the positions of both the first symbol declared and the first
* side effect in the file. A NULL value for either indicates nothing was
* found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $start The token to start searching from.
* @param int $end The token to search to.
* @param array $tokens The stack of tokens that make up
* the file.
*
* @return array
*/
private function searchForConflict($phpcsFile, $start, $end, $tokens)
{
$symbols = [
T_CLASS => T_CLASS,
T_INTERFACE => T_INTERFACE,
T_TRAIT => T_TRAIT,
T_ENUM => T_ENUM,
T_FUNCTION => T_FUNCTION,
];
$conditions = [
T_IF => T_IF,
T_ELSE => T_ELSE,
T_ELSEIF => T_ELSEIF,
];
$checkAnnotations = $phpcsFile->config->annotations;
$firstSymbol = null;
$firstEffect = null;
for ($i = $start; $i <= $end; $i++) {
// Respect phpcs:disable comments.
if ($checkAnnotations === true
&& $tokens[$i]['code'] === T_PHPCS_DISABLE
&& (empty($tokens[$i]['sniffCodes']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1.Files']) === true
|| isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === true)
) {
do {
$i = $phpcsFile->findNext(T_PHPCS_ENABLE, ($i + 1));
} while ($i !== false
&& empty($tokens[$i]['sniffCodes']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1.Files']) === false
&& isset($tokens[$i]['sniffCodes']['PSR1.Files.SideEffects']) === false);
if ($i === false) {
// The entire rest of the file is disabled,
// so return what we have so far.
break;
}
continue;
}
// Ignore whitespace and comments.
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
continue;
}
// Ignore PHP tags.
if ($tokens[$i]['code'] === T_OPEN_TAG
|| $tokens[$i]['code'] === T_CLOSE_TAG
) {
continue;
}
// Ignore shebang.
if (substr($tokens[$i]['content'], 0, 2) === '#!') {
continue;
}
// Ignore logical operators.
if (isset(Tokens::$booleanOperators[$tokens[$i]['code']]) === true) {
continue;
}
// Ignore entire namespace, declare, const and use statements.
if ($tokens[$i]['code'] === T_NAMESPACE
|| $tokens[$i]['code'] === T_USE
|| $tokens[$i]['code'] === T_DECLARE
|| $tokens[$i]['code'] === T_CONST
) {
if (isset($tokens[$i]['scope_opener']) === true) {
$i = $tokens[$i]['scope_closer'];
if ($tokens[$i]['code'] === T_ENDDECLARE) {
$semicolon = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
if ($semicolon !== false && $tokens[$semicolon]['code'] === T_SEMICOLON) {
$i = $semicolon;
}
}
} else {
$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
if ($semicolon !== false) {
$i = $semicolon;
}
}
continue;
}
// Ignore function/class prefixes.
if (isset(Tokens::$methodPrefixes[$tokens[$i]['code']]) === true
|| $tokens[$i]['code'] === T_READONLY
) {
continue;
}
// Ignore anon classes.
if ($tokens[$i]['code'] === T_ANON_CLASS) {
$i = $tokens[$i]['scope_closer'];
continue;
}
// Ignore attributes.
if ($tokens[$i]['code'] === T_ATTRIBUTE
&& isset($tokens[$i]['attribute_closer']) === true
) {
$i = $tokens[$i]['attribute_closer'];
continue;
}
// Detect and skip over symbols.
if (isset($symbols[$tokens[$i]['code']]) === true
&& isset($tokens[$i]['scope_closer']) === true
) {
if ($firstSymbol === null) {
$firstSymbol = $i;
}
$i = $tokens[$i]['scope_closer'];
continue;
} else if ($tokens[$i]['code'] === T_STRING
&& strtolower($tokens[$i]['content']) === 'define'
) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_DOUBLE_COLON
&& $tokens[$prev]['code'] !== T_FUNCTION
) {
if ($firstSymbol === null) {
$firstSymbol = $i;
}
$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1));
if ($semicolon !== false) {
$i = $semicolon;
}
continue;
}
}//end if
// Special case for defined() as it can be used to see
// if a constant (a symbol) should be defined or not and
// doesn't need to use a full conditional block.
if ($tokens[$i]['code'] === T_STRING
&& strtolower($tokens[$i]['content']) === 'defined'
) {
$openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
if ($openBracket !== false
&& $tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS
&& isset($tokens[$openBracket]['parenthesis_closer']) === true
) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_DOUBLE_COLON
&& $tokens[$prev]['code'] !== T_FUNCTION
) {
$i = $tokens[$openBracket]['parenthesis_closer'];
continue;
}
}
}//end if
// Conditional statements are allowed in symbol files as long as the
// contents is only a symbol definition. So don't count these as effects
// in this case.
if (isset($conditions[$tokens[$i]['code']]) === true) {
if (isset($tokens[$i]['scope_opener']) === false) {
// Probably an "else if", so just ignore.
continue;
}
$result = $this->searchForConflict(
$phpcsFile,
($tokens[$i]['scope_opener'] + 1),
($tokens[$i]['scope_closer'] - 1),
$tokens
);
if ($result['symbol'] !== null) {
if ($firstSymbol === null) {
$firstSymbol = $result['symbol'];
}
if ($result['effect'] !== null) {
// Found a conflict.
$firstEffect = $result['effect'];
break;
}
}
if ($firstEffect === null) {
$firstEffect = $result['effect'];
}
$i = $tokens[$i]['scope_closer'];
continue;
}//end if
if ($firstEffect === null) {
$firstEffect = $i;
}
if ($firstSymbol !== null) {
// We have a conflict we have to report, so no point continuing.
break;
}
}//end for
return [
'symbol' => $firstSymbol,
'effect' => $firstEffect,
];
}//end searchForConflict()
}//end class