Total Complexity | 122 |
Total Lines | 661 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Services_JSON often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Services_JSON, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
115 | class Services_JSON |
||
116 | { |
||
117 | /** |
||
118 | * constructs a new JSON instance |
||
119 | * |
||
120 | * @param int $use object behavior flags; combine with boolean-OR |
||
121 | * |
||
122 | * possible values: |
||
123 | * - SERVICES_JSON_LOOSE_TYPE: loose typing. |
||
124 | * "{...}" syntax creates associative arrays |
||
125 | * instead of objects in decode(). |
||
126 | * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression. |
||
127 | * Values which can't be encoded (e.g. resources) |
||
128 | * appear as NULL instead of throwing errors. |
||
129 | * By default, a deeply-nested resource will |
||
130 | * bubble up with an error, so all return values |
||
131 | * from encode() should be checked with isError() |
||
132 | */ |
||
133 | function Services_JSON($use = 0) |
||
|
|||
134 | { |
||
135 | $this->use = $use; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * convert a string from one UTF-16 char to one UTF-8 char |
||
140 | * |
||
141 | * Normally should be handled by mb_convert_encoding, but |
||
142 | * provides a slower PHP-only method for installations |
||
143 | * that lack the multibye string extension. |
||
144 | * |
||
145 | * @param string $utf16 UTF-16 character |
||
146 | * @return string UTF-8 character |
||
147 | * @access private |
||
148 | */ |
||
149 | function utf162utf8($utf16) |
||
150 | { |
||
151 | // oh please oh please oh please oh please oh please |
||
152 | if(function_exists('mb_convert_encoding')) { |
||
153 | return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); |
||
154 | } |
||
155 | |||
156 | $bytes = (ord($utf16{0}) << 8) | ord($utf16{1}); |
||
157 | |||
158 | switch(true) { |
||
159 | case ((0x7F & $bytes) == $bytes): |
||
160 | // this case should never be reached, because we are in ASCII range |
||
161 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
162 | return chr(0x7F & $bytes); |
||
163 | |||
164 | case (0x07FF & $bytes) == $bytes: |
||
165 | // return a 2-byte UTF-8 character |
||
166 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
167 | return chr(0xC0 | (($bytes >> 6) & 0x1F)) |
||
168 | . chr(0x80 | ($bytes & 0x3F)); |
||
169 | |||
170 | case (0xFFFF & $bytes) == $bytes: |
||
171 | // return a 3-byte UTF-8 character |
||
172 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
173 | return chr(0xE0 | (($bytes >> 12) & 0x0F)) |
||
174 | . chr(0x80 | (($bytes >> 6) & 0x3F)) |
||
175 | . chr(0x80 | ($bytes & 0x3F)); |
||
176 | } |
||
177 | |||
178 | // ignoring UTF-32 for now, sorry |
||
179 | return ''; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * convert a string from one UTF-8 char to one UTF-16 char |
||
184 | * |
||
185 | * Normally should be handled by mb_convert_encoding, but |
||
186 | * provides a slower PHP-only method for installations |
||
187 | * that lack the multibye string extension. |
||
188 | * |
||
189 | * @param string $utf8 UTF-8 character |
||
190 | * @return string UTF-16 character |
||
191 | * @access private |
||
192 | */ |
||
193 | function utf82utf16($utf8) |
||
194 | { |
||
195 | // oh please oh please oh please oh please oh please |
||
196 | if(function_exists('mb_convert_encoding')) { |
||
197 | return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); |
||
198 | } |
||
199 | |||
200 | switch(strlen($utf8)) { |
||
201 | case 1: |
||
202 | // this case should never be reached, because we are in ASCII range |
||
203 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
204 | return $utf8; |
||
205 | |||
206 | case 2: |
||
207 | // return a UTF-16 character from a 2-byte UTF-8 char |
||
208 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
209 | return chr(0x07 & (ord($utf8{0}) >> 2)) |
||
210 | . chr((0xC0 & (ord($utf8{0}) << 6)) |
||
211 | | (0x3F & ord($utf8{1}))); |
||
212 | |||
213 | case 3: |
||
214 | // return a UTF-16 character from a 3-byte UTF-8 char |
||
215 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
216 | return chr((0xF0 & (ord($utf8{0}) << 4)) |
||
217 | | (0x0F & (ord($utf8{1}) >> 2))) |
||
218 | . chr((0xC0 & (ord($utf8{1}) << 6)) |
||
219 | | (0x7F & ord($utf8{2}))); |
||
220 | } |
||
221 | |||
222 | // ignoring UTF-32 for now, sorry |
||
223 | return ''; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * encodes an arbitrary variable into JSON format |
||
228 | * |
||
229 | * @param mixed $var any number, boolean, string, array, or object to be encoded. |
||
230 | * see argument 1 to Services_JSON() above for array-parsing behavior. |
||
231 | * if var is a strng, note that encode() always expects it |
||
232 | * to be in ASCII or UTF-8 format! |
||
233 | * |
||
234 | * @return mixed JSON string representation of input var or an error if a problem occurs |
||
235 | * @access public |
||
236 | */ |
||
237 | function encode($var) |
||
422 | } |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * array-walking function for use in generating JSON-formatted name-value pairs |
||
427 | * |
||
428 | * @param string $name name of key to use |
||
429 | * @param mixed $value reference to an array element to be encoded |
||
430 | * |
||
431 | * @return string JSON-formatted name-value pair, like '"name":value' |
||
432 | * @access private |
||
433 | */ |
||
434 | function name_value($name, $value) |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * reduce a string by removing leading and trailing comments and whitespace |
||
447 | * |
||
448 | * @param $str string string value to strip of comments and whitespace |
||
449 | * |
||
450 | * @return string string value stripped of comments and whitespace |
||
451 | * @access private |
||
452 | */ |
||
453 | function reduce_string($str) |
||
454 | { |
||
455 | $str = preg_replace(array( |
||
456 | |||
457 | // eliminate single line comments in '// ...' form |
||
458 | '#^\s*//(.+)$#m', |
||
459 | |||
460 | // eliminate multi-line comments in '/* ... */' form, at start of string |
||
461 | '#^\s*/\*(.+)\*/#Us', |
||
462 | |||
463 | // eliminate multi-line comments in '/* ... */' form, at end of string |
||
464 | '#/\*(.+)\*/\s*$#Us' |
||
465 | |||
466 | ), '', $str); |
||
467 | |||
468 | // eliminate extraneous space |
||
469 | return trim($str); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * decodes a JSON string into appropriate variable |
||
474 | * |
||
475 | * @param string $str JSON-formatted string |
||
476 | * |
||
477 | * @return mixed number, boolean, string, array, or object |
||
478 | * corresponding to given JSON input string. |
||
479 | * See argument 1 to Services_JSON() above for object-output behavior. |
||
480 | * Note that decode() always returns strings |
||
481 | * in ASCII or UTF-8 format! |
||
482 | * @access public |
||
483 | */ |
||
484 | function decode($str) |
||
485 | { |
||
486 | $str = $this->reduce_string($str); |
||
487 | |||
488 | switch (strtolower($str)) { |
||
489 | case 'true': |
||
490 | return true; |
||
491 | |||
492 | case 'false': |
||
493 | return false; |
||
494 | |||
495 | case 'null': |
||
496 | return null; |
||
497 | |||
498 | default: |
||
499 | $m = array(); |
||
500 | |||
501 | if (is_numeric($str)) { |
||
502 | // Lookie-loo, it's a number |
||
503 | |||
504 | // This would work on its own, but I'm trying to be |
||
505 | // good about returning integers where appropriate: |
||
506 | // return (float)$str; |
||
507 | |||
508 | // Return float or int, as appropriate |
||
509 | return ((float)$str == (integer)$str) |
||
510 | ? (integer)$str |
||
511 | : (float)$str; |
||
512 | |||
513 | } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { |
||
514 | // STRINGS RETURNED IN UTF-8 FORMAT |
||
515 | $delim = substr($str, 0, 1); |
||
516 | $chrs = substr($str, 1, -1); |
||
517 | $utf8 = ''; |
||
518 | $strlen_chrs = strlen($chrs); |
||
519 | |||
520 | for ($c = 0; $c < $strlen_chrs; ++$c) { |
||
521 | |||
522 | $substr_chrs_c_2 = substr($chrs, $c, 2); |
||
523 | $ord_chrs_c = ord($chrs{$c}); |
||
524 | |||
525 | switch (true) { |
||
526 | case $substr_chrs_c_2 == '\b': |
||
527 | $utf8 .= chr(0x08); |
||
528 | ++$c; |
||
529 | break; |
||
530 | case $substr_chrs_c_2 == '\t': |
||
531 | $utf8 .= chr(0x09); |
||
532 | ++$c; |
||
533 | break; |
||
534 | case $substr_chrs_c_2 == '\n': |
||
535 | $utf8 .= chr(0x0A); |
||
536 | ++$c; |
||
537 | break; |
||
538 | case $substr_chrs_c_2 == '\f': |
||
539 | $utf8 .= chr(0x0C); |
||
540 | ++$c; |
||
541 | break; |
||
542 | case $substr_chrs_c_2 == '\r': |
||
543 | $utf8 .= chr(0x0D); |
||
544 | ++$c; |
||
545 | break; |
||
546 | |||
547 | case $substr_chrs_c_2 == '\\"': |
||
548 | case $substr_chrs_c_2 == '\\\'': |
||
549 | case $substr_chrs_c_2 == '\\\\': |
||
550 | case $substr_chrs_c_2 == '\\/': |
||
551 | if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
||
552 | ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
||
553 | $utf8 .= $chrs{++$c}; |
||
554 | } |
||
555 | break; |
||
556 | |||
557 | case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
||
558 | // single, escaped unicode character |
||
559 | $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) |
||
560 | . chr(hexdec(substr($chrs, ($c + 4), 2))); |
||
561 | $utf8 .= $this->utf162utf8($utf16); |
||
562 | $c += 5; |
||
563 | break; |
||
564 | |||
565 | case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): |
||
566 | $utf8 .= $chrs{$c}; |
||
567 | break; |
||
568 | |||
569 | case ($ord_chrs_c & 0xE0) == 0xC0: |
||
570 | // characters U-00000080 - U-000007FF, mask 110XXXXX |
||
571 | //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
572 | $utf8 .= substr($chrs, $c, 2); |
||
573 | ++$c; |
||
574 | break; |
||
575 | |||
576 | case ($ord_chrs_c & 0xF0) == 0xE0: |
||
577 | // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
||
578 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
579 | $utf8 .= substr($chrs, $c, 3); |
||
580 | $c += 2; |
||
581 | break; |
||
582 | |||
583 | case ($ord_chrs_c & 0xF8) == 0xF0: |
||
584 | // characters U-00010000 - U-001FFFFF, mask 11110XXX |
||
585 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
586 | $utf8 .= substr($chrs, $c, 4); |
||
587 | $c += 3; |
||
588 | break; |
||
589 | |||
590 | case ($ord_chrs_c & 0xFC) == 0xF8: |
||
591 | // characters U-00200000 - U-03FFFFFF, mask 111110XX |
||
592 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
593 | $utf8 .= substr($chrs, $c, 5); |
||
594 | $c += 4; |
||
595 | break; |
||
596 | |||
597 | case ($ord_chrs_c & 0xFE) == 0xFC: |
||
598 | // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
||
599 | // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
||
600 | $utf8 .= substr($chrs, $c, 6); |
||
601 | $c += 5; |
||
602 | break; |
||
603 | |||
604 | } |
||
605 | |||
606 | } |
||
607 | |||
608 | return $utf8; |
||
609 | |||
610 | } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { |
||
611 | // array, or object notation |
||
612 | |||
613 | if ($str{0} == '[') { |
||
614 | $stk = array(SERVICES_JSON_IN_ARR); |
||
615 | $arr = array(); |
||
616 | } else { |
||
617 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
618 | $stk = array(SERVICES_JSON_IN_OBJ); |
||
619 | $obj = array(); |
||
620 | } else { |
||
621 | $stk = array(SERVICES_JSON_IN_OBJ); |
||
622 | $obj = new stdClass(); |
||
623 | } |
||
624 | } |
||
625 | |||
626 | array_push($stk, array('what' => SERVICES_JSON_SLICE, |
||
627 | 'where' => 0, |
||
628 | 'delim' => false)); |
||
629 | |||
630 | $chrs = substr($str, 1, -1); |
||
631 | $chrs = $this->reduce_string($chrs); |
||
632 | |||
633 | if ($chrs == '') { |
||
634 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
635 | return $arr; |
||
636 | |||
637 | } else { |
||
638 | return $obj; |
||
639 | |||
640 | } |
||
641 | } |
||
642 | |||
643 | //print("\nparsing {$chrs}\n"); |
||
644 | |||
645 | $strlen_chrs = strlen($chrs); |
||
646 | |||
647 | for ($c = 0; $c <= $strlen_chrs; ++$c) { |
||
648 | |||
649 | $top = end($stk); |
||
650 | $substr_chrs_c_2 = substr($chrs, $c, 2); |
||
651 | |||
652 | if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
||
653 | // found a comma that is not inside a string, array, etc., |
||
654 | // OR we've reached the end of the character list |
||
655 | $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
||
656 | array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
||
657 | //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
658 | |||
659 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
660 | // we are in an array, so just push an element onto the stack |
||
661 | array_push($arr, $this->decode($slice)); |
||
662 | |||
663 | } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
||
664 | // we are in an object, so figure |
||
665 | // out the property name and set an |
||
666 | // element in an associative array, |
||
667 | // for now |
||
668 | $parts = array(); |
||
669 | |||
670 | if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
671 | // "name":value pair |
||
672 | $key = $this->decode($parts[1]); |
||
673 | $val = $this->decode($parts[2]); |
||
674 | |||
675 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
676 | $obj[$key] = $val; |
||
677 | } else { |
||
678 | $obj->$key = $val; |
||
679 | } |
||
680 | } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
||
681 | // name:value pair, where name is unquoted |
||
682 | $key = $parts[1]; |
||
683 | $val = $this->decode($parts[2]); |
||
684 | |||
685 | if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
||
686 | $obj[$key] = $val; |
||
687 | } else { |
||
688 | $obj->$key = $val; |
||
689 | } |
||
690 | } |
||
691 | |||
692 | } |
||
693 | |||
694 | } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
||
695 | // found a quote, and we are not inside a string |
||
696 | array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); |
||
697 | //print("Found start of string at {$c}\n"); |
||
698 | |||
699 | } elseif (($chrs{$c} == $top['delim']) && |
||
700 | ($top['what'] == SERVICES_JSON_IN_STR) && |
||
701 | ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
||
702 | // found a quote, we're in a string, and it's not escaped |
||
703 | // we know that it's not escaped becase there is _not_ an |
||
704 | // odd number of backslashes at the end of the string so far |
||
705 | array_pop($stk); |
||
706 | //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
||
707 | |||
708 | } elseif (($chrs{$c} == '[') && |
||
709 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
710 | // found a left-bracket, and we are in an array, object, or slice |
||
711 | array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
||
712 | //print("Found start of array at {$c}\n"); |
||
713 | |||
714 | } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
||
715 | // found a right-bracket, and we're in an array |
||
716 | array_pop($stk); |
||
717 | //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
718 | |||
719 | } elseif (($chrs{$c} == '{') && |
||
720 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
721 | // found a left-brace, and we are in an array, object, or slice |
||
722 | array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
||
723 | //print("Found start of object at {$c}\n"); |
||
724 | |||
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 | |||
730 | } elseif (($substr_chrs_c_2 == '/*') && |
||
731 | in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
||
732 | // found a comment start, and we are in an array, object, or slice |
||
733 | array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); |
||
734 | $c++; |
||
735 | //print("Found start of comment at {$c}\n"); |
||
736 | |||
737 | } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { |
||
738 | // found a comment end, and we're in one now |
||
739 | array_pop($stk); |
||
740 | $c++; |
||
741 | |||
742 | for ($i = $top['where']; $i <= $c; ++$i) |
||
743 | $chrs = substr_replace($chrs, ' ', $i, 1); |
||
744 | |||
745 | //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
||
746 | |||
747 | } |
||
748 | |||
749 | } |
||
750 | |||
751 | if (reset($stk) == SERVICES_JSON_IN_ARR) { |
||
752 | return $arr; |
||
753 | |||
754 | } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
||
755 | return $obj; |
||
756 | |||
757 | } |
||
758 | |||
759 | } |
||
760 | } |
||
761 | } |
||
762 | |||
763 | /** |
||
764 | * @todo Ultimately, this should just call PEAR::isError() |
||
765 | */ |
||
766 | function isError($data, $code = null) |
||
776 | } |
||
777 | } |
||
778 | |||
779 | if (class_exists('PEAR_Error')) { |
||
780 | |||
806 | ?> |
||
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.