Conditions | 25 |
Paths | 216 |
Total Lines | 104 |
Lines | 10 |
Ratio | 9.62 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
199 | private function isTargetPHPErrormsgVar(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $tokens) |
||
200 | { |
||
201 | $scopeStart = 0; |
||
202 | |||
203 | /* |
||
204 | * If the variable is detected within the scope of a function/closure, limit the checking. |
||
205 | */ |
||
206 | $function = $phpcsFile->getCondition($stackPtr, T_CLOSURE); |
||
207 | if ($function === false) { |
||
208 | $function = $phpcsFile->getCondition($stackPtr, T_FUNCTION); |
||
209 | } |
||
210 | |||
211 | // It could also be a function param, which is not in the function scope. |
||
212 | if ($function === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
||
213 | $nestedParentheses = $tokens[$stackPtr]['nested_parenthesis']; |
||
214 | $parenthesisCloser = end($nestedParentheses); |
||
215 | if (isset($tokens[$parenthesisCloser]['parenthesis_owner']) |
||
216 | && ($tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === T_FUNCTION |
||
217 | || $tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === T_CLOSURE) |
||
218 | ) { |
||
219 | $function = $tokens[$parenthesisCloser]['parenthesis_owner']; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | if ($function !== false) { |
||
224 | $scopeStart = $tokens[$function]['scope_opener']; |
||
225 | } |
||
226 | |||
227 | /* |
||
228 | * Now, let's do some additional checks. |
||
229 | */ |
||
230 | $nextNonEmpty = $phpcsFile->findNext( |
||
231 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
232 | ($stackPtr + 1), |
||
233 | null, |
||
234 | true |
||
235 | ); |
||
236 | |||
237 | // Is the variable being used as an array ? |
||
238 | if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_OPEN_SQUARE_BRACKET) { |
||
239 | // The PHP native variable is a string, so this is probably not it |
||
240 | // (except for array access to string, but why would you in this case ?). |
||
241 | return false; |
||
242 | } |
||
243 | |||
244 | // Is this a variable assignment ? |
||
245 | View Code Duplication | if ($nextNonEmpty !== false |
|
246 | && in_array($tokens[$nextNonEmpty]['code'], \PHP_CodeSniffer_Tokens::$assignmentTokens, true) |
||
247 | ) { |
||
248 | return false; |
||
249 | } |
||
250 | |||
251 | // Is this a function param shadowing the PHP native one ? |
||
252 | if ($function !== false) { |
||
253 | $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $function); |
||
254 | if (is_array($parameters) === true && empty($parameters) === false) { |
||
255 | foreach ($parameters as $param) { |
||
256 | if ($param['name'] === '$php_errormsg') { |
||
257 | return false; |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | $skipPast = array( |
||
264 | 'T_CLASS' => true, |
||
265 | 'T_ANON_CLASS' => true, |
||
266 | 'T_INTERFACE' => true, |
||
267 | 'T_TRAIT' => true, |
||
268 | 'T_FUNCTION' => true, |
||
269 | 'T_CLOSURE' => true, |
||
270 | ); |
||
271 | |||
272 | // Walk back and see if there is an assignment to the variable within the same scope. |
||
273 | for ($i = ($stackPtr - 1); $i >= $scopeStart; $i--) { |
||
274 | if ($tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET |
||
275 | && isset($tokens[$i]['scope_condition']) |
||
276 | && isset($skipPast[$tokens[$tokens[$i]['scope_condition']]['type']]) |
||
277 | ) { |
||
278 | // Skip past functions, classes etc. |
||
279 | $i = $tokens[$i]['scope_condition']; |
||
280 | continue; |
||
281 | } |
||
282 | |||
283 | if ($tokens[$i]['code'] !== T_VARIABLE || $tokens[$i]['content'] !== '$php_errormsg') { |
||
284 | continue; |
||
285 | } |
||
286 | |||
287 | $nextNonEmpty = $phpcsFile->findNext( |
||
288 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
289 | ($i + 1), |
||
290 | null, |
||
291 | true |
||
292 | ); |
||
293 | |||
294 | View Code Duplication | if ($nextNonEmpty !== false |
|
295 | && in_array($tokens[$nextNonEmpty]['code'], \PHP_CodeSniffer_Tokens::$assignmentTokens, true) |
||
296 | ) { |
||
297 | return false; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | return true; |
||
302 | } |
||
303 | |||
306 |