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