| Conditions | 67 |
| Paths | 187 |
| Total Lines | 278 |
| Code Lines | 157 |
| Lines | 62 |
| Ratio | 22.3 % |
| Tests | 0 |
| CRAP Score | 4556 |
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 |
||
| 557 | public function decode($str) |
||
| 558 | { |
||
| 559 | $str = $this->reduce_string($str); |
||
| 560 | |||
| 561 | switch (strtolower($str)) { |
||
| 562 | case 'true': |
||
| 563 | return true; |
||
| 564 | |||
| 565 | case 'false': |
||
| 566 | return false; |
||
| 567 | |||
| 568 | case 'null': |
||
| 569 | return null; |
||
| 570 | |||
| 571 | default: |
||
| 572 | $m = array(); |
||
| 573 | |||
| 574 | if (is_numeric($str)) { |
||
| 575 | // Lookie-loo, it's a number |
||
| 576 | |||
| 577 | // This would work on its own, but I'm trying to be |
||
| 578 | // good about returning integers where appropriate: |
||
| 579 | // return (float)$str; |
||
| 580 | |||
| 581 | // Return float or int, as appropriate |
||
| 582 | return ((float)$str == (integer)$str) |
||
| 583 | ? (integer)$str |
||
| 584 | : (float)$str; |
||
| 585 | |||
| 586 | } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { |
||
| 587 | // STRINGS RETURNED IN UTF-8 FORMAT |
||
| 588 | $delim = substr($str, 0, 1); |
||
| 589 | $chrs = substr($str, 1, -1); |
||
| 590 | $utf8 = ''; |
||
| 591 | $strlen_chrs = strlen($chrs); |
||
| 592 | |||
| 593 | for ($c = 0; $c < $strlen_chrs; ++$c) { |
||
| 594 | |||
| 595 | $substr_chrs_c_2 = substr($chrs, $c, 2); |
||
| 596 | $ord_chrs_c = ord($chrs{$c}); |
||
| 597 | |||
| 598 | switch (true) { |
||
| 599 | case $substr_chrs_c_2 == '\b': |
||
| 600 | $utf8 .= chr(0x08); |
||
| 601 | ++$c; |
||
| 602 | break; |
||
| 603 | case $substr_chrs_c_2 == '\t': |
||
| 604 | $utf8 .= chr(0x09); |
||
| 605 | ++$c; |
||
| 606 | break; |
||
| 607 | case $substr_chrs_c_2 == '\n': |
||
| 608 | $utf8 .= chr(0x0A); |
||
| 609 | ++$c; |
||
| 610 | break; |
||
| 611 | case $substr_chrs_c_2 == '\f': |
||
| 612 | $utf8 .= chr(0x0C); |
||
| 613 | ++$c; |
||
| 614 | break; |
||
| 615 | case $substr_chrs_c_2 == '\r': |
||
| 616 | $utf8 .= chr(0x0D); |
||
| 617 | ++$c; |
||
| 618 | break; |
||
| 619 | |||
| 620 | case $substr_chrs_c_2 == '\\"': |
||
| 621 | case $substr_chrs_c_2 == '\\\'': |
||
| 622 | case $substr_chrs_c_2 == '\\\\': |
||
| 623 | case $substr_chrs_c_2 == '\\/': |
||
| 624 | if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
||
| 625 | ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
||
| 626 | $utf8 .= $chrs{++$c}; |
||
| 627 | } |
||
| 628 | break; |
||
| 629 | |||
| 630 | case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
||
| 631 | // single, escaped unicode character |
||
| 632 | $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) |
||
| 633 | . chr(hexdec(substr($chrs, ($c + 4), 2))); |
||
| 634 | $utf8 .= $this->utf162utf8($utf16); |
||
| 635 | $c += 5; |
||
| 636 | break; |
||
| 637 | |||
| 638 | case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): |
||
| 639 | $utf8 .= $chrs{$c}; |
||
| 640 | break; |
||
| 641 | |||
| 642 | View Code Duplication | case ($ord_chrs_c & 0xE0) == 0xC0: |
|
| 643 | // characters U-00000080 - U-000007FF, mask 110XXXXX |
||
| 644 | //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 645 | $utf8 .= substr($chrs, $c, 2); |
||
| 646 | ++$c; |
||
| 647 | break; |
||
| 648 | |||
| 649 | View Code Duplication | case ($ord_chrs_c & 0xF0) == 0xE0: |
|
| 650 | // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
||
| 651 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 652 | $utf8 .= substr($chrs, $c, 3); |
||
| 653 | $c += 2; |
||
| 654 | break; |
||
| 655 | |||
| 656 | View Code Duplication | case ($ord_chrs_c & 0xF8) == 0xF0: |
|
| 657 | // characters U-00010000 - U-001FFFFF, mask 11110XXX |
||
| 658 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 659 | $utf8 .= substr($chrs, $c, 4); |
||
| 660 | $c += 3; |
||
| 661 | break; |
||
| 662 | |||
| 663 | View Code Duplication | case ($ord_chrs_c & 0xFC) == 0xF8: |
|
| 664 | // characters U-00200000 - U-03FFFFFF, mask 111110XX |
||
| 665 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 666 | $utf8 .= substr($chrs, $c, 5); |
||
| 667 | $c += 4; |
||
| 668 | break; |
||
| 669 | |||
| 670 | View Code Duplication | case ($ord_chrs_c & 0xFE) == 0xFC: |
|
| 671 | // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
||
| 672 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 673 | $utf8 .= substr($chrs, $c, 6); |
||
| 674 | $c += 5; |
||
| 675 | break; |
||
| 676 | |||
| 677 | } |
||
| 678 | |||
| 679 | } |
||
| 680 | |||
| 681 | return $utf8; |
||
| 682 | |||
| 683 | } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { |
||
| 684 | // array, or object notation |
||
| 685 | |||
| 686 | if ($str{0} == '[') { |
||
| 687 | $stk = array(SERVICES_JSON_IN_ARR); |
||
| 688 | $arr = array(); |
||
| 689 | } else { |
||
| 690 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
| 691 | $stk = array(SERVICES_JSON_IN_OBJ); |
||
| 692 | $obj = array(); |
||
| 693 | } else { |
||
| 694 | $stk = array(SERVICES_JSON_IN_OBJ); |
||
| 695 | $obj = new stdClass(); |
||
| 696 | } |
||
| 697 | } |
||
| 698 | |||
| 699 | array_push($stk, array('what' => SERVICES_JSON_SLICE, |
||
| 700 | 'where' => 0, |
||
| 701 | 'delim' => false)); |
||
| 702 | |||
| 703 | $chrs = substr($str, 1, -1); |
||
| 704 | $chrs = $this->reduce_string($chrs); |
||
| 705 | |||
| 706 | if ($chrs == '') { |
||
| 707 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
| 708 | return $arr; |
||
| 709 | |||
| 710 | } else { |
||
| 711 | return $obj; |
||
| 712 | |||
| 713 | } |
||
| 714 | } |
||
| 715 | |||
| 716 | //print("\nparsing {$chrs}\n"); |
||
| 717 | |||
| 718 | $strlen_chrs = strlen($chrs); |
||
| 719 | |||
| 720 | for ($c = 0; $c <= $strlen_chrs; ++$c) { |
||
| 721 | |||
| 722 | $top = end($stk); |
||
| 723 | $substr_chrs_c_2 = substr($chrs, $c, 2); |
||
| 724 | |||
| 725 | if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
||
| 726 | // found a comma that is not inside a string, array, etc., |
||
| 727 | // OR we've reached the end of the character list |
||
| 728 | $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
||
| 729 | array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
||
| 730 | //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 731 | |||
| 732 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
| 733 | // we are in an array, so just push an element onto the stack |
||
| 734 | array_push($arr, $this->decode($slice)); |
||
| 735 | |||
| 736 | } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
||
| 737 | // we are in an object, so figure |
||
| 738 | // out the property name and set an |
||
| 739 | // element in an associative array, |
||
| 740 | // for now |
||
| 741 | $parts = array(); |
||
| 742 | |||
| 743 | if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
| 744 | // "name":value pair |
||
| 745 | $key = $this->decode($parts[1]); |
||
| 746 | $val = $this->decode($parts[2]); |
||
| 747 | |||
| 748 | View Code Duplication | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
|
| 749 | $obj[$key] = $val; |
||
| 750 | } else { |
||
| 751 | $obj->$key = $val; |
||
| 752 | } |
||
| 753 | } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
| 754 | // name:value pair, where name is unquoted |
||
| 755 | $key = $parts[1]; |
||
| 756 | $val = $this->decode($parts[2]); |
||
| 757 | |||
| 758 | View Code Duplication | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
|
| 759 | $obj[$key] = $val; |
||
| 760 | } else { |
||
| 761 | $obj->$key = $val; |
||
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | } |
||
| 766 | |||
| 767 | } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
||
| 768 | // found a quote, and we are not inside a string |
||
| 769 | array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); |
||
| 770 | //print("Found start of string at {$c}\n"); |
||
| 771 | |||
| 772 | } elseif (($chrs{$c} == $top['delim']) && |
||
| 773 | ($top['what'] == SERVICES_JSON_IN_STR) && |
||
| 774 | ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
||
| 775 | // found a quote, we're in a string, and it's not escaped |
||
| 776 | // we know that it's not escaped becase there is _not_ an |
||
| 777 | // odd number of backslashes at the end of the string so far |
||
| 778 | array_pop($stk); |
||
| 779 | //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
||
| 780 | |||
| 781 | View Code Duplication | } elseif (($chrs{$c} == '[') && |
|
| 782 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
| 783 | // found a left-bracket, and we are in an array, object, or slice |
||
| 784 | array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
||
| 785 | //print("Found start of array at {$c}\n"); |
||
| 786 | |||
| 787 | } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
||
| 788 | // found a right-bracket, and we're in an array |
||
| 789 | array_pop($stk); |
||
| 790 | //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 791 | |||
| 792 | View Code Duplication | } elseif (($chrs{$c} == '{') && |
|
| 793 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
| 794 | // found a left-brace, and we are in an array, object, or slice |
||
| 795 | array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
||
| 796 | //print("Found start of object at {$c}\n"); |
||
| 797 | |||
| 798 | } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { |
||
| 799 | // found a right-brace, and we're in an object |
||
| 800 | array_pop($stk); |
||
| 801 | //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 802 | |||
| 803 | View Code Duplication | } elseif (($substr_chrs_c_2 == '/*') && |
|
| 804 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
| 805 | // found a comment start, and we are in an array, object, or slice |
||
| 806 | array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); |
||
| 807 | $c++; |
||
| 808 | //print("Found start of comment at {$c}\n"); |
||
| 809 | |||
| 810 | } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { |
||
| 811 | // found a comment end, and we're in one now |
||
| 812 | array_pop($stk); |
||
| 813 | $c++; |
||
| 814 | |||
| 815 | for ($i = $top['where']; $i <= $c; ++$i) |
||
| 816 | $chrs = substr_replace($chrs, ' ', $i, 1); |
||
| 817 | |||
| 818 | //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 819 | |||
| 820 | } |
||
| 821 | |||
| 822 | } |
||
| 823 | |||
| 824 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
| 825 | return $arr; |
||
| 826 | |||
| 827 | } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
||
| 828 | return $obj; |
||
| 829 | |||
| 830 | } |
||
| 831 | |||
| 832 | } |
||
| 833 | } |
||
| 834 | } |
||
| 835 | |||
| 853 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.