| Conditions | 129 |
| Paths | > 20000 |
| Total Lines | 680 |
| Code Lines | 372 |
| Lines | 114 |
| Ratio | 16.76 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 448 | protected function tokenize($string) |
||
| 449 | { |
||
| 450 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 451 | echo "\t*** START PHP TOKENIZING ***".PHP_EOL; |
||
| 452 | $isWin = false; |
||
| 453 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
| 454 | $isWin = true; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | $tokens = @token_get_all($string); |
||
| 459 | $finalTokens = array(); |
||
| 460 | |||
| 461 | $newStackPtr = 0; |
||
| 462 | $numTokens = count($tokens); |
||
| 463 | $lastNotEmptyToken = 0; |
||
| 464 | |||
| 465 | $insideInlineIf = array(); |
||
| 466 | $insideUseGroup = false; |
||
| 467 | |||
| 468 | $commentTokenizer = new Comment(); |
||
| 469 | |||
| 470 | for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) { |
||
| 471 | $token = (array) $tokens[$stackPtr]; |
||
| 472 | $tokenIsArray = isset($token[1]); |
||
| 473 | |||
| 474 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 475 | if ($tokenIsArray === true) { |
||
| 476 | $type = token_name($token[0]); |
||
| 477 | $content = Util\Common::prepareForOutput($token[1]); |
||
| 478 | } else { |
||
| 479 | $newToken = self::resolveSimpleToken($token[0]); |
||
| 480 | $type = $newToken['type']; |
||
| 481 | $content = Util\Common::prepareForOutput($token[0]); |
||
| 482 | } |
||
| 483 | |||
| 484 | echo "\tProcess token "; |
||
| 485 | if ($tokenIsArray === true) { |
||
| 486 | echo "[$stackPtr]"; |
||
| 487 | } else { |
||
| 488 | echo " $stackPtr "; |
||
| 489 | } |
||
| 490 | |||
| 491 | echo ": $type => $content"; |
||
| 492 | }//end if |
||
| 493 | |||
| 494 | if ($newStackPtr > 0 && $finalTokens[($newStackPtr - 1)]['code'] !== T_WHITESPACE) { |
||
| 495 | $lastNotEmptyToken = ($newStackPtr - 1); |
||
| 496 | } |
||
| 497 | |||
| 498 | /* |
||
| 499 | If we are using \r\n newline characters, the \r and \n are sometimes |
||
| 500 | split over two tokens. This normally occurs after comments. We need |
||
| 501 | to merge these two characters together so that our line endings are |
||
| 502 | consistent for all lines. |
||
| 503 | */ |
||
| 504 | |||
| 505 | if ($tokenIsArray === true && substr($token[1], -1) === "\r") { |
||
| 506 | if (isset($tokens[($stackPtr + 1)]) === true |
||
| 507 | && is_array($tokens[($stackPtr + 1)]) === true |
||
| 508 | && $tokens[($stackPtr + 1)][1][0] === "\n" |
||
| 509 | ) { |
||
| 510 | $token[1] .= "\n"; |
||
| 511 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 512 | if ($isWin === true) { |
||
|
|
|||
| 513 | echo '\n'; |
||
| 514 | } else { |
||
| 515 | echo "\033[30;1m\\n\033[0m"; |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | if ($tokens[($stackPtr + 1)][1] === "\n") { |
||
| 520 | // This token's content has been merged into the previous, |
||
| 521 | // so we can skip it. |
||
| 522 | $tokens[($stackPtr + 1)] = ''; |
||
| 523 | } else { |
||
| 524 | $tokens[($stackPtr + 1)][1] = substr($tokens[($stackPtr + 1)][1], 1); |
||
| 525 | } |
||
| 526 | } |
||
| 527 | }//end if |
||
| 528 | |||
| 529 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 530 | echo PHP_EOL; |
||
| 531 | } |
||
| 532 | |||
| 533 | /* |
||
| 534 | Parse doc blocks into something that can be easily iterated over. |
||
| 535 | */ |
||
| 536 | |||
| 537 | if ($tokenIsArray === true && $token[0] === T_DOC_COMMENT) { |
||
| 538 | $commentTokens = $commentTokenizer->tokenizeString($token[1], $this->eolChar, $newStackPtr); |
||
| 539 | foreach ($commentTokens as $commentToken) { |
||
| 540 | $finalTokens[$newStackPtr] = $commentToken; |
||
| 541 | $newStackPtr++; |
||
| 542 | } |
||
| 543 | |||
| 544 | continue; |
||
| 545 | } |
||
| 546 | |||
| 547 | /* |
||
| 548 | If this is a double quoted string, PHP will tokenize the whole |
||
| 549 | thing which causes problems with the scope map when braces are |
||
| 550 | within the string. So we need to merge the tokens together to |
||
| 551 | provide a single string. |
||
| 552 | */ |
||
| 553 | |||
| 554 | if ($tokenIsArray === false && ($token[0] === '"' || $token[0] === 'b"')) { |
||
| 555 | // Binary casts need a special token. |
||
| 556 | View Code Duplication | if ($token[0] === 'b"') { |
|
| 557 | $finalTokens[$newStackPtr] = array( |
||
| 558 | 'code' => T_BINARY_CAST, |
||
| 559 | 'type' => 'T_BINARY_CAST', |
||
| 560 | 'content' => 'b', |
||
| 561 | ); |
||
| 562 | $newStackPtr++; |
||
| 563 | } |
||
| 564 | |||
| 565 | $tokenContent = '"'; |
||
| 566 | $nestedVars = array(); |
||
| 567 | for ($i = ($stackPtr + 1); $i < $numTokens; $i++) { |
||
| 568 | $subToken = (array) $tokens[$i]; |
||
| 569 | $subTokenIsArray = isset($subToken[1]); |
||
| 570 | |||
| 571 | if ($subTokenIsArray === true) { |
||
| 572 | $tokenContent .= $subToken[1]; |
||
| 573 | if ($subToken[1] === '{' |
||
| 574 | && $subToken[0] !== T_ENCAPSED_AND_WHITESPACE |
||
| 575 | ) { |
||
| 576 | $nestedVars[] = $i; |
||
| 577 | } |
||
| 578 | } else { |
||
| 579 | $tokenContent .= $subToken[0]; |
||
| 580 | if ($subToken[0] === '}') { |
||
| 581 | array_pop($nestedVars); |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | if ($subTokenIsArray === false |
||
| 586 | && $subToken[0] === '"' |
||
| 587 | && empty($nestedVars) === true |
||
| 588 | ) { |
||
| 589 | // We found the other end of the double quoted string. |
||
| 590 | break; |
||
| 591 | } |
||
| 592 | }//end for |
||
| 593 | |||
| 594 | $stackPtr = $i; |
||
| 595 | |||
| 596 | // Convert each line within the double quoted string to a |
||
| 597 | // new token, so it conforms with other multiple line tokens. |
||
| 598 | $tokenLines = explode($this->eolChar, $tokenContent); |
||
| 599 | $numLines = count($tokenLines); |
||
| 600 | $newToken = array(); |
||
| 601 | |||
| 602 | for ($j = 0; $j < $numLines; $j++) { |
||
| 603 | $newToken['content'] = $tokenLines[$j]; |
||
| 604 | View Code Duplication | if ($j === ($numLines - 1)) { |
|
| 605 | if ($tokenLines[$j] === '') { |
||
| 606 | break; |
||
| 607 | } |
||
| 608 | } else { |
||
| 609 | $newToken['content'] .= $this->eolChar; |
||
| 610 | } |
||
| 611 | |||
| 612 | $newToken['code'] = T_DOUBLE_QUOTED_STRING; |
||
| 613 | $newToken['type'] = 'T_DOUBLE_QUOTED_STRING'; |
||
| 614 | $finalTokens[$newStackPtr] = $newToken; |
||
| 615 | $newStackPtr++; |
||
| 616 | } |
||
| 617 | |||
| 618 | // Continue, as we're done with this token. |
||
| 619 | continue; |
||
| 620 | }//end if |
||
| 621 | |||
| 622 | /* |
||
| 623 | If this is a heredoc, PHP will tokenize the whole |
||
| 624 | thing which causes problems when heredocs don't |
||
| 625 | contain real PHP code, which is almost never. |
||
| 626 | We want to leave the start and end heredoc tokens |
||
| 627 | alone though. |
||
| 628 | */ |
||
| 629 | |||
| 630 | if ($tokenIsArray === true && $token[0] === T_START_HEREDOC) { |
||
| 631 | // Add the start heredoc token to the final array. |
||
| 632 | $finalTokens[$newStackPtr] = self::standardiseToken($token); |
||
| 633 | |||
| 634 | // Check if this is actually a nowdoc and use a different token |
||
| 635 | // to help the sniffs. |
||
| 636 | $nowdoc = false; |
||
| 637 | if ($token[1][3] === "'") { |
||
| 638 | $finalTokens[$newStackPtr]['code'] = T_START_NOWDOC; |
||
| 639 | $finalTokens[$newStackPtr]['type'] = 'T_START_NOWDOC'; |
||
| 640 | $nowdoc = true; |
||
| 641 | } |
||
| 642 | |||
| 643 | $tokenContent = ''; |
||
| 644 | for ($i = ($stackPtr + 1); $i < $numTokens; $i++) { |
||
| 645 | $subTokenIsArray = is_array($tokens[$i]); |
||
| 646 | if ($subTokenIsArray === true |
||
| 647 | && $tokens[$i][0] === T_END_HEREDOC |
||
| 648 | ) { |
||
| 649 | // We found the other end of the heredoc. |
||
| 650 | break; |
||
| 651 | } |
||
| 652 | |||
| 653 | if ($subTokenIsArray === true) { |
||
| 654 | $tokenContent .= $tokens[$i][1]; |
||
| 655 | } else { |
||
| 656 | $tokenContent .= $tokens[$i]; |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | if ($i === $numTokens) { |
||
| 661 | // We got to the end of the file and never |
||
| 662 | // found the closing token, so this probably wasn't |
||
| 663 | // a heredoc. |
||
| 664 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 665 | $type = $finalTokens[$newStackPtr]['type']; |
||
| 666 | echo "\t\t* failed to find the end of the here/nowdoc".PHP_EOL; |
||
| 667 | echo "\t\t* token $stackPtr changed from $type to T_STRING".PHP_EOL; |
||
| 668 | } |
||
| 669 | |||
| 670 | $finalTokens[$newStackPtr]['code'] = T_STRING; |
||
| 671 | $finalTokens[$newStackPtr]['type'] = 'T_STRING'; |
||
| 672 | $newStackPtr++; |
||
| 673 | continue; |
||
| 674 | } |
||
| 675 | |||
| 676 | $stackPtr = $i; |
||
| 677 | $newStackPtr++; |
||
| 678 | |||
| 679 | // Convert each line within the heredoc to a |
||
| 680 | // new token, so it conforms with other multiple line tokens. |
||
| 681 | $tokenLines = explode($this->eolChar, $tokenContent); |
||
| 682 | $numLines = count($tokenLines); |
||
| 683 | $newToken = array(); |
||
| 684 | |||
| 685 | for ($j = 0; $j < $numLines; $j++) { |
||
| 686 | $newToken['content'] = $tokenLines[$j]; |
||
| 687 | View Code Duplication | if ($j === ($numLines - 1)) { |
|
| 688 | if ($tokenLines[$j] === '') { |
||
| 689 | break; |
||
| 690 | } |
||
| 691 | } else { |
||
| 692 | $newToken['content'] .= $this->eolChar; |
||
| 693 | } |
||
| 694 | |||
| 695 | if ($nowdoc === true) { |
||
| 696 | $newToken['code'] = T_NOWDOC; |
||
| 697 | $newToken['type'] = 'T_NOWDOC'; |
||
| 698 | } else { |
||
| 699 | $newToken['code'] = T_HEREDOC; |
||
| 700 | $newToken['type'] = 'T_HEREDOC'; |
||
| 701 | } |
||
| 702 | |||
| 703 | $finalTokens[$newStackPtr] = $newToken; |
||
| 704 | $newStackPtr++; |
||
| 705 | }//end for |
||
| 706 | |||
| 707 | // Add the end heredoc token to the final array. |
||
| 708 | $finalTokens[$newStackPtr] = self::standardiseToken($tokens[$stackPtr]); |
||
| 709 | |||
| 710 | if ($nowdoc === true) { |
||
| 711 | $finalTokens[$newStackPtr]['code'] = T_END_NOWDOC; |
||
| 712 | $finalTokens[$newStackPtr]['type'] = 'T_END_NOWDOC'; |
||
| 713 | $nowdoc = true; |
||
| 714 | } |
||
| 715 | |||
| 716 | $newStackPtr++; |
||
| 717 | |||
| 718 | // Continue, as we're done with this token. |
||
| 719 | continue; |
||
| 720 | }//end if |
||
| 721 | |||
| 722 | /* |
||
| 723 | Before PHP 5.6, the ... operator was tokenized as three |
||
| 724 | T_STRING_CONCAT tokens in a row. So look for and combine |
||
| 725 | these tokens in earlier versions. |
||
| 726 | */ |
||
| 727 | |||
| 728 | if ($tokenIsArray === false |
||
| 729 | && $token[0] === '.' |
||
| 730 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 731 | && isset($tokens[($stackPtr + 2)]) === true |
||
| 732 | && $tokens[($stackPtr + 1)] === '.' |
||
| 733 | && $tokens[($stackPtr + 2)] === '.' |
||
| 734 | ) { |
||
| 735 | $newToken = array(); |
||
| 736 | $newToken['code'] = T_ELLIPSIS; |
||
| 737 | $newToken['type'] = 'T_ELLIPSIS'; |
||
| 738 | $newToken['content'] = '...'; |
||
| 739 | $finalTokens[$newStackPtr] = $newToken; |
||
| 740 | |||
| 741 | $newStackPtr++; |
||
| 742 | $stackPtr += 2; |
||
| 743 | continue; |
||
| 744 | } |
||
| 745 | |||
| 746 | /* |
||
| 747 | Before PHP 5.6, the ** operator was tokenized as two |
||
| 748 | T_MULTIPLY tokens in a row. So look for and combine |
||
| 749 | these tokens in earlier versions. |
||
| 750 | */ |
||
| 751 | |||
| 752 | View Code Duplication | if ($tokenIsArray === false |
|
| 753 | && $token[0] === '*' |
||
| 754 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 755 | && $tokens[($stackPtr + 1)] === '*' |
||
| 756 | ) { |
||
| 757 | $newToken = array(); |
||
| 758 | $newToken['code'] = T_POW; |
||
| 759 | $newToken['type'] = 'T_POW'; |
||
| 760 | $newToken['content'] = '**'; |
||
| 761 | $finalTokens[$newStackPtr] = $newToken; |
||
| 762 | |||
| 763 | $newStackPtr++; |
||
| 764 | $stackPtr++; |
||
| 765 | continue; |
||
| 766 | } |
||
| 767 | |||
| 768 | /* |
||
| 769 | Before PHP 5.6, the **= operator was tokenized as |
||
| 770 | T_MULTIPLY followed by T_MUL_EQUAL. So look for and combine |
||
| 771 | these tokens in earlier versions. |
||
| 772 | */ |
||
| 773 | |||
| 774 | if ($tokenIsArray === false |
||
| 775 | && $token[0] === '*' |
||
| 776 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 777 | && is_array($tokens[($stackPtr + 1)]) === true |
||
| 778 | && $tokens[($stackPtr + 1)][1] === '*=' |
||
| 779 | ) { |
||
| 780 | $newToken = array(); |
||
| 781 | $newToken['code'] = T_POW_EQUAL; |
||
| 782 | $newToken['type'] = 'T_POW_EQUAL'; |
||
| 783 | $newToken['content'] = '**='; |
||
| 784 | $finalTokens[$newStackPtr] = $newToken; |
||
| 785 | |||
| 786 | $newStackPtr++; |
||
| 787 | $stackPtr++; |
||
| 788 | continue; |
||
| 789 | } |
||
| 790 | |||
| 791 | /* |
||
| 792 | Before PHP 7, the ?? operator was tokenized as |
||
| 793 | T_INLINE_THEN followed by T_INLINE_THEN. |
||
| 794 | So look for and combine these tokens in earlier versions. |
||
| 795 | */ |
||
| 796 | |||
| 797 | View Code Duplication | if ($tokenIsArray === false |
|
| 798 | && $token[0] === '?' |
||
| 799 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 800 | && $tokens[($stackPtr + 1)][0] === '?' |
||
| 801 | ) { |
||
| 802 | $newToken = array(); |
||
| 803 | $newToken['code'] = T_COALESCE; |
||
| 804 | $newToken['type'] = 'T_COALESCE'; |
||
| 805 | $newToken['content'] = '??'; |
||
| 806 | $finalTokens[$newStackPtr] = $newToken; |
||
| 807 | |||
| 808 | $newStackPtr++; |
||
| 809 | $stackPtr++; |
||
| 810 | continue; |
||
| 811 | } |
||
| 812 | |||
| 813 | /* |
||
| 814 | Before PHP 7, the <=> operator was tokenized as |
||
| 815 | T_IS_SMALLER_OR_EQUAL followed by T_GREATER_THAN. |
||
| 816 | So look for and combine these tokens in earlier versions. |
||
| 817 | */ |
||
| 818 | |||
| 819 | View Code Duplication | if ($tokenIsArray === true |
|
| 820 | && $token[0] === T_IS_SMALLER_OR_EQUAL |
||
| 821 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 822 | && $tokens[($stackPtr + 1)][0] === '>' |
||
| 823 | ) { |
||
| 824 | $newToken = array(); |
||
| 825 | $newToken['code'] = T_SPACESHIP; |
||
| 826 | $newToken['type'] = 'T_SPACESHIP'; |
||
| 827 | $newToken['content'] = '<=>'; |
||
| 828 | $finalTokens[$newStackPtr] = $newToken; |
||
| 829 | |||
| 830 | $newStackPtr++; |
||
| 831 | $stackPtr++; |
||
| 832 | continue; |
||
| 833 | } |
||
| 834 | |||
| 835 | /* |
||
| 836 | Emulate traits in PHP versions less than 5.4. |
||
| 837 | */ |
||
| 838 | |||
| 839 | if ($tokenIsArray === true |
||
| 840 | && $token[0] === T_STRING |
||
| 841 | && strtolower($token[1]) === 'trait' |
||
| 842 | && $tokens[($stackPtr - 1)][0] !== T_OBJECT_OPERATOR |
||
| 843 | ) { |
||
| 844 | $finalTokens[$newStackPtr] = array( |
||
| 845 | 'content' => $token[1], |
||
| 846 | 'code' => T_TRAIT, |
||
| 847 | 'type' => 'T_TRAIT', |
||
| 848 | ); |
||
| 849 | |||
| 850 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 851 | echo "\t\t* token $stackPtr changed from T_STRING to T_TRAIT".PHP_EOL; |
||
| 852 | } |
||
| 853 | |||
| 854 | $newStackPtr++; |
||
| 855 | continue; |
||
| 856 | } |
||
| 857 | |||
| 858 | /* |
||
| 859 | PHP doesn't assign a token to goto labels, so we have to. |
||
| 860 | These are just string tokens with a single colon after them. Double |
||
| 861 | colons are already tokenized and so don't interfere with this check. |
||
| 862 | But we do have to account for CASE statements, that look just like |
||
| 863 | goto labels. |
||
| 864 | */ |
||
| 865 | |||
| 866 | if ($tokenIsArray === true |
||
| 867 | && $token[0] === T_STRING |
||
| 868 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 869 | && $tokens[($stackPtr + 1)] === ':' |
||
| 870 | && $tokens[($stackPtr - 1)][0] !== T_PAAMAYIM_NEKUDOTAYIM |
||
| 871 | ) { |
||
| 872 | $stopTokens = array( |
||
| 873 | T_CASE => true, |
||
| 874 | T_SEMICOLON => true, |
||
| 875 | T_OPEN_CURLY_BRACKET => true, |
||
| 876 | T_INLINE_THEN => true, |
||
| 877 | ); |
||
| 878 | |||
| 879 | for ($x = ($newStackPtr - 1); $x > 0; $x--) { |
||
| 880 | if (isset($stopTokens[$finalTokens[$x]['code']]) === true) { |
||
| 881 | break; |
||
| 882 | } |
||
| 883 | } |
||
| 884 | |||
| 885 | if ($finalTokens[$x]['code'] !== T_CASE |
||
| 886 | && $finalTokens[$x]['code'] !== T_INLINE_THEN |
||
| 887 | ) { |
||
| 888 | $finalTokens[$newStackPtr] = array( |
||
| 889 | 'content' => $token[1].':', |
||
| 890 | 'code' => T_GOTO_LABEL, |
||
| 891 | 'type' => 'T_GOTO_LABEL', |
||
| 892 | ); |
||
| 893 | |||
| 894 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 895 | echo "\t\t* token $stackPtr changed from T_STRING to T_GOTO_LABEL".PHP_EOL; |
||
| 896 | echo "\t\t* skipping T_COLON token ".($stackPtr + 1).PHP_EOL; |
||
| 897 | } |
||
| 898 | |||
| 899 | $newStackPtr++; |
||
| 900 | $stackPtr++; |
||
| 901 | continue; |
||
| 902 | } |
||
| 903 | }//end if |
||
| 904 | |||
| 905 | /* |
||
| 906 | HHVM 3.5 tokenizes "else[\s]+if" as a T_ELSEIF token while PHP |
||
| 907 | proper only tokenizes "elseif" as a T_ELSEIF token. So split |
||
| 908 | up the HHVM token to make it looks like proper PHP. |
||
| 909 | */ |
||
| 910 | |||
| 911 | if ($tokenIsArray === true |
||
| 912 | && $token[0] === T_ELSEIF |
||
| 913 | && strtolower($token[1]) !== 'elseif' |
||
| 914 | ) { |
||
| 915 | $finalTokens[$newStackPtr] = array( |
||
| 916 | 'content' => substr($token[1], 0, 4), |
||
| 917 | 'code' => T_ELSE, |
||
| 918 | 'type' => 'T_ELSE', |
||
| 919 | ); |
||
| 920 | |||
| 921 | $newStackPtr++; |
||
| 922 | $finalTokens[$newStackPtr] = array( |
||
| 923 | 'content' => substr($token[1], 4, -2), |
||
| 924 | 'code' => T_WHITESPACE, |
||
| 925 | 'type' => 'T_WHITESPACE', |
||
| 926 | ); |
||
| 927 | |||
| 928 | $newStackPtr++; |
||
| 929 | $finalTokens[$newStackPtr] = array( |
||
| 930 | 'content' => substr($token[1], -2), |
||
| 931 | 'code' => T_IF, |
||
| 932 | 'type' => 'T_IF', |
||
| 933 | ); |
||
| 934 | |||
| 935 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 936 | echo "\t\t* token $stackPtr changed from T_ELSEIF to T_ELSE/T_WHITESPACE/T_IF".PHP_EOL; |
||
| 937 | } |
||
| 938 | |||
| 939 | $newStackPtr++; |
||
| 940 | continue; |
||
| 941 | }//end if |
||
| 942 | |||
| 943 | /* |
||
| 944 | HHVM 3.5 and 3.6 tokenizes a hashbang line such as #!/usr/bin/php |
||
| 945 | as T_HASHANG while PHP proper uses T_INLINE_HTML. |
||
| 946 | */ |
||
| 947 | |||
| 948 | if ($tokenIsArray === true && token_name($token[0]) === 'T_HASHBANG') { |
||
| 949 | $finalTokens[$newStackPtr] = array( |
||
| 950 | 'content' => $token[1], |
||
| 951 | 'code' => T_INLINE_HTML, |
||
| 952 | 'type' => 'T_INLINE_HTML', |
||
| 953 | ); |
||
| 954 | |||
| 955 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 956 | echo "\t\t* token $stackPtr changed from T_HASHBANG to T_INLINE_HTML".PHP_EOL; |
||
| 957 | } |
||
| 958 | |||
| 959 | $newStackPtr++; |
||
| 960 | continue; |
||
| 961 | }//end if |
||
| 962 | |||
| 963 | /* |
||
| 964 | If this token has newlines in its content, split each line up |
||
| 965 | and create a new token for each line. We do this so it's easier |
||
| 966 | to ascertain where errors occur on a line. |
||
| 967 | Note that $token[1] is the token's content. |
||
| 968 | */ |
||
| 969 | |||
| 970 | if ($tokenIsArray === true && strpos($token[1], $this->eolChar) !== false) { |
||
| 971 | $tokenLines = explode($this->eolChar, $token[1]); |
||
| 972 | $numLines = count($tokenLines); |
||
| 973 | $newToken = array( |
||
| 974 | 'type' => token_name($token[0]), |
||
| 975 | 'code' => $token[0], |
||
| 976 | 'content' => '', |
||
| 977 | ); |
||
| 978 | |||
| 979 | for ($i = 0; $i < $numLines; $i++) { |
||
| 980 | $newToken['content'] = $tokenLines[$i]; |
||
| 981 | View Code Duplication | if ($i === ($numLines - 1)) { |
|
| 982 | if ($tokenLines[$i] === '') { |
||
| 983 | break; |
||
| 984 | } |
||
| 985 | } else { |
||
| 986 | $newToken['content'] .= $this->eolChar; |
||
| 987 | } |
||
| 988 | |||
| 989 | $finalTokens[$newStackPtr] = $newToken; |
||
| 990 | $newStackPtr++; |
||
| 991 | } |
||
| 992 | } else { |
||
| 993 | if ($tokenIsArray === true && $token[0] === T_STRING) { |
||
| 994 | // Some T_STRING tokens should remain that way |
||
| 995 | // due to their context. |
||
| 996 | $context = array( |
||
| 997 | T_OBJECT_OPERATOR => true, |
||
| 998 | T_FUNCTION => true, |
||
| 999 | T_CLASS => true, |
||
| 1000 | T_EXTENDS => true, |
||
| 1001 | T_IMPLEMENTS => true, |
||
| 1002 | T_NEW => true, |
||
| 1003 | T_CONST => true, |
||
| 1004 | T_NS_SEPARATOR => true, |
||
| 1005 | T_USE => true, |
||
| 1006 | T_NAMESPACE => true, |
||
| 1007 | T_PAAMAYIM_NEKUDOTAYIM => true, |
||
| 1008 | ); |
||
| 1009 | View Code Duplication | if (isset($context[$finalTokens[$lastNotEmptyToken]['code']]) === true) { |
|
| 1010 | $finalTokens[$newStackPtr] = array( |
||
| 1011 | 'content' => $token[1], |
||
| 1012 | 'code' => T_STRING, |
||
| 1013 | 'type' => 'T_STRING', |
||
| 1014 | ); |
||
| 1015 | $newStackPtr++; |
||
| 1016 | continue; |
||
| 1017 | } |
||
| 1018 | }//end if |
||
| 1019 | |||
| 1020 | $newToken = null; |
||
| 1021 | View Code Duplication | if ($tokenIsArray === false) { |
|
| 1022 | if (isset(self::$_resolveTokenCache[$token[0]]) === true) { |
||
| 1023 | $newToken = self::$_resolveTokenCache[$token[0]]; |
||
| 1024 | } |
||
| 1025 | } else { |
||
| 1026 | $cacheKey = null; |
||
| 1027 | if ($token[0] === T_STRING) { |
||
| 1028 | $cacheKey = strtolower($token[1]); |
||
| 1029 | } else if ($token[0] !== T_CURLY_OPEN) { |
||
| 1030 | $cacheKey = $token[0]; |
||
| 1031 | } |
||
| 1032 | |||
| 1033 | if ($cacheKey !== null && isset(self::$_resolveTokenCache[$cacheKey]) === true) { |
||
| 1034 | $newToken = self::$_resolveTokenCache[$cacheKey]; |
||
| 1035 | $newToken['content'] = $token[1]; |
||
| 1036 | } |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | if ($newToken === null) { |
||
| 1040 | $newToken = self::standardiseToken($token); |
||
| 1041 | } |
||
| 1042 | |||
| 1043 | // Convert colons that are actually the ELSE component of an |
||
| 1044 | // inline IF statement. |
||
| 1045 | if ($newToken['code'] === T_INLINE_THEN) { |
||
| 1046 | $insideInlineIf[] = $stackPtr; |
||
| 1047 | } else if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) { |
||
| 1048 | array_pop($insideInlineIf); |
||
| 1049 | $newToken['code'] = T_INLINE_ELSE; |
||
| 1050 | $newToken['type'] = 'T_INLINE_ELSE'; |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | // This is a special condition for T_ARRAY tokens used for |
||
| 1054 | // type hinting function arguments as being arrays. We want to keep |
||
| 1055 | // the parenthesis map clean, so let's tag these tokens as |
||
| 1056 | // T_ARRAY_HINT. |
||
| 1057 | if ($newToken['code'] === T_ARRAY) { |
||
| 1058 | // Recalculate number of tokens. |
||
| 1059 | for ($i = $stackPtr; $i < $numTokens; $i++) { |
||
| 1060 | if ($tokens[$i] === '(') { |
||
| 1061 | break; |
||
| 1062 | } else if ($tokens[$i][0] === T_VARIABLE) { |
||
| 1063 | $newToken['code'] = T_ARRAY_HINT; |
||
| 1064 | $newToken['type'] = 'T_ARRAY_HINT'; |
||
| 1065 | break; |
||
| 1066 | } |
||
| 1067 | } |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | // This is a special case when checking PHP 5.5+ code in PHP < 5.5 |
||
| 1071 | // where "finally" should be T_FINALLY instead of T_STRING. |
||
| 1072 | if ($newToken['code'] === T_STRING |
||
| 1073 | && strtolower($newToken['content']) === 'finally' |
||
| 1074 | ) { |
||
| 1075 | $newToken['code'] = T_FINALLY; |
||
| 1076 | $newToken['type'] = 'T_FINALLY'; |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | // This is a special case for the PHP 5.5 classname::class syntax |
||
| 1080 | // where "class" should be T_STRING instead of T_CLASS. |
||
| 1081 | if ($newToken['code'] === T_CLASS |
||
| 1082 | && $finalTokens[($newStackPtr - 1)]['code'] === T_DOUBLE_COLON |
||
| 1083 | ) { |
||
| 1084 | $newToken['code'] = T_STRING; |
||
| 1085 | $newToken['type'] = 'T_STRING'; |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | // This is a special case for PHP 5.6 use function and use const |
||
| 1089 | // where "function" and "const" should be T_STRING instead of T_FUNCTION |
||
| 1090 | // and T_CONST. |
||
| 1091 | View Code Duplication | if (($newToken['code'] === T_FUNCTION |
|
| 1092 | || $newToken['code'] === T_CONST) |
||
| 1093 | && $finalTokens[$lastNotEmptyToken]['code'] === T_USE |
||
| 1094 | ) { |
||
| 1095 | $newToken['code'] = T_STRING; |
||
| 1096 | $newToken['type'] = 'T_STRING'; |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | // This is a special case for use groups in PHP 7+ where leaving |
||
| 1100 | // the curly braces as their normal tokens would confuse |
||
| 1101 | // the scope map and sniffs. |
||
| 1102 | View Code Duplication | if ($newToken['code'] === T_OPEN_CURLY_BRACKET |
|
| 1103 | && $finalTokens[$lastNotEmptyToken]['code'] === T_NS_SEPARATOR |
||
| 1104 | ) { |
||
| 1105 | $newToken['code'] = T_OPEN_USE_GROUP; |
||
| 1106 | $newToken['type'] = 'T_OPEN_USE_GROUP'; |
||
| 1107 | $insideUseGroup = true; |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | if ($insideUseGroup === true && $newToken['code'] === T_CLOSE_CURLY_BRACKET) { |
||
| 1111 | $newToken['code'] = T_CLOSE_USE_GROUP; |
||
| 1112 | $newToken['type'] = 'T_CLOSE_USE_GROUP'; |
||
| 1113 | $insideUseGroup = false; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | $finalTokens[$newStackPtr] = $newToken; |
||
| 1117 | $newStackPtr++; |
||
| 1118 | }//end if |
||
| 1119 | }//end for |
||
| 1120 | |||
| 1121 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 1122 | echo "\t*** END PHP TOKENIZING ***".PHP_EOL; |
||
| 1123 | } |
||
| 1124 | |||
| 1125 | return $finalTokens; |
||
| 1126 | |||
| 1127 | }//end tokenize() |
||
| 1128 | |||
| 1734 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: