| Conditions | 138 |
| Paths | > 20000 |
| Total Lines | 705 |
| Code Lines | 388 |
| Lines | 129 |
| Ratio | 18.3 % |
| 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 |
||
| 316 | public function tokenizeString($string, $eolChar='\n') |
||
| 317 | { |
||
| 318 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 319 | echo "\t*** START PHP TOKENIZING ***".PHP_EOL; |
||
| 320 | $isWin = false; |
||
| 321 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
| 322 | $isWin = true; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | $tokens = @token_get_all($string); |
||
| 327 | $finalTokens = array(); |
||
| 328 | |||
| 329 | $newStackPtr = 0; |
||
| 330 | $numTokens = count($tokens); |
||
| 331 | $lastNotEmptyToken = 0; |
||
| 332 | |||
| 333 | $insideInlineIf = array(); |
||
| 334 | $insideUseGroup = false; |
||
| 335 | |||
| 336 | $commentTokenizer = new PHP_CodeSniffer_Tokenizers_Comment(); |
||
| 337 | |||
| 338 | for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) { |
||
| 339 | $token = (array) $tokens[$stackPtr]; |
||
| 340 | $tokenIsArray = isset($token[1]); |
||
| 341 | |||
| 342 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 343 | if ($tokenIsArray === true) { |
||
| 344 | $type = token_name($token[0]); |
||
| 345 | $content = PHP_CodeSniffer::prepareForOutput($token[1]); |
||
| 346 | } else { |
||
| 347 | $newToken = self::resolveSimpleToken($token[0]); |
||
| 348 | $type = $newToken['type']; |
||
| 349 | $content = PHP_CodeSniffer::prepareForOutput($token[0]); |
||
| 350 | } |
||
| 351 | |||
| 352 | echo "\tProcess token "; |
||
| 353 | if ($tokenIsArray === true) { |
||
| 354 | echo "[$stackPtr]"; |
||
| 355 | } else { |
||
| 356 | echo " $stackPtr "; |
||
| 357 | } |
||
| 358 | |||
| 359 | echo ": $type => $content"; |
||
| 360 | }//end if |
||
| 361 | |||
| 362 | if ($newStackPtr > 0 && $finalTokens[($newStackPtr - 1)]['code'] !== T_WHITESPACE) { |
||
| 363 | $lastNotEmptyToken = ($newStackPtr - 1); |
||
| 364 | } |
||
| 365 | |||
| 366 | /* |
||
| 367 | If we are using \r\n newline characters, the \r and \n are sometimes |
||
| 368 | split over two tokens. This normally occurs after comments. We need |
||
| 369 | to merge these two characters together so that our line endings are |
||
| 370 | consistent for all lines. |
||
| 371 | */ |
||
| 372 | |||
| 373 | if ($tokenIsArray === true && substr($token[1], -1) === "\r") { |
||
| 374 | if (isset($tokens[($stackPtr + 1)]) === true |
||
| 375 | && is_array($tokens[($stackPtr + 1)]) === true |
||
| 376 | && $tokens[($stackPtr + 1)][1][0] === "\n" |
||
| 377 | ) { |
||
| 378 | $token[1] .= "\n"; |
||
| 379 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 380 | if ($isWin === true) { |
||
| 381 | echo '\n'; |
||
| 382 | } else { |
||
| 383 | echo "\033[30;1m\\n\033[0m"; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | if ($tokens[($stackPtr + 1)][1] === "\n") { |
||
| 388 | // This token's content has been merged into the previous, |
||
| 389 | // so we can skip it. |
||
| 390 | $tokens[($stackPtr + 1)] = ''; |
||
| 391 | } else { |
||
| 392 | $tokens[($stackPtr + 1)][1] = substr($tokens[($stackPtr + 1)][1], 1); |
||
| 393 | } |
||
| 394 | } |
||
| 395 | }//end if |
||
| 396 | |||
| 397 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 398 | echo PHP_EOL; |
||
| 399 | } |
||
| 400 | |||
| 401 | /* |
||
| 402 | Parse doc blocks into something that can be easily iterated over. |
||
| 403 | */ |
||
| 404 | |||
| 405 | if ($tokenIsArray === true && $token[0] === T_DOC_COMMENT) { |
||
| 406 | $commentTokens = $commentTokenizer->tokenizeString($token[1], $eolChar, $newStackPtr); |
||
| 407 | foreach ($commentTokens as $commentToken) { |
||
| 408 | $finalTokens[$newStackPtr] = $commentToken; |
||
| 409 | $newStackPtr++; |
||
| 410 | } |
||
| 411 | |||
| 412 | continue; |
||
| 413 | } |
||
| 414 | |||
| 415 | /* |
||
| 416 | If this is a double quoted string, PHP will tokenize the whole |
||
| 417 | thing which causes problems with the scope map when braces are |
||
| 418 | within the string. So we need to merge the tokens together to |
||
| 419 | provide a single string. |
||
| 420 | */ |
||
| 421 | |||
| 422 | if ($tokenIsArray === false && ($token[0] === '"' || $token[0] === 'b"')) { |
||
| 423 | // Binary casts need a special token. |
||
| 424 | if ($token[0] === 'b"') { |
||
| 425 | $finalTokens[$newStackPtr] = array( |
||
| 426 | 'code' => T_BINARY_CAST, |
||
| 427 | 'type' => 'T_BINARY_CAST', |
||
| 428 | 'content' => 'b', |
||
| 429 | ); |
||
| 430 | $newStackPtr++; |
||
| 431 | } |
||
| 432 | |||
| 433 | $tokenContent = '"'; |
||
| 434 | $nestedVars = array(); |
||
| 435 | for ($i = ($stackPtr + 1); $i < $numTokens; $i++) { |
||
| 436 | $subToken = (array) $tokens[$i]; |
||
| 437 | $subTokenIsArray = isset($subToken[1]); |
||
| 438 | |||
| 439 | if ($subTokenIsArray === true) { |
||
| 440 | $tokenContent .= $subToken[1]; |
||
| 441 | if ($subToken[1] === '{' |
||
| 442 | && $subToken[0] !== T_ENCAPSED_AND_WHITESPACE |
||
| 443 | ) { |
||
| 444 | $nestedVars[] = $i; |
||
| 445 | } |
||
| 446 | } else { |
||
| 447 | $tokenContent .= $subToken[0]; |
||
| 448 | if ($subToken[0] === '}') { |
||
| 449 | array_pop($nestedVars); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | if ($subTokenIsArray === false |
||
| 454 | && $subToken[0] === '"' |
||
| 455 | && empty($nestedVars) === true |
||
| 456 | ) { |
||
| 457 | // We found the other end of the double quoted string. |
||
| 458 | break; |
||
| 459 | } |
||
| 460 | }//end for |
||
| 461 | |||
| 462 | $stackPtr = $i; |
||
| 463 | |||
| 464 | // Convert each line within the double quoted string to a |
||
| 465 | // new token, so it conforms with other multiple line tokens. |
||
| 466 | $tokenLines = explode($eolChar, $tokenContent); |
||
| 467 | $numLines = count($tokenLines); |
||
| 468 | $newToken = array(); |
||
| 469 | |||
| 470 | for ($j = 0; $j < $numLines; $j++) { |
||
| 471 | $newToken['content'] = $tokenLines[$j]; |
||
| 472 | if ($j === ($numLines - 1)) { |
||
| 473 | if ($tokenLines[$j] === '') { |
||
| 474 | break; |
||
| 475 | } |
||
| 476 | } else { |
||
| 477 | $newToken['content'] .= $eolChar; |
||
| 478 | } |
||
| 479 | |||
| 480 | $newToken['code'] = T_DOUBLE_QUOTED_STRING; |
||
| 481 | $newToken['type'] = 'T_DOUBLE_QUOTED_STRING'; |
||
| 482 | $finalTokens[$newStackPtr] = $newToken; |
||
| 483 | $newStackPtr++; |
||
| 484 | } |
||
| 485 | |||
| 486 | // Continue, as we're done with this token. |
||
| 487 | continue; |
||
| 488 | }//end if |
||
| 489 | |||
| 490 | /* |
||
| 491 | If this is a heredoc, PHP will tokenize the whole |
||
| 492 | thing which causes problems when heredocs don't |
||
| 493 | contain real PHP code, which is almost never. |
||
| 494 | We want to leave the start and end heredoc tokens |
||
| 495 | alone though. |
||
| 496 | */ |
||
| 497 | |||
| 498 | if ($tokenIsArray === true && $token[0] === T_START_HEREDOC) { |
||
| 499 | // Add the start heredoc token to the final array. |
||
| 500 | $finalTokens[$newStackPtr] = self::standardiseToken($token); |
||
| 501 | |||
| 502 | // Check if this is actually a nowdoc and use a different token |
||
| 503 | // to help the sniffs. |
||
| 504 | $nowdoc = false; |
||
| 505 | if ($token[1][3] === "'") { |
||
| 506 | $finalTokens[$newStackPtr]['code'] = T_START_NOWDOC; |
||
| 507 | $finalTokens[$newStackPtr]['type'] = 'T_START_NOWDOC'; |
||
| 508 | $nowdoc = true; |
||
| 509 | } |
||
| 510 | |||
| 511 | $tokenContent = ''; |
||
| 512 | for ($i = ($stackPtr + 1); $i < $numTokens; $i++) { |
||
| 513 | $subTokenIsArray = is_array($tokens[$i]); |
||
| 514 | if ($subTokenIsArray === true |
||
| 515 | && $tokens[$i][0] === T_END_HEREDOC |
||
| 516 | ) { |
||
| 517 | // We found the other end of the heredoc. |
||
| 518 | break; |
||
| 519 | } |
||
| 520 | |||
| 521 | if ($subTokenIsArray === true) { |
||
| 522 | $tokenContent .= $tokens[$i][1]; |
||
| 523 | } else { |
||
| 524 | $tokenContent .= $tokens[$i]; |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | if ($i === $numTokens) { |
||
| 529 | // We got to the end of the file and never |
||
| 530 | // found the closing token, so this probably wasn't |
||
| 531 | // a heredoc. |
||
| 532 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 533 | $type = $finalTokens[$newStackPtr]['type']; |
||
| 534 | echo "\t\t* failed to find the end of the here/nowdoc".PHP_EOL; |
||
| 535 | echo "\t\t* token $stackPtr changed from $type to T_STRING".PHP_EOL; |
||
| 536 | } |
||
| 537 | |||
| 538 | $finalTokens[$newStackPtr]['code'] = T_STRING; |
||
| 539 | $finalTokens[$newStackPtr]['type'] = 'T_STRING'; |
||
| 540 | $newStackPtr++; |
||
| 541 | continue; |
||
| 542 | } |
||
| 543 | |||
| 544 | $stackPtr = $i; |
||
| 545 | $newStackPtr++; |
||
| 546 | |||
| 547 | // Convert each line within the heredoc to a |
||
| 548 | // new token, so it conforms with other multiple line tokens. |
||
| 549 | $tokenLines = explode($eolChar, $tokenContent); |
||
| 550 | $numLines = count($tokenLines); |
||
| 551 | $newToken = array(); |
||
| 552 | |||
| 553 | for ($j = 0; $j < $numLines; $j++) { |
||
| 554 | $newToken['content'] = $tokenLines[$j]; |
||
| 555 | if ($j === ($numLines - 1)) { |
||
| 556 | if ($tokenLines[$j] === '') { |
||
| 557 | break; |
||
| 558 | } |
||
| 559 | } else { |
||
| 560 | $newToken['content'] .= $eolChar; |
||
| 561 | } |
||
| 562 | |||
| 563 | if ($nowdoc === true) { |
||
| 564 | $newToken['code'] = T_NOWDOC; |
||
| 565 | $newToken['type'] = 'T_NOWDOC'; |
||
| 566 | } else { |
||
| 567 | $newToken['code'] = T_HEREDOC; |
||
| 568 | $newToken['type'] = 'T_HEREDOC'; |
||
| 569 | } |
||
| 570 | |||
| 571 | $finalTokens[$newStackPtr] = $newToken; |
||
| 572 | $newStackPtr++; |
||
| 573 | }//end for |
||
| 574 | |||
| 575 | // Add the end heredoc token to the final array. |
||
| 576 | $finalTokens[$newStackPtr] = self::standardiseToken($tokens[$stackPtr]); |
||
| 577 | |||
| 578 | if ($nowdoc === true) { |
||
| 579 | $finalTokens[$newStackPtr]['code'] = T_END_NOWDOC; |
||
| 580 | $finalTokens[$newStackPtr]['type'] = 'T_END_NOWDOC'; |
||
| 581 | $nowdoc = true; |
||
| 582 | } |
||
| 583 | |||
| 584 | $newStackPtr++; |
||
| 585 | |||
| 586 | // Continue, as we're done with this token. |
||
| 587 | continue; |
||
| 588 | }//end if |
||
| 589 | |||
| 590 | /* |
||
| 591 | Before PHP 5.6, the ... operator was tokenized as three |
||
| 592 | T_STRING_CONCAT tokens in a row. So look for and combine |
||
| 593 | these tokens in earlier versions. |
||
| 594 | */ |
||
| 595 | |||
| 596 | if ($tokenIsArray === false |
||
| 597 | && $token[0] === '.' |
||
| 598 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 599 | && isset($tokens[($stackPtr + 2)]) === true |
||
| 600 | && $tokens[($stackPtr + 1)] === '.' |
||
| 601 | && $tokens[($stackPtr + 2)] === '.' |
||
| 602 | ) { |
||
| 603 | $newToken = array(); |
||
| 604 | $newToken['code'] = T_ELLIPSIS; |
||
| 605 | $newToken['type'] = 'T_ELLIPSIS'; |
||
| 606 | $newToken['content'] = '...'; |
||
| 607 | $finalTokens[$newStackPtr] = $newToken; |
||
| 608 | |||
| 609 | $newStackPtr++; |
||
| 610 | $stackPtr += 2; |
||
| 611 | continue; |
||
| 612 | } |
||
| 613 | |||
| 614 | /* |
||
| 615 | Before PHP 5.6, the ** operator was tokenized as two |
||
| 616 | T_MULTIPLY tokens in a row. So look for and combine |
||
| 617 | these tokens in earlier versions. |
||
| 618 | */ |
||
| 619 | |||
| 620 | if ($tokenIsArray === false |
||
| 621 | && $token[0] === '*' |
||
| 622 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 623 | && $tokens[($stackPtr + 1)] === '*' |
||
| 624 | ) { |
||
| 625 | $newToken = array(); |
||
| 626 | $newToken['code'] = T_POW; |
||
| 627 | $newToken['type'] = 'T_POW'; |
||
| 628 | $newToken['content'] = '**'; |
||
| 629 | $finalTokens[$newStackPtr] = $newToken; |
||
| 630 | |||
| 631 | $newStackPtr++; |
||
| 632 | $stackPtr++; |
||
| 633 | continue; |
||
| 634 | } |
||
| 635 | |||
| 636 | /* |
||
| 637 | Before PHP 5.6, the **= operator was tokenized as |
||
| 638 | T_MULTIPLY followed by T_MUL_EQUAL. So look for and combine |
||
| 639 | these tokens in earlier versions. |
||
| 640 | */ |
||
| 641 | |||
| 642 | if ($tokenIsArray === false |
||
| 643 | && $token[0] === '*' |
||
| 644 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 645 | && is_array($tokens[($stackPtr + 1)]) === true |
||
| 646 | && $tokens[($stackPtr + 1)][1] === '*=' |
||
| 647 | ) { |
||
| 648 | $newToken = array(); |
||
| 649 | $newToken['code'] = T_POW_EQUAL; |
||
| 650 | $newToken['type'] = 'T_POW_EQUAL'; |
||
| 651 | $newToken['content'] = '**='; |
||
| 652 | $finalTokens[$newStackPtr] = $newToken; |
||
| 653 | |||
| 654 | $newStackPtr++; |
||
| 655 | $stackPtr++; |
||
| 656 | continue; |
||
| 657 | } |
||
| 658 | |||
| 659 | /* |
||
| 660 | Before PHP 7, the ?? operator was tokenized as |
||
| 661 | T_INLINE_THEN followed by T_INLINE_THEN. |
||
| 662 | So look for and combine these tokens in earlier versions. |
||
| 663 | */ |
||
| 664 | |||
| 665 | if ($tokenIsArray === false |
||
| 666 | && $token[0] === '?' |
||
| 667 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 668 | && $tokens[($stackPtr + 1)][0] === '?' |
||
| 669 | ) { |
||
| 670 | $newToken = array(); |
||
| 671 | $newToken['code'] = T_COALESCE; |
||
| 672 | $newToken['type'] = 'T_COALESCE'; |
||
| 673 | $newToken['content'] = '??'; |
||
| 674 | $finalTokens[$newStackPtr] = $newToken; |
||
| 675 | |||
| 676 | $newStackPtr++; |
||
| 677 | $stackPtr++; |
||
| 678 | continue; |
||
| 679 | } |
||
| 680 | |||
| 681 | /* |
||
| 682 | Tokens after a double colon may be look like scope openers, |
||
| 683 | such as when writing code like Foo::NAMESAPCE, but they are |
||
| 684 | only ever variables or strings. |
||
| 685 | */ |
||
| 686 | |||
| 687 | if ($stackPtr > 1 |
||
| 688 | && $tokens[($stackPtr - 1)][0] === T_PAAMAYIM_NEKUDOTAYIM |
||
| 689 | && $tokenIsArray === true |
||
| 690 | && $token[0] !== T_STRING |
||
| 691 | && $token[0] !== T_VARIABLE |
||
| 692 | && $token[0] !== T_DOLLAR |
||
| 693 | && isset(PHP_CodeSniffer_Tokens::$emptyTokens[$token[0]]) === false |
||
| 694 | ) { |
||
| 695 | $newToken = array(); |
||
| 696 | $newToken['code'] = T_STRING; |
||
| 697 | $newToken['type'] = 'T_STRING'; |
||
| 698 | $newToken['content'] = $token[1]; |
||
| 699 | $finalTokens[$newStackPtr] = $newToken; |
||
| 700 | |||
| 701 | $newStackPtr++; |
||
| 702 | continue; |
||
| 703 | } |
||
| 704 | |||
| 705 | /* |
||
| 706 | Before PHP 7, the <=> operator was tokenized as |
||
| 707 | T_IS_SMALLER_OR_EQUAL followed by T_GREATER_THAN. |
||
| 708 | So look for and combine these tokens in earlier versions. |
||
| 709 | */ |
||
| 710 | |||
| 711 | if ($tokenIsArray === true |
||
| 712 | && $token[0] === T_IS_SMALLER_OR_EQUAL |
||
| 713 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 714 | && $tokens[($stackPtr + 1)][0] === '>' |
||
| 715 | ) { |
||
| 716 | $newToken = array(); |
||
| 717 | $newToken['code'] = T_SPACESHIP; |
||
| 718 | $newToken['type'] = 'T_SPACESHIP'; |
||
| 719 | $newToken['content'] = '<=>'; |
||
| 720 | $finalTokens[$newStackPtr] = $newToken; |
||
| 721 | |||
| 722 | $newStackPtr++; |
||
| 723 | $stackPtr++; |
||
| 724 | continue; |
||
| 725 | } |
||
| 726 | |||
| 727 | /* |
||
| 728 | Emulate traits in PHP versions less than 5.4. |
||
| 729 | */ |
||
| 730 | |||
| 731 | if ($tokenIsArray === true |
||
| 732 | && $token[0] === T_STRING |
||
| 733 | && strtolower($token[1]) === 'trait' |
||
| 734 | && $tokens[($stackPtr - 1)][0] !== T_OBJECT_OPERATOR |
||
| 735 | && $tokens[($stackPtr - 1)][0] !== T_PAAMAYIM_NEKUDOTAYIM |
||
| 736 | ) { |
||
| 737 | $finalTokens[$newStackPtr] = array( |
||
| 738 | 'content' => $token[1], |
||
| 739 | 'code' => T_TRAIT, |
||
| 740 | 'type' => 'T_TRAIT', |
||
| 741 | ); |
||
| 742 | |||
| 743 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 744 | echo "\t\t* token $stackPtr changed from T_STRING to T_TRAIT".PHP_EOL; |
||
| 745 | } |
||
| 746 | |||
| 747 | $newStackPtr++; |
||
| 748 | continue; |
||
| 749 | } |
||
| 750 | |||
| 751 | /* |
||
| 752 | PHP doesn't assign a token to goto labels, so we have to. |
||
| 753 | These are just string tokens with a single colon after them. Double |
||
| 754 | colons are already tokenized and so don't interfere with this check. |
||
| 755 | But we do have to account for CASE statements, that look just like |
||
| 756 | goto labels. |
||
| 757 | */ |
||
| 758 | |||
| 759 | if ($tokenIsArray === true |
||
| 760 | && $token[0] === T_STRING |
||
| 761 | && isset($tokens[($stackPtr + 1)]) === true |
||
| 762 | && $tokens[($stackPtr + 1)] === ':' |
||
| 763 | && $tokens[($stackPtr - 1)][0] !== T_PAAMAYIM_NEKUDOTAYIM |
||
| 764 | ) { |
||
| 765 | $stopTokens = array( |
||
| 766 | T_CASE => true, |
||
| 767 | T_SEMICOLON => true, |
||
| 768 | T_OPEN_CURLY_BRACKET => true, |
||
| 769 | T_INLINE_THEN => true, |
||
| 770 | ); |
||
| 771 | |||
| 772 | for ($x = ($newStackPtr - 1); $x > 0; $x--) { |
||
| 773 | if (isset($stopTokens[$finalTokens[$x]['code']]) === true) { |
||
| 774 | break; |
||
| 775 | } |
||
| 776 | } |
||
| 777 | |||
| 778 | if ($finalTokens[$x]['code'] !== T_CASE |
||
| 779 | && $finalTokens[$x]['code'] !== T_INLINE_THEN |
||
| 780 | ) { |
||
| 781 | $finalTokens[$newStackPtr] = array( |
||
| 782 | 'content' => $token[1].':', |
||
| 783 | 'code' => T_GOTO_LABEL, |
||
| 784 | 'type' => 'T_GOTO_LABEL', |
||
| 785 | ); |
||
| 786 | |||
| 787 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 788 | echo "\t\t* token $stackPtr changed from T_STRING to T_GOTO_LABEL".PHP_EOL; |
||
| 789 | echo "\t\t* skipping T_COLON token ".($stackPtr + 1).PHP_EOL; |
||
| 790 | } |
||
| 791 | |||
| 792 | $newStackPtr++; |
||
| 793 | $stackPtr++; |
||
| 794 | continue; |
||
| 795 | } |
||
| 796 | }//end if |
||
| 797 | |||
| 798 | /* |
||
| 799 | HHVM 3.5 tokenizes "else[\s]+if" as a T_ELSEIF token while PHP |
||
| 800 | proper only tokenizes "elseif" as a T_ELSEIF token. So split |
||
| 801 | up the HHVM token to make it looks like proper PHP. |
||
| 802 | */ |
||
| 803 | |||
| 804 | if ($tokenIsArray === true |
||
| 805 | && $token[0] === T_ELSEIF |
||
| 806 | && strtolower($token[1]) !== 'elseif' |
||
| 807 | ) { |
||
| 808 | $finalTokens[$newStackPtr] = array( |
||
| 809 | 'content' => substr($token[1], 0, 4), |
||
| 810 | 'code' => T_ELSE, |
||
| 811 | 'type' => 'T_ELSE', |
||
| 812 | ); |
||
| 813 | |||
| 814 | $newStackPtr++; |
||
| 815 | $finalTokens[$newStackPtr] = array( |
||
| 816 | 'content' => substr($token[1], 4, -2), |
||
| 817 | 'code' => T_WHITESPACE, |
||
| 818 | 'type' => 'T_WHITESPACE', |
||
| 819 | ); |
||
| 820 | |||
| 821 | $newStackPtr++; |
||
| 822 | $finalTokens[$newStackPtr] = array( |
||
| 823 | 'content' => substr($token[1], -2), |
||
| 824 | 'code' => T_IF, |
||
| 825 | 'type' => 'T_IF', |
||
| 826 | ); |
||
| 827 | |||
| 828 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 829 | echo "\t\t* token $stackPtr changed from T_ELSEIF to T_ELSE/T_WHITESPACE/T_IF".PHP_EOL; |
||
| 830 | } |
||
| 831 | |||
| 832 | $newStackPtr++; |
||
| 833 | continue; |
||
| 834 | }//end if |
||
| 835 | |||
| 836 | /* |
||
| 837 | HHVM 3.5 and 3.6 tokenizes a hashbang line such as #!/usr/bin/php |
||
| 838 | as T_HASHANG while PHP proper uses T_INLINE_HTML. |
||
| 839 | */ |
||
| 840 | |||
| 841 | if ($tokenIsArray === true && token_name($token[0]) === 'T_HASHBANG') { |
||
| 842 | $finalTokens[$newStackPtr] = array( |
||
| 843 | 'content' => $token[1], |
||
| 844 | 'code' => T_INLINE_HTML, |
||
| 845 | 'type' => 'T_INLINE_HTML', |
||
| 846 | ); |
||
| 847 | |||
| 848 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 849 | echo "\t\t* token $stackPtr changed from T_HASHBANG to T_INLINE_HTML".PHP_EOL; |
||
| 850 | } |
||
| 851 | |||
| 852 | $newStackPtr++; |
||
| 853 | continue; |
||
| 854 | }//end if |
||
| 855 | |||
| 856 | /* |
||
| 857 | If this token has newlines in its content, split each line up |
||
| 858 | and create a new token for each line. We do this so it's easier |
||
| 859 | to ascertain where errors occur on a line. |
||
| 860 | Note that $token[1] is the token's content. |
||
| 861 | */ |
||
| 862 | |||
| 863 | if ($tokenIsArray === true && strpos($token[1], $eolChar) !== false) { |
||
| 864 | $tokenLines = explode($eolChar, $token[1]); |
||
| 865 | $numLines = count($tokenLines); |
||
| 866 | $newToken = array( |
||
| 867 | 'type' => token_name($token[0]), |
||
| 868 | 'code' => $token[0], |
||
| 869 | 'content' => '', |
||
| 870 | ); |
||
| 871 | |||
| 872 | for ($i = 0; $i < $numLines; $i++) { |
||
| 873 | $newToken['content'] = $tokenLines[$i]; |
||
| 874 | if ($i === ($numLines - 1)) { |
||
| 875 | if ($tokenLines[$i] === '') { |
||
| 876 | break; |
||
| 877 | } |
||
| 878 | } else { |
||
| 879 | $newToken['content'] .= $eolChar; |
||
| 880 | } |
||
| 881 | |||
| 882 | $finalTokens[$newStackPtr] = $newToken; |
||
| 883 | $newStackPtr++; |
||
| 884 | } |
||
| 885 | } else { |
||
| 886 | if ($tokenIsArray === true && $token[0] === T_STRING) { |
||
| 887 | // Some T_STRING tokens should remain that way |
||
| 888 | // due to their context. |
||
| 889 | $context = array( |
||
| 890 | T_OBJECT_OPERATOR => true, |
||
| 891 | T_FUNCTION => true, |
||
| 892 | T_CLASS => true, |
||
| 893 | T_EXTENDS => true, |
||
| 894 | T_IMPLEMENTS => true, |
||
| 895 | T_NEW => true, |
||
| 896 | T_CONST => true, |
||
| 897 | T_NS_SEPARATOR => true, |
||
| 898 | T_USE => true, |
||
| 899 | T_NAMESPACE => true, |
||
| 900 | T_PAAMAYIM_NEKUDOTAYIM => true, |
||
| 901 | ); |
||
| 902 | if (isset($context[$finalTokens[$lastNotEmptyToken]['code']]) === true) { |
||
| 903 | $finalTokens[$newStackPtr] = array( |
||
| 904 | 'content' => $token[1], |
||
| 905 | 'code' => T_STRING, |
||
| 906 | 'type' => 'T_STRING', |
||
| 907 | ); |
||
| 908 | $newStackPtr++; |
||
| 909 | continue; |
||
| 910 | } |
||
| 911 | }//end if |
||
| 912 | |||
| 913 | $newToken = null; |
||
| 914 | if ($tokenIsArray === false) { |
||
| 915 | if (isset(self::$_resolveTokenCache[$token[0]]) === true) { |
||
| 916 | $newToken = self::$_resolveTokenCache[$token[0]]; |
||
| 917 | } |
||
| 918 | } else { |
||
| 919 | $cacheKey = null; |
||
| 920 | if ($token[0] === T_STRING) { |
||
| 921 | $cacheKey = strtolower($token[1]); |
||
| 922 | } else if ($token[0] !== T_CURLY_OPEN) { |
||
| 923 | $cacheKey = $token[0]; |
||
| 924 | } |
||
| 925 | |||
| 926 | if ($cacheKey !== null && isset(self::$_resolveTokenCache[$cacheKey]) === true) { |
||
| 927 | $newToken = self::$_resolveTokenCache[$cacheKey]; |
||
| 928 | $newToken['content'] = $token[1]; |
||
| 929 | } |
||
| 930 | } |
||
| 931 | |||
| 932 | if ($newToken === null) { |
||
| 933 | $newToken = self::standardiseToken($token); |
||
| 934 | } |
||
| 935 | |||
| 936 | // Convert colons that are actually the ELSE component of an |
||
| 937 | // inline IF statement. |
||
| 938 | if ($newToken['code'] === T_INLINE_THEN) { |
||
| 939 | $insideInlineIf[] = $stackPtr; |
||
| 940 | } else if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) { |
||
| 941 | array_pop($insideInlineIf); |
||
| 942 | $newToken['code'] = T_INLINE_ELSE; |
||
| 943 | $newToken['type'] = 'T_INLINE_ELSE'; |
||
| 944 | } |
||
| 945 | |||
| 946 | // This is a special condition for T_ARRAY tokens used for |
||
| 947 | // type hinting function arguments as being arrays. We want to keep |
||
| 948 | // the parenthesis map clean, so let's tag these tokens as |
||
| 949 | // T_ARRAY_HINT. |
||
| 950 | if ($newToken['code'] === T_ARRAY) { |
||
| 951 | for ($i = $stackPtr; $i < $numTokens; $i++) { |
||
| 952 | if ($tokens[$i] === '(') { |
||
| 953 | break; |
||
| 954 | } else if ($tokens[$i][0] === T_VARIABLE) { |
||
| 955 | $newToken['code'] = T_ARRAY_HINT; |
||
| 956 | $newToken['type'] = 'T_ARRAY_HINT'; |
||
| 957 | break; |
||
| 958 | } |
||
| 959 | } |
||
| 960 | } |
||
| 961 | |||
| 962 | // This is a special case when checking PHP 5.5+ code in PHP < 5.5 |
||
| 963 | // where "finally" should be T_FINALLY instead of T_STRING. |
||
| 964 | if ($newToken['code'] === T_STRING |
||
| 965 | && strtolower($newToken['content']) === 'finally' |
||
| 966 | ) { |
||
| 967 | $newToken['code'] = T_FINALLY; |
||
| 968 | $newToken['type'] = 'T_FINALLY'; |
||
| 969 | } |
||
| 970 | |||
| 971 | // This is a special case for the PHP 5.5 classname::class syntax |
||
| 972 | // where "class" should be T_STRING instead of T_CLASS. |
||
| 973 | if (($newToken['code'] === T_CLASS |
||
| 974 | || $newToken['code'] === T_FUNCTION) |
||
| 975 | && $finalTokens[($newStackPtr - 1)]['code'] === T_DOUBLE_COLON |
||
| 976 | ) { |
||
| 977 | $newToken['code'] = T_STRING; |
||
| 978 | $newToken['type'] = 'T_STRING'; |
||
| 979 | } |
||
| 980 | |||
| 981 | // This is a special case for PHP 5.6 use function and use const |
||
| 982 | // where "function" and "const" should be T_STRING instead of T_FUNCTION |
||
| 983 | // and T_CONST. |
||
| 984 | if (($newToken['code'] === T_FUNCTION |
||
| 985 | || $newToken['code'] === T_CONST) |
||
| 986 | && $finalTokens[$lastNotEmptyToken]['code'] === T_USE |
||
| 987 | ) { |
||
| 988 | $newToken['code'] = T_STRING; |
||
| 989 | $newToken['type'] = 'T_STRING'; |
||
| 990 | } |
||
| 991 | |||
| 992 | // This is a special case for use groups in PHP 7+ where leaving |
||
| 993 | // the curly braces as their normal tokens would confuse |
||
| 994 | // the scope map and sniffs. |
||
| 995 | if ($newToken['code'] === T_OPEN_CURLY_BRACKET |
||
| 996 | && $finalTokens[$lastNotEmptyToken]['code'] === T_NS_SEPARATOR |
||
| 997 | ) { |
||
| 998 | $newToken['code'] = T_OPEN_USE_GROUP; |
||
| 999 | $newToken['type'] = 'T_OPEN_USE_GROUP'; |
||
| 1000 | $insideUseGroup = true; |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | if ($insideUseGroup === true && $newToken['code'] === T_CLOSE_CURLY_BRACKET) { |
||
| 1004 | $newToken['code'] = T_CLOSE_USE_GROUP; |
||
| 1005 | $newToken['type'] = 'T_CLOSE_USE_GROUP'; |
||
| 1006 | $insideUseGroup = false; |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | $finalTokens[$newStackPtr] = $newToken; |
||
| 1010 | $newStackPtr++; |
||
| 1011 | }//end if |
||
| 1012 | }//end for |
||
| 1013 | |||
| 1014 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
||
| 1015 | echo "\t*** END PHP TOKENIZING ***".PHP_EOL; |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | return $finalTokens; |
||
| 1019 | |||
| 1020 | }//end tokenizeString() |
||
| 1021 | |||
| 1611 |