1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSR2R\Sniffs\PHP; |
4
|
|
|
|
5
|
|
|
use PHP_CodeSniffer_File; |
6
|
|
|
use PHP_CodeSniffer_Tokens; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* is_null() should be replaced by === null check. |
10
|
|
|
* |
11
|
|
|
* @author Mark Scherer |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
class NoIsNullSniff extends \PSR2R\Tools\AbstractSniff { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
19
|
|
|
public function register() { |
20
|
|
|
return [T_STRING]; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
27
|
|
|
$wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW, T_DOUBLE_COLON]; |
28
|
|
|
|
29
|
|
|
$tokens = $phpcsFile->getTokens(); |
30
|
|
|
|
31
|
|
|
$tokenContent = $tokens[$stackPtr]['content']; |
32
|
|
|
if (strtolower($tokenContent) !== 'is_null') { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
37
|
|
|
if (!$previous || in_array($tokens[$previous]['code'], $wrongTokens)) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$openingBraceIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
42
|
|
|
if (!$openingBraceIndex || $tokens[$openingBraceIndex]['type'] !== 'T_OPEN_PARENTHESIS') { |
|
|
|
|
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$closingBraceIndex = $tokens[$openingBraceIndex]['parenthesis_closer']; |
47
|
|
|
|
48
|
|
|
$error = $tokenContent . '() found, should be strict === null check.'; |
49
|
|
|
|
50
|
|
|
$possibleCastIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
51
|
|
|
$negated = false; |
52
|
|
|
if ($possibleCastIndex && $tokens[$possibleCastIndex]['code'] === T_BOOLEAN_NOT) { |
53
|
|
|
$negated = true; |
54
|
|
|
} |
55
|
|
|
// We dont want to fix double !! |
56
|
|
|
if ($negated) { |
57
|
|
|
$anotherPossibleCastIndex = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($possibleCastIndex - 1), null, true); |
58
|
|
|
if ($tokens[$anotherPossibleCastIndex]['code'] === T_BOOLEAN_NOT) { |
59
|
|
|
$phpcsFile->addError($error, $stackPtr); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// We don't want to fix stuff with bad inline assignment |
65
|
|
|
if ($this->contains($phpcsFile, 'T_EQUAL', $openingBraceIndex + 1, $closingBraceIndex - 1)) { |
66
|
|
|
$phpcsFile->addError($error, $stackPtr); |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$beginningIndex = $negated ? $possibleCastIndex : $stackPtr; |
71
|
|
|
$endIndex = $closingBraceIndex; |
72
|
|
|
|
73
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr); |
74
|
|
|
if ($fix) { |
75
|
|
|
$needsBrackets = $this->needsBrackets($phpcsFile, $openingBraceIndex, $closingBraceIndex); |
76
|
|
|
$leadingComparison = $this->hasLeadingComparison($phpcsFile, $beginningIndex); |
|
|
|
|
77
|
|
|
$trailingComparison = $this->hasTrailingComparison($phpcsFile, $closingBraceIndex); |
78
|
|
|
|
79
|
|
View Code Duplication |
if ($leadingComparison) { |
|
|
|
|
80
|
|
|
$possibleBeginningIndex = $this->findUnnecessaryLeadingComparisonStart($phpcsFile, $beginningIndex); |
|
|
|
|
81
|
|
|
if ($possibleBeginningIndex !== null) { |
82
|
|
|
$beginningIndex = $possibleBeginningIndex; |
83
|
|
|
$leadingComparison = false; |
84
|
|
|
if ($tokens[$beginningIndex]['code'] === T_FALSE) { |
85
|
|
|
$negated = !$negated; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
if ($trailingComparison) { |
|
|
|
|
91
|
|
|
$possibleEndIndex = $this->findUnnecessaryLeadingComparisonStart($phpcsFile, $endIndex); |
92
|
|
|
if ($possibleEndIndex !== null) { |
93
|
|
|
$endIndex = $possibleEndIndex; |
94
|
|
|
$trailingComparison = false; |
95
|
|
|
if ($tokens[$endIndex]['code'] === T_FALSE) { |
96
|
|
|
$negated = !$negated; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$needsBrackets && ($leadingComparison || $this->leadRequiresBrackets($phpcsFile, $beginningIndex))) { |
|
|
|
|
102
|
|
|
$needsBrackets = true; |
103
|
|
|
} |
104
|
|
|
if (!$needsBrackets && $trailingComparison) { |
105
|
|
|
$needsBrackets = true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$comparisonString = ' ' . ($negated ? '!' : '=') . '== null'; |
109
|
|
|
|
110
|
|
|
$phpcsFile->fixer->beginChangeset(); |
111
|
|
|
|
112
|
|
|
if ($negated) { |
|
|
|
|
113
|
|
|
//$phpcsFile->fixer->replaceToken($possibleCastIndex, ''); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
if ($beginningIndex !== $stackPtr) { |
116
|
|
|
for ($i = $beginningIndex; $i < $stackPtr; $i++) { |
117
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
if ($endIndex !== $closingBraceIndex) { |
121
|
|
|
for ($i = $endIndex; $i > $closingBraceIndex; $i--) { |
122
|
|
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$phpcsFile->fixer->replaceToken($stackPtr, ''); |
127
|
|
|
if (!$needsBrackets) { |
128
|
|
|
$phpcsFile->fixer->replaceToken($openingBraceIndex, ''); |
129
|
|
|
$phpcsFile->fixer->replaceToken($closingBraceIndex, $comparisonString); |
130
|
|
|
} else { |
131
|
|
|
$phpcsFile->fixer->replaceToken($closingBraceIndex, $comparisonString . ')'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$phpcsFile->fixer->endChangeset(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
140
|
|
|
* @param int $index |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
protected function leadRequiresBrackets(PHP_CodeSniffer_File $phpcsFile, $index) { |
144
|
|
|
$tokens = $phpcsFile->getTokens(); |
145
|
|
|
|
146
|
|
|
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($index - 1), null, true); |
147
|
|
|
if ($this->isCast($phpcsFile, $previous)) { |
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$arithmeticTokens)) { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
159
|
|
|
* @param int $index |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
protected function isCast(PHP_CodeSniffer_File $phpcsFile, $index) { |
163
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
|
|
|
164
|
|
|
|
165
|
|
|
return in_array($index, PHP_CodeSniffer_Tokens::$castTokens); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
170
|
|
|
* @param int $index |
171
|
|
|
* @return int|null |
172
|
|
|
*/ |
173
|
|
|
protected function findUnnecessaryLeadingComparisonStart(PHP_CodeSniffer_File $phpcsFile, $index) { |
174
|
|
|
$tokens = $phpcsFile->getTokens(); |
175
|
|
|
|
176
|
|
|
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($index - 1), null, true); |
177
|
|
View Code Duplication |
if (!in_array($tokens[$previous]['code'], [T_IS_IDENTICAL, T_IS_NOT_IDENTICAL])) { |
|
|
|
|
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($previous - 1), null, true); |
182
|
|
View Code Duplication |
if (!in_array($tokens[$previous]['code'], [T_TRUE, T_FALSE])) { |
|
|
|
|
183
|
|
|
return null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $previous; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
191
|
|
|
* @param int $index |
192
|
|
|
* @return int|null |
193
|
|
|
*/ |
194
|
|
|
protected function findUnnecessaryTrailingComparisonEnd(PHP_CodeSniffer_File $phpcsFile, $index) { |
195
|
|
|
$tokens = $phpcsFile->getTokens(); |
196
|
|
|
|
197
|
|
|
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($index + 1), null, true); |
198
|
|
View Code Duplication |
if (!$next || !in_array($tokens[$next]['code'], [T_IS_IDENTICAL, T_IS_NOT_IDENTICAL])) { |
|
|
|
|
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($next - 1), null, true); |
203
|
|
View Code Duplication |
if (!$prev || !in_array($tokens[$prev]['code'], [T_TRUE, T_FALSE])) { |
|
|
|
|
204
|
|
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $next; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
212
|
|
|
* @param int $stackPtr |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
protected function hasLeadingComparison(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
216
|
|
|
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
217
|
|
|
return $this->isComparison($phpcsFile, $previous); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
222
|
|
|
* @param int $stackPtr |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
protected function hasTrailingComparison(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
226
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
|
|
|
227
|
|
|
|
228
|
|
|
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
229
|
|
|
return $this->isComparison($phpcsFile, $next); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile |
234
|
|
|
* @param int $index |
235
|
|
|
* @return bool |
236
|
|
|
*/ |
237
|
|
|
protected function isComparison(PHP_CodeSniffer_File $phpcsFile, $index) { |
238
|
|
|
$tokens = $phpcsFile->getTokens(); |
239
|
|
|
|
240
|
|
|
$blacklistedCodes = [ |
241
|
|
|
T_IS_NOT_EQUAL, T_IS_EQUAL, T_IS_IDENTICAL, T_IS_NOT_IDENTICAL, T_IS_GREATER_OR_EQUAL, T_IS_SMALLER_OR_EQUAL |
242
|
|
|
]; |
243
|
|
|
$blacklistedTypes = [ |
244
|
|
|
'T_LESS_THAN', 'T_GREATER_THAN', |
245
|
|
|
]; |
246
|
|
|
if (in_array($tokens[$index]['code'], $blacklistedCodes)) { |
247
|
|
|
return true; |
248
|
|
|
} |
249
|
|
|
if (in_array($tokens[$index]['type'], $blacklistedTypes)) { |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: