-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathCheckReturnValueSniff.php
318 lines (266 loc) · 9.59 KB
/
CheckReturnValueSniff.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
*/
namespace WordPressVIPMinimum\Sniffs\Functions;
use PHP_CodeSniffer\Util\Tokens;
use WordPressVIPMinimum\Sniffs\Sniff;
/**
* This sniff enforces checking the return value of a function before passing it to another one.
*
* An example of a not checking return value is:
*
* <code>
* echo esc_url( wpcom_vip_get_term_link( $term ) );
* </code>
*/
class CheckReturnValueSniff extends Sniff {
/**
* Pairs we are about to check.
*
* @var array<string, array<string>>
*/
public $catch = [
'esc_url' => [
'get_term_link',
],
'wp_list_pluck' => [
'get_the_tags',
'get_the_terms',
],
'foreach' => [
'get_post_meta',
'get_term_meta',
'get_the_terms',
'get_the_tags',
],
'array_key_exists' => [
'get_option',
],
];
/**
* Tokens we are about to examine, which are not functions.
*
* @var array<string, int|string>
*/
public $notFunctions = [
'foreach' => T_FOREACH,
];
/**
* Returns the token types that this sniff is interested in.
*
* @return array<int|string>
*/
public function register() {
return [ T_STRING ];
}
/**
* Processes the tokens that this sniff is interested in.
*
* @param int $stackPtr The position in the stack where
* the token was found.
*
* @return void
*/
public function process_token( $stackPtr ) {
$this->findDirectFunctionCalls( $stackPtr );
$this->findNonCheckedVariables( $stackPtr );
}
/**
* Check whether the currently examined code is a function call.
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return bool
*/
private function isFunctionCall( $stackPtr ) {
if ( $this->tokens[ $stackPtr ]['code'] !== T_STRING ) {
return false;
}
// Find the next non-empty token.
$openBracket = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true );
if ( $this->tokens[ $openBracket ]['code'] !== T_OPEN_PARENTHESIS ) {
// Not a function call.
return false;
}
// Find the previous non-empty token.
$search = Tokens::$emptyTokens;
$search[] = T_BITWISE_AND;
$previous = $this->phpcsFile->findPrevious( $search, $stackPtr - 1, null, true );
// It's a function definition, not a function call, so return false.
return ! ( $this->tokens[ $previous ]['code'] === T_FUNCTION );
}
/**
* Check whether the examined code is a variable assignment.
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return int|false
*/
private function isVariableAssignment( $stackPtr ) {
// Find the previous non-empty token.
$search = Tokens::$emptyTokens;
$search[] = T_BITWISE_AND;
$previous = $this->phpcsFile->findPrevious( $search, $stackPtr - 1, null, true );
if ( $this->tokens[ $previous ]['code'] !== T_EQUAL ) {
// It's not a variable assignment.
return false;
}
$previous = $this->phpcsFile->findPrevious( $search, $previous - 1, null, true );
if ( $this->tokens[ $previous ]['code'] !== T_VARIABLE ) {
// It's not a variable assignment.
return false;
}
return $previous;
}
/**
* Find instances in which a function call is directly passed to another one w/o checking the return type
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function findDirectFunctionCalls( $stackPtr ) {
$functionName = $this->tokens[ $stackPtr ]['content'];
if ( array_key_exists( $functionName, $this->catch ) === false ) {
// Not a function we are looking for.
return;
}
if ( $this->isFunctionCall( $stackPtr ) === false ) {
// Not a function call.
return;
}
// Find the next non-empty token.
$openBracket = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true );
// Find the closing bracket.
$closeBracket = $this->tokens[ $openBracket ]['parenthesis_closer'];
$startNext = $openBracket + 1;
$next = $this->phpcsFile->findNext( T_STRING, $startNext, $closeBracket, false, null, true );
while ( $next ) {
if ( in_array( $this->tokens[ $next ]['content'], $this->catch[ $functionName ], true ) === true ) {
$message = "`%s`'s return type must be checked before calling `%s` using that value.";
$data = [ $this->tokens[ $next ]['content'], $functionName ];
$this->phpcsFile->addError( $message, $next, 'DirectFunctionCall', $data );
}
$next = $this->phpcsFile->findNext( T_STRING, $next + 1, $closeBracket, false, null, true );
}
}
/**
* Deals with situations in which the variable is being used later in the code along with a function which is known for causing issues.
*
* This only catches situations in which the variable is not being used with some other function before it's interacting with function we look for.
* That's currently necessary in order to prevent false positives.
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function findNonCheckedVariables( $stackPtr ) {
$functionName = $this->tokens[ $stackPtr ]['content'];
$isFunctionWeLookFor = false;
$callees = [];
foreach ( $this->catch as $callee => $checkReturnArray ) {
if ( in_array( $functionName, $checkReturnArray, true ) === true ) {
$isFunctionWeLookFor = true;
$callees[] = $callee;
}
}
if ( $isFunctionWeLookFor === false ) {
// Not a function we are looking for.
return;
}
if ( $this->isFunctionCall( $stackPtr ) === false ) {
// Not a function call.
return;
}
$variablePos = $this->isVariableAssignment( $stackPtr );
if ( $variablePos === false ) {
// Not a variable assignment.
return;
}
$variableToken = $this->tokens[ $variablePos ];
$variableName = $variableToken['content'];
// Find the next non-empty token.
$openBracket = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true );
// Find the closing bracket.
$closeBracket = $this->tokens[ $openBracket ]['parenthesis_closer'];
if ( in_array( $functionName, [ 'get_post_meta', 'get_term_meta' ], true ) === true ) {
// Since the get_post_meta and get_term_meta always returns an array if $single is set to `true` we need to check for the value of it's third param before proceeding.
$params = [];
$paramNo = 1;
$prevCommaPos = $openBracket + 1;
for ( $i = $openBracket + 1; $i <= $closeBracket; $i++ ) {
if ( $this->tokens[ $i ]['code'] === T_OPEN_PARENTHESIS ) {
$i = $this->tokens[ $i ]['parenthesis_closer'];
}
if ( $this->tokens[ $i ]['code'] === T_COMMA ) {
$params[ $paramNo++ ] = trim( array_reduce( array_slice( $this->tokens, $prevCommaPos, $i - $prevCommaPos ), [ $this, 'reduce_array' ] ) );
$prevCommaPos = $i + 1;
}
if ( $i === $closeBracket ) {
$params[ $paramNo ] = trim( array_reduce( array_slice( $this->tokens, $prevCommaPos, $i - $prevCommaPos ), [ $this, 'reduce_array' ] ) );
break;
}
}
if ( array_key_exists( 3, $params ) === false || $params[3] === 'false' ) {
// Third param of get_post_meta is not set (default to false) or is set to false.
// Means the function returns an array. We are good then.
return;
}
}
$nextVariableOccurrence = $this->phpcsFile->findNext( T_VARIABLE, $closeBracket + 1, null, false, $variableName );
// Find previous non-empty token, which is not an open parenthesis, comma nor variable.
$search = Tokens::$emptyTokens;
$search[] = T_OPEN_PARENTHESIS;
// This allows us to check for variables which are passed as second parameter of a function e.g.: array_key_exists.
$search[] = T_COMMA;
$search[] = T_VARIABLE;
$search[] = T_CONSTANT_ENCAPSED_STRING;
$nextFunctionCallWithVariable = $this->phpcsFile->findPrevious( $search, $nextVariableOccurrence - 1, null, true );
foreach ( $callees as $callee ) {
$notFunctionsCallee = array_key_exists( $callee, $this->notFunctions ) ? (array) $this->notFunctions[ $callee ] : [];
// Check whether the found token is one of the function calls (or foreach call) we are interested in.
if ( in_array( $this->tokens[ $nextFunctionCallWithVariable ]['code'], array_merge( [ T_STRING ], $notFunctionsCallee ), true ) === true
&& $this->tokens[ $nextFunctionCallWithVariable ]['content'] === $callee
) {
$this->addNonCheckedVariableError( $nextFunctionCallWithVariable, $variableName, $callee );
return;
}
$search = array_merge( Tokens::$emptyTokens, [ T_EQUAL ] );
$next = $this->phpcsFile->findNext( $search, $nextVariableOccurrence + 1, null, true );
if ( $this->tokens[ $next ]['code'] === T_STRING
&& $this->tokens[ $next ]['content'] === $callee
) {
$this->addNonCheckedVariableError( $next, $variableName, $callee );
return;
}
}
}
/**
* Function used as as callback for the array_reduce call.
*
* @param string|null $carry The final string.
* @param array $item Processed item.
*
* @return string
*/
public function reduce_array( $carry, $item ) {
return $carry . $item['content'];
}
/**
* Consolidated violation.
*
* @param int $stackPtr The position in the stack where the token was found.
* @param string $variableName Variable name.
* @param string $callee Function name.
*
* @return void
*/
private function addNonCheckedVariableError( $stackPtr, $variableName, $callee ) {
$message = 'Type of `%s` must be checked before calling `%s()` using that variable.';
$data = [ $variableName, $callee ];
$this->phpcsFile->addError( $message, $stackPtr, 'NonCheckedVariable', $data );
}
}