Conditions | 25 |
Paths | 216 |
Total Lines | 94 |
Lines | 21 |
Ratio | 22.34 % |
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 |
||
224 | private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens) |
||
225 | { |
||
226 | $scopeStart = 0; |
||
227 | |||
228 | /* |
||
229 | * If the variable is detected within the scope of a function/closure, limit the checking. |
||
230 | */ |
||
231 | $function = $phpcsFile->getCondition($stackPtr, \T_CLOSURE); |
||
232 | if ($function === false) { |
||
233 | $function = $phpcsFile->getCondition($stackPtr, \T_FUNCTION); |
||
234 | } |
||
235 | |||
236 | // It could also be a function param, which is not in the function scope. |
||
237 | if ($function === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
||
238 | $nestedParentheses = $tokens[$stackPtr]['nested_parenthesis']; |
||
239 | $parenthesisCloser = end($nestedParentheses); |
||
240 | if (isset($tokens[$parenthesisCloser]['parenthesis_owner']) |
||
241 | && ($tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_FUNCTION |
||
242 | || $tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_CLOSURE) |
||
243 | ) { |
||
244 | $function = $tokens[$parenthesisCloser]['parenthesis_owner']; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | if ($function !== false) { |
||
249 | $scopeStart = $tokens[$function]['scope_opener']; |
||
250 | } |
||
251 | |||
252 | /* |
||
253 | * Now, let's do some additional checks. |
||
254 | */ |
||
255 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
256 | |||
257 | // Is the variable being used as an array ? |
||
258 | if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_OPEN_SQUARE_BRACKET) { |
||
259 | // The PHP native variable is a string, so this is probably not it |
||
260 | // (except for array access to string, but why would you in this case ?). |
||
261 | return false; |
||
262 | } |
||
263 | |||
264 | // Is this a variable assignment ? |
||
265 | View Code Duplication | if ($nextNonEmpty !== false |
|
|
|||
266 | && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
||
267 | ) { |
||
268 | return false; |
||
269 | } |
||
270 | |||
271 | // Is this a function param shadowing the PHP native one ? |
||
272 | if ($function !== false) { |
||
273 | $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $function); |
||
274 | if (\is_array($parameters) === true && empty($parameters) === false) { |
||
275 | foreach ($parameters as $param) { |
||
276 | if ($param['name'] === '$php_errormsg') { |
||
277 | return false; |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | $skipPast = array( |
||
284 | 'T_CLASS' => true, |
||
285 | 'T_ANON_CLASS' => true, |
||
286 | 'T_INTERFACE' => true, |
||
287 | 'T_TRAIT' => true, |
||
288 | 'T_FUNCTION' => true, |
||
289 | 'T_CLOSURE' => true, |
||
290 | ); |
||
291 | |||
292 | // Walk back and see if there is an assignment to the variable within the same scope. |
||
293 | for ($i = ($stackPtr - 1); $i >= $scopeStart; $i--) { |
||
294 | View Code Duplication | if ($tokens[$i]['code'] === \T_CLOSE_CURLY_BRACKET |
|
295 | && isset($tokens[$i]['scope_condition']) |
||
296 | && isset($skipPast[$tokens[$tokens[$i]['scope_condition']]['type']]) |
||
297 | ) { |
||
298 | // Skip past functions, classes etc. |
||
299 | $i = $tokens[$i]['scope_condition']; |
||
300 | continue; |
||
301 | } |
||
302 | |||
303 | View Code Duplication | if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$php_errormsg') { |
|
304 | continue; |
||
305 | } |
||
306 | |||
307 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); |
||
308 | |||
309 | View Code Duplication | if ($nextNonEmpty !== false |
|
310 | && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true |
||
311 | ) { |
||
312 | return false; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | return true; |
||
317 | } |
||
318 | } |
||
319 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.