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 | 360 | 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 | 360 | public static function decode($source, $associative = false, $depth = 512, $options = 0) |
|
91 | |||
92 | /** |
||
93 | * @param int $at |
||
94 | * |
||
95 | * @return string|null |
||
96 | */ |
||
97 | 360 | 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 | 330 | 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 | 210 | 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 | 210 | 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 | 39 | private function identifier() |
|
229 | |||
230 | 210 | private function number() |
|
309 | |||
310 | 66 | private function string() |
|
354 | |||
355 | /** |
||
356 | * Skip an inline comment, assuming this is one. |
||
357 | * |
||
358 | * The current character should be the second / character in the // pair that begins this inline comment. |
||
359 | * To finish the inline comment, we look for a newline or the end of the text. |
||
360 | */ |
||
361 | 36 | private function inlineComment() |
|
376 | |||
377 | /** |
||
378 | * Skip a block comment, assuming this is one. |
||
379 | * |
||
380 | * The current character should be the * character in the /* pair that begins this block comment. |
||
381 | * To finish the block comment, we look for an ending */ pair of characters, |
||
382 | * but we also watch for the end of text before the comment is terminated. |
||
383 | */ |
||
384 | 21 | private function blockComment() |
|
385 | { |
||
386 | 21 | if ($this->ch !== '*') { |
|
387 | $this->throwSyntaxError('Not a block comment'); |
||
388 | } |
||
389 | |||
390 | do { |
||
391 | 21 | $this->next(); |
|
392 | 21 | while ($this->ch === '*') { |
|
393 | 18 | $this->next('*'); |
|
394 | 18 | if ($this->ch === '/') { |
|
395 | 18 | $this->next('/'); |
|
396 | |||
397 | 18 | return; |
|
398 | } |
||
399 | 2 | } |
|
400 | 21 | } while ($this->ch !== null); |
|
401 | |||
402 | 3 | $this->throwSyntaxError('Unterminated block comment'); |
|
403 | } |
||
404 | |||
405 | /** |
||
406 | * Skip a comment, whether inline or block-level, assuming this is one. |
||
407 | */ |
||
408 | 54 | private function comment() |
|
409 | { |
||
410 | // Comments always begin with a / character. |
||
411 | 54 | if ($this->ch !== '/') { |
|
412 | $this->throwSyntaxError('Not a comment'); |
||
413 | } |
||
414 | |||
415 | 54 | $this->next('/'); |
|
416 | |||
417 | 54 | if ($this->ch === '/') { |
|
418 | 36 | $this->inlineComment(); |
|
419 | 43 | } elseif ($this->ch === '*') { |
|
420 | 21 | $this->blockComment(); |
|
421 | 12 | } else { |
|
422 | $this->throwSyntaxError('Unrecognized comment'); |
||
423 | } |
||
424 | 51 | } |
|
425 | |||
426 | /** |
||
427 | * Skip whitespace and comments. |
||
428 | * |
||
429 | * Note that we're detecting comments by only a single / character. |
||
430 | * This works since regular expressions are not valid JSON(5), but this will |
||
431 | * break if there are other valid values that begin with a / character! |
||
432 | */ |
||
433 | 360 | private function white() |
|
445 | |||
446 | /** |
||
447 | * Matches true, false, null, etc |
||
448 | */ |
||
449 | 78 | private function word() |
|
490 | |||
491 | 42 | private function arr() |
|
530 | |||
531 | /** |
||
532 | * Parse an object value |
||
533 | */ |
||
534 | 75 | private function obj() |
|
582 | |||
583 | /** |
||
584 | * Parse a JSON value. |
||
585 | * |
||
586 | * It could be an object, an array, a string, a number, |
||
587 | * or a word. |
||
588 | */ |
||
589 | 360 | private function value() |
|
608 | |||
609 | 96 | private function throwSyntaxError($message) |
|
613 | |||
614 | 18 | private static function renderChar($chr) |
|
618 | |||
619 | /** |
||
620 | * @param string $hex Hex code |
||
621 | * |
||
622 | * @return string Unicode character |
||
623 | */ |
||
624 | 3 | private static function fromCharCode($hex) |
|
628 | |||
629 | /** |
||
630 | * @param string $ch |
||
631 | * |
||
632 | * @return string|null |
||
633 | */ |
||
634 | 12 | private static function getEscapee($ch) |
|
652 | } |
||
653 |
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: