Conditions | 88 |
Paths | 42 |
Total Lines | 438 |
Code Lines | 240 |
Lines | 37 |
Ratio | 8.45 % |
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 |
||
257 | protected function processPattern($patternInfo, File $phpcsFile, $stackPtr) |
||
258 | { |
||
259 | $tokens = $phpcsFile->getTokens(); |
||
260 | $pattern = $patternInfo['pattern']; |
||
261 | $patternCode = $patternInfo['pattern_code']; |
||
262 | $errors = array(); |
||
263 | $found = ''; |
||
264 | |||
265 | $ignoreTokens = array(T_WHITESPACE); |
||
266 | if ($this->ignoreComments === true) { |
||
267 | $ignoreTokens |
||
268 | = array_merge($ignoreTokens, Tokens::$commentTokens); |
||
269 | } |
||
270 | |||
271 | $origStackPtr = $stackPtr; |
||
272 | $hasError = false; |
||
273 | |||
274 | if ($patternInfo['listen_pos'] > 0) { |
||
275 | $stackPtr--; |
||
276 | |||
277 | for ($i = ($patternInfo['listen_pos'] - 1); $i >= 0; $i--) { |
||
278 | if ($pattern[$i]['type'] === 'token') { |
||
279 | if ($pattern[$i]['token'] === T_WHITESPACE) { |
||
280 | if ($tokens[$stackPtr]['code'] === T_WHITESPACE) { |
||
281 | $found = $tokens[$stackPtr]['content'].$found; |
||
282 | } |
||
283 | |||
284 | // Only check the size of the whitespace if this is not |
||
285 | // the first token. We don't care about the size of |
||
286 | // leading whitespace, just that there is some. |
||
287 | if ($i !== 0) { |
||
288 | if ($tokens[$stackPtr]['content'] !== $pattern[$i]['value']) { |
||
289 | $hasError = true; |
||
290 | } |
||
291 | } |
||
292 | } else { |
||
293 | // Check to see if this important token is the same as the |
||
294 | // previous important token in the pattern. If it is not, |
||
295 | // then the pattern cannot be for this piece of code. |
||
296 | $prev = $phpcsFile->findPrevious( |
||
297 | $ignoreTokens, |
||
298 | $stackPtr, |
||
299 | null, |
||
300 | true |
||
301 | ); |
||
302 | |||
303 | View Code Duplication | if ($prev === false |
|
304 | || $tokens[$prev]['code'] !== $pattern[$i]['token'] |
||
305 | ) { |
||
306 | return false; |
||
307 | } |
||
308 | |||
309 | // If we skipped past some whitespace tokens, then add them |
||
310 | // to the found string. |
||
311 | $tokenContent = $phpcsFile->getTokensAsString( |
||
312 | ($prev + 1), |
||
313 | ($stackPtr - $prev - 1) |
||
314 | ); |
||
315 | |||
316 | $found = $tokens[$prev]['content'].$tokenContent.$found; |
||
317 | |||
318 | if (isset($pattern[($i - 1)]) === true |
||
319 | && $pattern[($i - 1)]['type'] === 'skip' |
||
320 | ) { |
||
321 | $stackPtr = $prev; |
||
322 | } else { |
||
323 | $stackPtr = ($prev - 1); |
||
324 | } |
||
325 | }//end if |
||
326 | } else if ($pattern[$i]['type'] === 'skip') { |
||
327 | // Skip to next piece of relevant code. |
||
328 | if ($pattern[$i]['to'] === 'parenthesis_closer') { |
||
329 | $to = 'parenthesis_opener'; |
||
330 | } else { |
||
331 | $to = 'scope_opener'; |
||
332 | } |
||
333 | |||
334 | // Find the previous opener. |
||
335 | $next = $phpcsFile->findPrevious( |
||
336 | $ignoreTokens, |
||
337 | $stackPtr, |
||
338 | null, |
||
339 | true |
||
340 | ); |
||
341 | |||
342 | if ($next === false || isset($tokens[$next][$to]) === false) { |
||
343 | // If there was not opener, then we must be |
||
344 | // using the wrong pattern. |
||
345 | return false; |
||
346 | } |
||
347 | |||
348 | if ($to === 'parenthesis_opener') { |
||
349 | $found = '{'.$found; |
||
350 | } else { |
||
351 | $found = '('.$found; |
||
352 | } |
||
353 | |||
354 | $found = '...'.$found; |
||
355 | |||
356 | // Skip to the opening token. |
||
357 | $stackPtr = ($tokens[$next][$to] - 1); |
||
358 | } else if ($pattern[$i]['type'] === 'string') { |
||
359 | $found = 'abc'; |
||
360 | } else if ($pattern[$i]['type'] === 'newline') { |
||
361 | if ($this->ignoreComments === true |
||
362 | && isset(Tokens::$commentTokens[$tokens[$stackPtr]['code']]) === true |
||
363 | ) { |
||
364 | $startComment = $phpcsFile->findPrevious( |
||
365 | Tokens::$commentTokens, |
||
366 | ($stackPtr - 1), |
||
367 | null, |
||
368 | true |
||
369 | ); |
||
370 | |||
371 | if ($tokens[$startComment]['line'] !== $tokens[($startComment + 1)]['line']) { |
||
372 | $startComment++; |
||
373 | } |
||
374 | |||
375 | $tokenContent = $phpcsFile->getTokensAsString( |
||
376 | $startComment, |
||
377 | ($stackPtr - $startComment + 1) |
||
378 | ); |
||
379 | |||
380 | $found = $tokenContent.$found; |
||
381 | $stackPtr = ($startComment - 1); |
||
382 | } |
||
383 | |||
384 | if ($tokens[$stackPtr]['code'] === T_WHITESPACE) { |
||
385 | if ($tokens[$stackPtr]['content'] !== $phpcsFile->eolChar) { |
||
386 | $found = $tokens[$stackPtr]['content'].$found; |
||
387 | |||
388 | // This may just be an indent that comes after a newline |
||
389 | // so check the token before to make sure. If it is a newline, we |
||
390 | // can ignore the error here. |
||
391 | if (($tokens[($stackPtr - 1)]['content'] !== $phpcsFile->eolChar) |
||
392 | && ($this->ignoreComments === true |
||
393 | && isset(Tokens::$commentTokens[$tokens[($stackPtr - 1)]['code']]) === false) |
||
394 | ) { |
||
395 | $hasError = true; |
||
396 | } else { |
||
397 | $stackPtr--; |
||
398 | } |
||
399 | } else { |
||
400 | $found = 'EOL'.$found; |
||
401 | } |
||
402 | } else { |
||
403 | $found = $tokens[$stackPtr]['content'].$found; |
||
404 | $hasError = true; |
||
405 | }//end if |
||
406 | |||
407 | if ($hasError === false && $pattern[($i - 1)]['type'] !== 'newline') { |
||
408 | // Make sure they only have 1 newline. |
||
409 | $prev = $phpcsFile->findPrevious($ignoreTokens, ($stackPtr - 1), null, true); |
||
410 | View Code Duplication | if ($prev !== false && $tokens[$prev]['line'] !== $tokens[$stackPtr]['line']) { |
|
411 | $hasError = true; |
||
412 | } |
||
413 | } |
||
414 | }//end if |
||
415 | }//end for |
||
416 | }//end if |
||
417 | |||
418 | $stackPtr = $origStackPtr; |
||
419 | $lastAddedStackPtr = null; |
||
420 | $patternLen = count($pattern); |
||
421 | |||
422 | for ($i = $patternInfo['listen_pos']; $i < $patternLen; $i++) { |
||
423 | if (isset($tokens[$stackPtr]) === false) { |
||
424 | break; |
||
425 | } |
||
426 | |||
427 | if ($pattern[$i]['type'] === 'token') { |
||
428 | if ($pattern[$i]['token'] === T_WHITESPACE) { |
||
429 | if ($this->ignoreComments === true) { |
||
430 | // If we are ignoring comments, check to see if this current |
||
431 | // token is a comment. If so skip it. |
||
432 | if (isset(Tokens::$commentTokens[$tokens[$stackPtr]['code']]) === true) { |
||
433 | continue; |
||
434 | } |
||
435 | |||
436 | // If the next token is a comment, the we need to skip the |
||
437 | // current token as we should allow a space before a |
||
438 | // comment for readability. |
||
439 | if (isset($tokens[($stackPtr + 1)]) === true |
||
440 | && isset(Tokens::$commentTokens[$tokens[($stackPtr + 1)]['code']]) === true |
||
441 | ) { |
||
442 | continue; |
||
443 | } |
||
444 | } |
||
445 | |||
446 | $tokenContent = ''; |
||
447 | if ($tokens[$stackPtr]['code'] === T_WHITESPACE) { |
||
448 | if (isset($pattern[($i + 1)]) === false) { |
||
449 | // This is the last token in the pattern, so just compare |
||
450 | // the next token of content. |
||
451 | $tokenContent = $tokens[$stackPtr]['content']; |
||
452 | } else { |
||
453 | // Get all the whitespace to the next token. |
||
454 | $next = $phpcsFile->findNext( |
||
455 | Tokens::$emptyTokens, |
||
456 | $stackPtr, |
||
457 | null, |
||
458 | true |
||
459 | ); |
||
460 | |||
461 | $tokenContent = $phpcsFile->getTokensAsString( |
||
462 | $stackPtr, |
||
463 | ($next - $stackPtr) |
||
464 | ); |
||
465 | |||
466 | $lastAddedStackPtr = $stackPtr; |
||
467 | $stackPtr = $next; |
||
468 | }//end if |
||
469 | |||
470 | if ($stackPtr !== $lastAddedStackPtr) { |
||
471 | $found .= $tokenContent; |
||
472 | } |
||
473 | } else { |
||
474 | if ($stackPtr !== $lastAddedStackPtr) { |
||
475 | $found .= $tokens[$stackPtr]['content']; |
||
476 | $lastAddedStackPtr = $stackPtr; |
||
477 | } |
||
478 | }//end if |
||
479 | |||
480 | if (isset($pattern[($i + 1)]) === true |
||
481 | && $pattern[($i + 1)]['type'] === 'skip' |
||
482 | ) { |
||
483 | // The next token is a skip token, so we just need to make |
||
484 | // sure the whitespace we found has *at least* the |
||
485 | // whitespace required. |
||
486 | if (strpos($tokenContent, $pattern[$i]['value']) !== 0) { |
||
487 | $hasError = true; |
||
488 | } |
||
489 | } else { |
||
490 | if ($tokenContent !== $pattern[$i]['value']) { |
||
491 | $hasError = true; |
||
492 | } |
||
493 | } |
||
494 | } else { |
||
495 | // Check to see if this important token is the same as the |
||
496 | // next important token in the pattern. If it is not, then |
||
497 | // the pattern cannot be for this piece of code. |
||
498 | $next = $phpcsFile->findNext( |
||
499 | $ignoreTokens, |
||
500 | $stackPtr, |
||
501 | null, |
||
502 | true |
||
503 | ); |
||
504 | |||
505 | View Code Duplication | if ($next === false |
|
506 | || $tokens[$next]['code'] !== $pattern[$i]['token'] |
||
507 | ) { |
||
508 | // The next important token did not match the pattern. |
||
509 | return false; |
||
510 | } |
||
511 | |||
512 | if ($lastAddedStackPtr !== null) { |
||
513 | View Code Duplication | if (($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET |
|
514 | || $tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) |
||
515 | && isset($tokens[$next]['scope_condition']) === true |
||
516 | && $tokens[$next]['scope_condition'] > $lastAddedStackPtr |
||
517 | ) { |
||
518 | // This is a brace, but the owner of it is after the current |
||
519 | // token, which means it does not belong to any token in |
||
520 | // our pattern. This means the pattern is not for us. |
||
521 | return false; |
||
522 | } |
||
523 | |||
524 | View Code Duplication | if (($tokens[$next]['code'] === T_OPEN_PARENTHESIS |
|
525 | || $tokens[$next]['code'] === T_CLOSE_PARENTHESIS) |
||
526 | && isset($tokens[$next]['parenthesis_owner']) === true |
||
527 | && $tokens[$next]['parenthesis_owner'] > $lastAddedStackPtr |
||
528 | ) { |
||
529 | // This is a bracket, but the owner of it is after the current |
||
530 | // token, which means it does not belong to any token in |
||
531 | // our pattern. This means the pattern is not for us. |
||
532 | return false; |
||
533 | } |
||
534 | }//end if |
||
535 | |||
536 | // If we skipped past some whitespace tokens, then add them |
||
537 | // to the found string. |
||
538 | if (($next - $stackPtr) > 0) { |
||
539 | $hasComment = false; |
||
540 | for ($j = $stackPtr; $j < $next; $j++) { |
||
541 | $found .= $tokens[$j]['content']; |
||
542 | if (isset(Tokens::$commentTokens[$tokens[$j]['code']]) === true) { |
||
543 | $hasComment = true; |
||
544 | } |
||
545 | } |
||
546 | |||
547 | // If we are not ignoring comments, this additional |
||
548 | // whitespace or comment is not allowed. If we are |
||
549 | // ignoring comments, there needs to be at least one |
||
550 | // comment for this to be allowed. |
||
551 | if ($this->ignoreComments === false |
||
552 | || ($this->ignoreComments === true |
||
553 | && $hasComment === false) |
||
554 | ) { |
||
555 | $hasError = true; |
||
556 | } |
||
557 | |||
558 | // Even when ignoring comments, we are not allowed to include |
||
559 | // newlines without the pattern specifying them, so |
||
560 | // everything should be on the same line. |
||
561 | View Code Duplication | if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { |
|
562 | $hasError = true; |
||
563 | } |
||
564 | }//end if |
||
565 | |||
566 | if ($next !== $lastAddedStackPtr) { |
||
567 | $found .= $tokens[$next]['content']; |
||
568 | $lastAddedStackPtr = $next; |
||
569 | } |
||
570 | |||
571 | if (isset($pattern[($i + 1)]) === true |
||
572 | && $pattern[($i + 1)]['type'] === 'skip' |
||
573 | ) { |
||
574 | $stackPtr = $next; |
||
575 | } else { |
||
576 | $stackPtr = ($next + 1); |
||
577 | } |
||
578 | }//end if |
||
579 | } else if ($pattern[$i]['type'] === 'skip') { |
||
580 | if ($pattern[$i]['to'] === 'unknown') { |
||
581 | $next = $phpcsFile->findNext( |
||
582 | $pattern[($i + 1)]['token'], |
||
583 | $stackPtr |
||
584 | ); |
||
585 | |||
586 | if ($next === false) { |
||
587 | // Couldn't find the next token, so we must |
||
588 | // be using the wrong pattern. |
||
589 | return false; |
||
590 | } |
||
591 | |||
592 | $found .= '...'; |
||
593 | $stackPtr = $next; |
||
594 | } else { |
||
595 | // Find the previous opener. |
||
596 | $next = $phpcsFile->findPrevious( |
||
597 | Tokens::$blockOpeners, |
||
598 | $stackPtr |
||
599 | ); |
||
600 | |||
601 | if ($next === false |
||
602 | || isset($tokens[$next][$pattern[$i]['to']]) === false |
||
603 | ) { |
||
604 | // If there was not opener, then we must |
||
605 | // be using the wrong pattern. |
||
606 | return false; |
||
607 | } |
||
608 | |||
609 | $found .= '...'; |
||
610 | if ($pattern[$i]['to'] === 'parenthesis_closer') { |
||
611 | $found .= ')'; |
||
612 | } else { |
||
613 | $found .= '}'; |
||
614 | } |
||
615 | |||
616 | // Skip to the closing token. |
||
617 | $stackPtr = ($tokens[$next][$pattern[$i]['to']] + 1); |
||
618 | }//end if |
||
619 | } else if ($pattern[$i]['type'] === 'string') { |
||
620 | if ($tokens[$stackPtr]['code'] !== T_STRING) { |
||
621 | $hasError = true; |
||
622 | } |
||
623 | |||
624 | if ($stackPtr !== $lastAddedStackPtr) { |
||
625 | $found .= 'abc'; |
||
626 | $lastAddedStackPtr = $stackPtr; |
||
627 | } |
||
628 | |||
629 | $stackPtr++; |
||
630 | } else if ($pattern[$i]['type'] === 'newline') { |
||
631 | // Find the next token that contains a newline character. |
||
632 | $newline = 0; |
||
633 | for ($j = $stackPtr; $j < $phpcsFile->numTokens; $j++) { |
||
634 | if (strpos($tokens[$j]['content'], $phpcsFile->eolChar) !== false) { |
||
635 | $newline = $j; |
||
636 | break; |
||
637 | } |
||
638 | } |
||
639 | |||
640 | if ($newline === 0) { |
||
641 | // We didn't find a newline character in the rest of the file. |
||
642 | $next = ($phpcsFile->numTokens - 1); |
||
643 | $hasError = true; |
||
644 | } else { |
||
645 | if ($this->ignoreComments === false) { |
||
646 | // The newline character cannot be part of a comment. |
||
647 | if (isset(Tokens::$commentTokens[$tokens[$newline]['code']]) === true) { |
||
648 | $hasError = true; |
||
649 | } |
||
650 | } |
||
651 | |||
652 | if ($newline === $stackPtr) { |
||
653 | $next = ($stackPtr + 1); |
||
654 | } else { |
||
655 | // Check that there were no significant tokens that we |
||
656 | // skipped over to find our newline character. |
||
657 | $next = $phpcsFile->findNext( |
||
658 | $ignoreTokens, |
||
659 | $stackPtr, |
||
660 | null, |
||
661 | true |
||
662 | ); |
||
663 | |||
664 | if ($next < $newline) { |
||
665 | // We skipped a non-ignored token. |
||
666 | $hasError = true; |
||
667 | } else { |
||
668 | $next = ($newline + 1); |
||
669 | } |
||
670 | } |
||
671 | }//end if |
||
672 | |||
673 | if ($stackPtr !== $lastAddedStackPtr) { |
||
674 | $found .= $phpcsFile->getTokensAsString( |
||
675 | $stackPtr, |
||
676 | ($next - $stackPtr) |
||
677 | ); |
||
678 | |||
679 | $diff = ($next - $stackPtr); |
||
680 | $lastAddedStackPtr = ($next - 1); |
||
681 | } |
||
682 | |||
683 | $stackPtr = $next; |
||
684 | }//end if |
||
685 | }//end for |
||
686 | |||
687 | if ($hasError === true) { |
||
688 | $error = $this->prepareError($found, $patternCode); |
||
689 | $errors[$origStackPtr] = $error; |
||
690 | } |
||
691 | |||
692 | return $errors; |
||
693 | |||
694 | }//end processPattern() |
||
695 | |||
941 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.