| Conditions | 67 |
| Paths | 187 |
| Total Lines | 246 |
| Code Lines | 153 |
| Lines | 0 |
| Ratio | 0 % |
| 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 declare(strict_types=1); |
||
| 459 | public function decode($str) |
||
| 460 | { |
||
| 461 | $str = $this->reduce_string($str); |
||
| 462 | |||
| 463 | switch (mb_strtolower($str)) { |
||
| 464 | case 'true': |
||
| 465 | return true; |
||
| 466 | case 'false': |
||
| 467 | return false; |
||
| 468 | case 'null': |
||
| 469 | return null; |
||
| 470 | default: |
||
| 471 | $m = []; |
||
| 472 | |||
| 473 | if (is_numeric($str)) { |
||
| 474 | // Lookie-loo, it's a number |
||
| 475 | |||
| 476 | // This would work on its own, but I'm trying to be |
||
| 477 | // good about returning integers where appropriate: |
||
| 478 | // return (float)$str; |
||
| 479 | |||
| 480 | // Return float or int, as appropriate |
||
| 481 | return ((float)$str == (int)$str) ? (int)$str : (float)$str; |
||
| 482 | } |
||
| 483 | |||
| 484 | if (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { |
||
| 485 | // STRINGS RETURNED IN UTF-8 FORMAT |
||
| 486 | $delim = mb_substr($str, 0, 1); |
||
| 487 | $chrs = mb_substr($str, 1, -1); |
||
| 488 | $utf8 = ''; |
||
| 489 | $strlen_chrs = mb_strlen($chrs); |
||
| 490 | |||
| 491 | for ($c = 0; $c < $strlen_chrs; ++$c) { |
||
| 492 | $substr_chrs_c_2 = mb_substr($chrs, $c, 2); |
||
| 493 | $ord_chrs_c = ord($chrs[$c]); |
||
| 494 | |||
| 495 | switch (true) { |
||
| 496 | case '\b' === $substr_chrs_c_2: |
||
| 497 | $utf8 .= chr(0x08); |
||
| 498 | ++$c; |
||
| 499 | break; |
||
| 500 | case '\t' === $substr_chrs_c_2: |
||
| 501 | $utf8 .= chr(0x09); |
||
| 502 | ++$c; |
||
| 503 | break; |
||
| 504 | case '\n' === $substr_chrs_c_2: |
||
| 505 | $utf8 .= chr(0x0A); |
||
| 506 | ++$c; |
||
| 507 | break; |
||
| 508 | case '\f' === $substr_chrs_c_2: |
||
| 509 | $utf8 .= chr(0x0C); |
||
| 510 | ++$c; |
||
| 511 | break; |
||
| 512 | case '\r' === $substr_chrs_c_2: |
||
| 513 | $utf8 .= chr(0x0D); |
||
| 514 | ++$c; |
||
| 515 | break; |
||
| 516 | case '\\"' === $substr_chrs_c_2: |
||
| 517 | case '\\\'' === $substr_chrs_c_2: |
||
| 518 | case '\\\\' === $substr_chrs_c_2: |
||
| 519 | case '\\/' === $substr_chrs_c_2: |
||
| 520 | if (('"' === $delim && '\\\'' !== $substr_chrs_c_2) |
||
| 521 | || ("'" === $delim && '\\"' !== $substr_chrs_c_2)) { |
||
| 522 | $utf8 .= $chrs[++$c]; |
||
| 523 | } |
||
| 524 | break; |
||
| 525 | case preg_match('/\\\u[0-9A-F]{4}/i', mb_substr($chrs, $c, 6)): |
||
| 526 | // single, escaped unicode character |
||
| 527 | $utf16 = chr(hexdec(mb_substr($chrs, $c + 2, 2))) . chr(hexdec(mb_substr($chrs, $c + 4, 2))); |
||
| 528 | $utf8 .= $this->utf162utf8($utf16); |
||
| 529 | $c += 5; |
||
| 530 | break; |
||
| 531 | case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): |
||
| 532 | $utf8 .= $chrs[$c]; |
||
| 533 | break; |
||
| 534 | case 0xC0 == ($ord_chrs_c & 0xE0): |
||
| 535 | // characters U-00000080 - U-000007FF, mask 110SONGLIST |
||
| 536 | //see https://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 537 | $utf8 .= mb_substr($chrs, $c, 2); |
||
| 538 | ++$c; |
||
| 539 | break; |
||
| 540 | case 0xE0 == ($ord_chrs_c & 0xF0): |
||
| 541 | // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
||
| 542 | // see https://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 543 | $utf8 .= mb_substr($chrs, $c, 3); |
||
| 544 | $c += 2; |
||
| 545 | break; |
||
| 546 | case 0xF0 == ($ord_chrs_c & 0xF8): |
||
| 547 | // characters U-00010000 - U-001FFFFF, mask 11110XXX |
||
| 548 | // see https://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 549 | $utf8 .= mb_substr($chrs, $c, 4); |
||
| 550 | $c += 3; |
||
| 551 | break; |
||
| 552 | case 0xF8 == ($ord_chrs_c & 0xFC): |
||
| 553 | // characters U-00200000 - U-03FFFFFF, mask 111110XX |
||
| 554 | // see https://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 555 | $utf8 .= mb_substr($chrs, $c, 5); |
||
| 556 | $c += 4; |
||
| 557 | break; |
||
| 558 | case 0xFC == ($ord_chrs_c & 0xFE): |
||
| 559 | // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
||
| 560 | // see https://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
| 561 | $utf8 .= mb_substr($chrs, $c, 6); |
||
| 562 | $c += 5; |
||
| 563 | break; |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | return $utf8; |
||
| 568 | } |
||
| 569 | |||
| 570 | if (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { |
||
| 571 | // array, or object notation |
||
| 572 | |||
| 573 | if ('[' === $str[0]) { |
||
| 574 | $stk = [SERVICES_JSON_IN_ARR]; |
||
| 575 | $arr = []; |
||
| 576 | } elseif ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
| 577 | $stk = [SERVICES_JSON_IN_OBJ]; |
||
| 578 | $obj = []; |
||
| 579 | } else { |
||
| 580 | $stk = [SERVICES_JSON_IN_OBJ]; |
||
| 581 | $obj = new stdClass(); |
||
| 582 | } |
||
| 583 | |||
| 584 | array_push( |
||
| 585 | $stk, |
||
| 586 | [ |
||
| 587 | 'what' => SERVICES_JSON_SLICE, |
||
| 588 | 'where' => 0, |
||
| 589 | 'delim' => false, |
||
| 590 | ] |
||
| 591 | ); |
||
| 592 | |||
| 593 | $chrs = mb_substr($str, 1, -1); |
||
| 594 | $chrs = $this->reduce_string($chrs); |
||
| 595 | |||
| 596 | if ('' == $chrs) { |
||
| 597 | if (SERVICES_JSON_IN_ARR == reset($stk)) { |
||
| 598 | return $arr; |
||
| 599 | } |
||
| 600 | |||
| 601 | return $obj; |
||
| 602 | } |
||
| 603 | |||
| 604 | //print("\nparsing {$chrs}\n"); |
||
| 605 | |||
| 606 | $strlen_chrs = mb_strlen($chrs); |
||
| 607 | |||
| 608 | for ($c = 0; $c <= $strlen_chrs; ++$c) { |
||
| 609 | $top = end($stk); |
||
| 610 | $substr_chrs_c_2 = mb_substr($chrs, $c, 2); |
||
| 611 | |||
| 612 | if (($c == $strlen_chrs) || ((',' === $chrs[$c]) && (SERVICES_JSON_SLICE == $top['what']))) { |
||
| 613 | // found a comma that is not inside a string, array, etc., |
||
| 614 | // OR we've reached the end of the character list |
||
| 615 | $slice = mb_substr($chrs, $top['where'], $c - $top['where']); |
||
| 616 | array_push($stk, ['what' => SERVICES_JSON_SLICE, 'where' => $c + 1, 'delim' => false]); |
||
| 617 | //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 618 | |||
| 619 | if (SERVICES_JSON_IN_ARR == reset($stk)) { |
||
| 620 | // we are in an array, so just push an element onto the stack |
||
| 621 | $arr[] = $this->decode($slice); |
||
| 622 | } elseif (SERVICES_JSON_IN_OBJ == reset($stk)) { |
||
| 623 | // we are in an object, so figure |
||
| 624 | // out the property name and set an |
||
| 625 | // element in an associative array, |
||
| 626 | // for now |
||
| 627 | $parts = []; |
||
| 628 | |||
| 629 | if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
| 630 | // "name":value pair |
||
| 631 | $key = $this->decode($parts[1]); |
||
| 632 | $val = $this->decode($parts[2]); |
||
| 633 | |||
| 634 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
| 635 | $obj[$key] = $val; |
||
| 636 | } else { |
||
| 637 | $obj->$key = $val; |
||
| 638 | } |
||
| 639 | } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
| 640 | // name:value pair, where name is unquoted |
||
| 641 | $key = $parts[1]; |
||
| 642 | $val = $this->decode($parts[2]); |
||
| 643 | |||
| 644 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
| 645 | $obj[$key] = $val; |
||
| 646 | } else { |
||
| 647 | $obj->$key = $val; |
||
| 648 | } |
||
| 649 | } |
||
| 650 | } |
||
| 651 | } elseif ((('"' === $chrs[$c]) || ("'" === $chrs[$c])) && (SERVICES_JSON_IN_STR != $top['what'])) { |
||
| 652 | // found a quote, and we are not inside a string |
||
| 653 | array_push($stk, ['what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]]); |
||
| 654 | //print("Found start of string at {$c}\n"); |
||
| 655 | } elseif (($chrs[$c] == $top['delim']) |
||
| 656 | && (SERVICES_JSON_IN_STR == $top['what']) |
||
| 657 | && (1 != (mb_strlen(mb_substr($chrs, 0, $c)) - mb_strlen(rtrim(mb_substr($chrs, 0, $c), '\\'))) % 2)) { |
||
| 658 | // found a quote, we're in a string, and it's not escaped |
||
| 659 | // we know that it's not escaped becase there is _not_ an |
||
| 660 | // odd number of backslashes at the end of the string so far |
||
| 661 | array_pop($stk); |
||
| 662 | //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
||
| 663 | } elseif (('[' === $chrs[$c]) |
||
| 664 | && in_array($top['what'], [SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ], true)) { |
||
| 665 | // found a left-bracket, and we are in an array, object, or slice |
||
| 666 | array_push($stk, ['what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false]); |
||
| 667 | //print("Found start of array at {$c}\n"); |
||
| 668 | } elseif ((']' === $chrs[$c]) && (SERVICES_JSON_IN_ARR == $top['what'])) { |
||
| 669 | // found a right-bracket, and we're in an array |
||
| 670 | array_pop($stk); |
||
| 671 | //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 672 | } elseif (('{' === $chrs[$c]) |
||
| 673 | && in_array($top['what'], [SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ], true)) { |
||
| 674 | // found a left-brace, and we are in an array, object, or slice |
||
| 675 | array_push($stk, ['what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false]); |
||
| 676 | //print("Found start of object at {$c}\n"); |
||
| 677 | } elseif (('}' === $chrs[$c]) && (SERVICES_JSON_IN_OBJ == $top['what'])) { |
||
| 678 | // found a right-brace, and we're in an object |
||
| 679 | array_pop($stk); |
||
| 680 | //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 681 | } elseif (('/*' === $substr_chrs_c_2) |
||
| 682 | && in_array($top['what'], [SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ], true)) { |
||
| 683 | // found a comment start, and we are in an array, object, or slice |
||
| 684 | array_push($stk, ['what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false]); |
||
| 685 | ++$c; |
||
| 686 | //print("Found start of comment at {$c}\n"); |
||
| 687 | } elseif (('*/' === $substr_chrs_c_2) && (SERVICES_JSON_IN_CMT == $top['what'])) { |
||
| 688 | // found a comment end, and we're in one now |
||
| 689 | array_pop($stk); |
||
| 690 | ++$c; |
||
| 691 | |||
| 692 | for ($i = $top['where']; $i <= $c; ++$i) { |
||
| 693 | $chrs = substr_replace($chrs, ' ', $i, 1); |
||
| 694 | } |
||
| 695 | //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
| 696 | } |
||
| 697 | } |
||
| 698 | |||
| 699 | if (SERVICES_JSON_IN_ARR == reset($stk)) { |
||
| 700 | return $arr; |
||
| 701 | } |
||
| 702 | |||
| 703 | if (SERVICES_JSON_IN_OBJ == reset($stk)) { |
||
| 704 | return $obj; |
||
| 705 | } |
||
| 780 |