| Conditions | 67 |
| Paths | 187 |
| Total Lines | 257 |
| Lines | 59 |
| Ratio | 22.96 % |
| 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 |
||
| 499 | public function decode($str) |
||
| 500 | { |
||
| 501 | $str = $this->reduce_string($str); |
||
| 502 | |||
| 503 | switch (strtolower($str)) { |
||
| 504 | case 'true': |
||
| 505 | return true; |
||
| 506 | |||
| 507 | case 'false': |
||
| 508 | return false; |
||
| 509 | |||
| 510 | case 'null': |
||
| 511 | return null; |
||
| 512 | |||
| 513 | default: |
||
| 514 | $m = array(); |
||
| 515 | |||
| 516 | if (is_numeric($str)) { |
||
| 517 | // Lookie-loo, it's a number |
||
| 518 | |||
| 519 | // This would work on its own, but I'm trying to be |
||
| 520 | // good about returning integers where appropriate: |
||
| 521 | // return (float)$str; |
||
| 522 | |||
| 523 | // Return float or int, as appropriate |
||
| 524 | return ((float)$str == (integer)$str) |
||
| 525 | ? (integer)$str |
||
| 526 | : (float)$str; |
||
| 527 | } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { |
||
| 528 | // STRINGS RETURNED IN UTF-8 FORMAT |
||
| 529 | $delim = substr($str, 0, 1); |
||
| 530 | $chrs = substr($str, 1, -1); |
||
| 531 | $utf8 = ''; |
||
| 532 | $strlen_chrs = strlen($chrs); |
||
| 533 | |||
| 534 | for ($c = 0; $c < $strlen_chrs; ++$c) { |
||
| 535 | $substr_chrs_c_2 = substr($chrs, $c, 2); |
||
| 536 | $ord_chrs_c = ord($chrs{$c}); |
||
| 537 | |||
| 538 | switch (true) { |
||
| 539 | case $substr_chrs_c_2 == '\b': |
||
| 540 | $utf8 .= chr(0x08); |
||
| 541 | ++$c; |
||
| 542 | break; |
||
| 543 | case $substr_chrs_c_2 == '\t': |
||
| 544 | $utf8 .= chr(0x09); |
||
| 545 | ++$c; |
||
| 546 | break; |
||
| 547 | case $substr_chrs_c_2 == '\n': |
||
| 548 | $utf8 .= chr(0x0A); |
||
| 549 | ++$c; |
||
| 550 | break; |
||
| 551 | case $substr_chrs_c_2 == '\f': |
||
| 552 | $utf8 .= chr(0x0C); |
||
| 553 | ++$c; |
||
| 554 | break; |
||
| 555 | case $substr_chrs_c_2 == '\r': |
||
| 556 | $utf8 .= chr(0x0D); |
||
| 557 | ++$c; |
||
| 558 | break; |
||
| 559 | |||
| 560 | case $substr_chrs_c_2 == '\\"': |
||
| 561 | case $substr_chrs_c_2 == '\\\'': |
||
| 562 | case $substr_chrs_c_2 == '\\\\': |
||
| 563 | case $substr_chrs_c_2 == '\\/': |
||
| 564 | if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
||
| 565 | ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
||
| 566 | $utf8 .= $chrs{++$c}; |
||
| 567 | } |
||
| 568 | break; |
||
| 569 | |||
| 570 | case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
||
| 571 | // single, escaped unicode character |
||
| 572 | $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) |
||
| 573 | . chr(hexdec(substr($chrs, ($c + 4), 2))); |
||
| 574 | $utf8 .= $this->utf162utf8($utf16); |
||
| 575 | $c += 5; |
||
| 576 | break; |
||
| 577 | |||
| 578 | case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): |
||
| 579 | $utf8 .= $chrs{$c}; |
||
| 580 | break; |
||
| 581 | |||
| 582 | View Code Duplication | case ($ord_chrs_c & 0xE0) == 0xC0: |
|
| 583 | // characters U-00000080 - U-000007FF, mask 110XXXXX |
||
| 584 | //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 585 | $utf8 .= substr($chrs, $c, 2); |
||
| 586 | ++$c; |
||
| 587 | break; |
||
| 588 | |||
| 589 | View Code Duplication | case ($ord_chrs_c & 0xF0) == 0xE0: |
|
| 590 | // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
||
| 591 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 592 | $utf8 .= substr($chrs, $c, 3); |
||
| 593 | $c += 2; |
||
| 594 | break; |
||
| 595 | |||
| 596 | View Code Duplication | case ($ord_chrs_c & 0xF8) == 0xF0: |
|
| 597 | // characters U-00010000 - U-001FFFFF, mask 11110XXX |
||
| 598 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 599 | $utf8 .= substr($chrs, $c, 4); |
||
| 600 | $c += 3; |
||
| 601 | break; |
||
| 602 | |||
| 603 | View Code Duplication | case ($ord_chrs_c & 0xFC) == 0xF8: |
|
| 604 | // characters U-00200000 - U-03FFFFFF, mask 111110XX |
||
| 605 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 606 | $utf8 .= substr($chrs, $c, 5); |
||
| 607 | $c += 4; |
||
| 608 | break; |
||
| 609 | |||
| 610 | View Code Duplication | case ($ord_chrs_c & 0xFE) == 0xFC: |
|
| 611 | // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
||
| 612 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 613 | $utf8 .= substr($chrs, $c, 6); |
||
| 614 | $c += 5; |
||
| 615 | break; |
||
| 616 | |||
| 617 | } |
||
| 618 | } |
||
| 619 | |||
| 620 | return $utf8; |
||
| 621 | } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { |
||
| 622 | // array, or object notation |
||
| 623 | |||
| 624 | if ($str{0} == '[') { |
||
| 625 | $stk = array(SERVICES_JSON_IN_ARR); |
||
| 626 | $arr = array(); |
||
| 627 | } else { |
||
| 628 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
| 629 | $stk = array(SERVICES_JSON_IN_OBJ); |
||
| 630 | $obj = array(); |
||
| 631 | } else { |
||
| 632 | $stk = array(SERVICES_JSON_IN_OBJ); |
||
| 633 | $obj = new stdClass(); |
||
| 634 | } |
||
| 635 | } |
||
| 636 | |||
| 637 | array_push($stk, array('what' => SERVICES_JSON_SLICE, |
||
| 638 | 'where' => 0, |
||
| 639 | 'delim' => false)); |
||
| 640 | |||
| 641 | $chrs = substr($str, 1, -1); |
||
| 642 | $chrs = $this->reduce_string($chrs); |
||
| 643 | |||
| 644 | if ($chrs == '') { |
||
| 645 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
| 646 | return $arr; |
||
| 647 | } else { |
||
| 648 | return $obj; |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | //print("\nparsing {$chrs}\n"); |
||
| 653 | |||
| 654 | $strlen_chrs = strlen($chrs); |
||
| 655 | |||
| 656 | for ($c = 0; $c <= $strlen_chrs; ++$c) { |
||
| 657 | $top = end($stk); |
||
| 658 | $substr_chrs_c_2 = substr($chrs, $c, 2); |
||
| 659 | |||
| 660 | if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
||
| 661 | // found a comma that is not inside a string, array, etc., |
||
| 662 | // OR we've reached the end of the character list |
||
| 663 | $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
||
| 664 | array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
||
| 665 | //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 666 | |||
| 667 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
| 668 | // we are in an array, so just push an element onto the stack |
||
| 669 | array_push($arr, $this->decode($slice)); |
||
| 670 | } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
||
| 671 | // we are in an object, so figure |
||
| 672 | // out the property name and set an |
||
| 673 | // element in an associative array, |
||
| 674 | // for now |
||
| 675 | $parts = array(); |
||
| 676 | |||
| 677 | if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
| 678 | // "name":value pair |
||
| 679 | $key = $this->decode($parts[1]); |
||
| 680 | $val = $this->decode($parts[2]); |
||
| 681 | |||
| 682 | View Code Duplication | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
|
| 683 | $obj[$key] = $val; |
||
| 684 | } else { |
||
| 685 | $obj->$key = $val; |
||
| 686 | } |
||
| 687 | } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
| 688 | // name:value pair, where name is unquoted |
||
| 689 | $key = $parts[1]; |
||
| 690 | $val = $this->decode($parts[2]); |
||
| 691 | |||
| 692 | View Code Duplication | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
|
| 693 | $obj[$key] = $val; |
||
| 694 | } else { |
||
| 695 | $obj->$key = $val; |
||
| 696 | } |
||
| 697 | } |
||
| 698 | } |
||
| 699 | } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
||
| 700 | // found a quote, and we are not inside a string |
||
| 701 | array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); |
||
| 702 | // print("Found start of string at {$c}\n"); |
||
| 703 | } elseif (($chrs{$c} == $top['delim']) && |
||
| 704 | ($top['what'] == SERVICES_JSON_IN_STR) && |
||
| 705 | ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
||
| 706 | // found a quote, we're in a string, and it's not escaped |
||
| 707 | // we know that it's not escaped becase there is _not_ an |
||
| 708 | // odd number of backslashes at the end of the string so far |
||
| 709 | array_pop($stk); |
||
| 710 | // print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
||
| 711 | View Code Duplication | } elseif (($chrs{$c} == '[') && |
|
| 712 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
| 713 | // found a left-bracket, and we are in an array, object, or slice |
||
| 714 | array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
||
| 715 | // print("Found start of array at {$c}\n"); |
||
| 716 | } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
||
| 717 | // found a right-bracket, and we're in an array |
||
| 718 | array_pop($stk); |
||
| 719 | // print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 720 | View Code Duplication | } elseif (($chrs{$c} == '{') && |
|
| 721 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
| 722 | // found a left-brace, and we are in an array, object, or slice |
||
| 723 | array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
||
| 724 | //print("Found start of object at {$c}\n"); |
||
| 725 | } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { |
||
| 726 | // found a right-brace, and we're in an object |
||
| 727 | array_pop($stk); |
||
| 728 | //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 729 | View Code Duplication | } elseif (($substr_chrs_c_2 == '/*') && |
|
| 730 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
| 731 | // found a comment start, and we are in an array, object, or slice |
||
| 732 | array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); |
||
| 733 | $c++; |
||
| 734 | // print("Found start of comment at {$c}\n"); |
||
| 735 | } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { |
||
| 736 | // found a comment end, and we're in one now |
||
| 737 | array_pop($stk); |
||
| 738 | $c++; |
||
| 739 | |||
| 740 | for ($i = $top['where']; $i <= $c; ++$i) { |
||
| 741 | $chrs = substr_replace($chrs, ' ', $i, 1); |
||
| 742 | } |
||
| 743 | |||
| 744 | //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 745 | } |
||
| 746 | } |
||
| 747 | |||
| 748 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
| 749 | return $arr; |
||
| 750 | } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
||
| 751 | return $obj; |
||
| 752 | } |
||
| 753 | } |
||
| 754 | } |
||
| 755 | } |
||
| 756 | |||
| 803 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: