Complex classes like Json5Decoder 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Json5Decoder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class Json5Decoder |
||
18 | { |
||
19 | const REGEX_WHITESPACE = '/[ \t\r\n\v\f\xA0\x{FEFF}]/u'; |
||
20 | |||
21 | private $json; |
||
22 | |||
23 | private $at = 0; |
||
24 | |||
25 | private $lineNumber = 1; |
||
26 | |||
27 | private $columnNumber = 1; |
||
28 | |||
29 | private $ch; |
||
30 | |||
31 | private $associative = false; |
||
32 | |||
33 | private $maxDepth = 512; |
||
34 | |||
35 | private $castBigIntToString = false; |
||
36 | |||
37 | private $depth = 1; |
||
38 | |||
39 | private $length; |
||
40 | |||
41 | private $lineCache; |
||
42 | |||
43 | /** |
||
44 | * Private constructor. |
||
45 | * |
||
46 | * @param string $json |
||
47 | * @param bool $associative |
||
48 | * @param int $depth |
||
49 | * @param bool $castBigIntToString |
||
50 | */ |
||
51 | 387 | private function __construct($json, $associative = false, $depth = 512, $castBigIntToString = false) |
|
62 | |||
63 | /** |
||
64 | * Takes a JSON encoded string and converts it into a PHP variable. |
||
65 | * |
||
66 | * The parameters exactly match PHP's json_decode() function - see |
||
67 | * http://php.net/manual/en/function.json-decode.php for more information. |
||
68 | * |
||
69 | * @param string $source The JSON string being decoded. |
||
70 | * @param bool $associative When TRUE, returned objects will be converted into associative arrays. |
||
71 | * @param int $depth User specified recursion depth. |
||
72 | * @param int $options Bitmask of JSON decode options. |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | 387 | public static function decode($source, $associative = false, $depth = 512, $options = 0) |
|
91 | |||
92 | /** |
||
93 | * @param int $at |
||
94 | * |
||
95 | * @return string|null |
||
96 | */ |
||
97 | 387 | private function charAt($at) |
|
105 | |||
106 | /** |
||
107 | * Parse the next character. |
||
108 | * |
||
109 | * If $c is given, the next char will only be parsed if the current |
||
110 | * one matches $c. |
||
111 | * |
||
112 | * @param string|null $c |
||
113 | * |
||
114 | * @return null|string |
||
115 | */ |
||
116 | 357 | private function next($c = null) |
|
142 | |||
143 | /** |
||
144 | * Get the next character without consuming it or |
||
145 | * assigning it to the ch variable. |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 12 | private function peek() |
|
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | 231 | private function getLineRemainder() |
|
168 | |||
169 | /** |
||
170 | * Attempt to match a regular expression at the current position on the current line. |
||
171 | * |
||
172 | * This function will not match across multiple lines. |
||
173 | * |
||
174 | * @param string $regex |
||
175 | * |
||
176 | * @return string|null |
||
177 | */ |
||
178 | 231 | private function match($regex) |
|
200 | |||
201 | /** |
||
202 | * Parse an identifier. |
||
203 | * |
||
204 | * Normally, reserved words are disallowed here, but we |
||
205 | * only use this for unquoted object keys, where reserved words are allowed, |
||
206 | * so we don't check for those here. References: |
||
207 | * - http://es5.github.com/#x7.6 |
||
208 | * - https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables |
||
209 | * - http://docstore.mik.ua/orelly/webprog/jscript/ch02_07.htm |
||
210 | */ |
||
211 | 42 | private function identifier() |
|
212 | { |
||
213 | // @codingStandardsIgnoreStart |
||
214 | // Be careful when editing this regex, there are a couple Unicode characters in between here -------------vv |
||
215 | 42 | $match = $this->match('/^(?:[\$_\p{L}\p{Nl}]|\\\\u[0-9A-Fa-f]{4})(?:[\$_\p{L}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]|\\\\u[0-9A-Fa-f]{4})*/u'); |
|
216 | // @codingStandardsIgnoreEnd |
||
217 | |||
218 | 42 | if ($match === null) { |
|
219 | 9 | $this->throwSyntaxError('Bad identifier as unquoted key'); |
|
220 | } |
||
221 | |||
222 | // Un-escape escaped Unicode chars |
||
223 | 33 | $unescaped = preg_replace_callback('/(?:\\\\u[0-9A-Fa-f]{4})+/', function ($m) { |
|
224 | 6 | return json_decode('"'.$m[0].'"'); |
|
225 | 33 | }, $match); |
|
226 | |||
227 | 33 | return $unescaped; |
|
228 | } |
||
229 | |||
230 | 210 | private function number() |
|
309 | |||
310 | 84 | private function string() |
|
355 | |||
356 | /** |
||
357 | * Skip an inline comment, assuming this is one. |
||
358 | * |
||
359 | * The current character should be the second / character in the // pair that begins this inline comment. |
||
360 | * To finish the inline comment, we look for a newline or the end of the text. |
||
361 | */ |
||
362 | 36 | private function inlineComment() |
|
373 | |||
374 | /** |
||
375 | * Skip a block comment, assuming this is one. |
||
376 | * |
||
377 | * The current character should be the * character in the /* pair that begins this block comment. |
||
378 | * To finish the block comment, we look for an ending */ pair of characters, |
||
379 | * but we also watch for the end of text before the comment is terminated. |
||
380 | */ |
||
381 | 21 | private function blockComment() |
|
397 | |||
398 | /** |
||
399 | * Skip a comment, whether inline or block-level, assuming this is one. |
||
400 | */ |
||
401 | 57 | private function comment() |
|
418 | |||
419 | /** |
||
420 | * Skip whitespace and comments. |
||
421 | * |
||
422 | * Note that we're detecting comments by only a single / character. |
||
423 | * This works since regular expressions are not valid JSON(5), but this will |
||
424 | * break if there are other valid values that begin with a / character! |
||
425 | */ |
||
426 | 387 | private function white() |
|
438 | |||
439 | /** |
||
440 | * Matches true, false, null, etc |
||
441 | */ |
||
442 | 84 | private function word() |
|
483 | |||
484 | 42 | private function arr() |
|
523 | |||
524 | /** |
||
525 | * Parse an object value |
||
526 | */ |
||
527 | 81 | private function obj() |
|
575 | |||
576 | /** |
||
577 | * Parse a JSON value. |
||
578 | * |
||
579 | * It could be an object, an array, a string, a number, |
||
580 | * or a word. |
||
581 | */ |
||
582 | 387 | private function value() |
|
601 | |||
602 | 108 | private function throwSyntaxError($message) |
|
606 | |||
607 | 24 | private static function renderChar($chr) |
|
611 | |||
612 | /** |
||
613 | * @param string $ch |
||
614 | * |
||
615 | * @return string|null |
||
616 | */ |
||
617 | 18 | private static function getEscapee($ch) |
|
635 | } |
||
636 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: