Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | private $at = 0; |
||
| 20 | |||
| 21 | private $lineNumber = 1; |
||
| 22 | |||
| 23 | private $columnNumber = 1; |
||
| 24 | |||
| 25 | private $ch; |
||
| 26 | |||
| 27 | private $chArr; |
||
| 28 | |||
| 29 | private $associative = false; |
||
| 30 | |||
| 31 | private $maxDepth = 512; |
||
| 32 | |||
| 33 | private $castBigIntToString = false; |
||
| 34 | |||
| 35 | private $depth = 1; |
||
| 36 | |||
| 37 | private $length; |
||
| 38 | |||
| 39 | private $remainderCache; |
||
| 40 | |||
| 41 | private $remainderCacheAt; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Private constructor. |
||
| 45 | * |
||
| 46 | * @param string $json |
||
| 47 | * @param bool $associative |
||
| 48 | * @param int $depth |
||
| 49 | * @param bool $castBigIntToString |
||
| 50 | */ |
||
| 51 | 223 | private function __construct($json, $associative = false, $depth = 512, $castBigIntToString = false) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Takes a JSON encoded string and converts it into a PHP variable. |
||
| 68 | * |
||
| 69 | * The parameters exactly match PHP's json_decode() function - see |
||
| 70 | * http://php.net/manual/en/function.json-decode.php for more information. |
||
| 71 | * |
||
| 72 | * @param string $source The JSON string being decoded. |
||
| 73 | * @param bool $associative When TRUE, returned objects will be converted into associative arrays. |
||
| 74 | * @param int $depth User specified recursion depth. |
||
| 75 | * @param int $options Bitmask of JSON decode options. |
||
| 76 | * |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | 396 | public static function decode($source, $associative = false, $depth = 512, $options = 0) |
|
| 80 | { |
||
| 81 | // Try parsing with json_decode first, since that's much faster |
||
| 82 | 396 | $result = json_decode($source, $associative, $depth, $options); |
|
| 83 | 396 | if (json_last_error() === JSON_ERROR_NONE) { |
|
| 84 | 173 | return $result; |
|
| 85 | } |
||
| 86 | |||
| 87 | // Fall back to JSON5 if that fails |
||
| 88 | 223 | $associative = $associative === true || ($associative === null && $options & JSON_OBJECT_AS_ARRAY); |
|
| 89 | 223 | $castBigIntToString = $options & JSON_BIGINT_AS_STRING; |
|
| 90 | |||
| 91 | 223 | $decoder = new self((string)$source, $associative, $depth, $castBigIntToString); |
|
|
|
|||
| 92 | |||
| 93 | 223 | $result = $decoder->value(); |
|
| 94 | 152 | $decoder->white(); |
|
| 95 | 149 | if ($decoder->ch) { |
|
| 96 | 18 | $decoder->throwSyntaxError('Syntax error'); |
|
| 97 | } |
||
| 98 | |||
| 99 | 131 | return $result; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $at |
||
| 104 | * |
||
| 105 | * @return string|null |
||
| 106 | */ |
||
| 107 | 223 | private function charAt($at) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Parse the next character. |
||
| 118 | * |
||
| 119 | * @return null|string |
||
| 120 | */ |
||
| 121 | 201 | private function next() |
|
| 122 | { |
||
| 123 | // Get the next character. When there are no more characters, |
||
| 124 | // return the empty string. |
||
| 125 | 201 | if ($this->ch === "\n" || ($this->ch === "\r" && $this->peek() !== "\n")) { |
|
| 126 | 164 | $this->at++; |
|
| 127 | 164 | $this->lineNumber++; |
|
| 128 | 164 | $this->columnNumber = 1; |
|
| 129 | 94 | } else { |
|
| 130 | 197 | $this->at++; |
|
| 131 | 197 | $this->columnNumber++; |
|
| 132 | } |
||
| 133 | |||
| 134 | 201 | $this->ch = $this->charAt($this->at); |
|
| 135 | |||
| 136 | 201 | return $this->ch; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Parse the next character if it matches $c or fail. |
||
| 141 | * |
||
| 142 | * @param string $c |
||
| 143 | * |
||
| 144 | * @return string|null |
||
| 145 | */ |
||
| 146 | 138 | private function nextOrFail($c) |
|
| 147 | { |
||
| 148 | 138 | if ($c !== $this->ch) { |
|
| 149 | 18 | $this->throwSyntaxError(sprintf( |
|
| 150 | 18 | 'Expected %s instead of %s', |
|
| 151 | 18 | self::renderChar($c), |
|
| 152 | 18 | self::renderChar($this->ch) |
|
| 153 | 12 | )); |
|
| 154 | } |
||
| 155 | |||
| 156 | 138 | return $this->next(); |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the next character without consuming it or |
||
| 161 | * assigning it to the ch variable. |
||
| 162 | * |
||
| 163 | * @return mixed |
||
| 164 | */ |
||
| 165 | 30 | private function peek() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Attempt to match a regular expression at the current position on the current line. |
||
| 172 | * |
||
| 173 | * This function will not match across multiple lines. |
||
| 174 | * |
||
| 175 | * @param string $regex |
||
| 176 | * |
||
| 177 | * @return string|null |
||
| 178 | */ |
||
| 179 | 103 | private function match($regex) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Parse an identifier. |
||
| 204 | * |
||
| 205 | * Normally, reserved words are disallowed here, but we |
||
| 206 | * only use this for unquoted object keys, where reserved words are allowed, |
||
| 207 | * so we don't check for those here. References: |
||
| 208 | * - http://es5.github.com/#x7.6 |
||
| 209 | * - https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables |
||
| 210 | * - http://docstore.mik.ua/orelly/webprog/jscript/ch02_07.htm |
||
| 211 | */ |
||
| 212 | 42 | private function identifier() |
|
| 230 | |||
| 231 | 78 | private function number() |
|
| 310 | |||
| 311 | 63 | 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() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Skip a block comment, assuming this is one. |
||
| 375 | * |
||
| 376 | * The current character should be the * character in the /* pair that begins this block comment. |
||
| 377 | * To finish the block comment, we look for an ending */ pair of characters, |
||
| 378 | * but we also watch for the end of text before the comment is terminated. |
||
| 379 | */ |
||
| 380 | 21 | private function blockComment() |
|
| 381 | { |
||
| 382 | do { |
||
| 383 | 21 | $this->next(); |
|
| 384 | 21 | while ($this->ch === '*') { |
|
| 385 | 18 | $this->nextOrFail('*'); |
|
| 386 | 18 | if ($this->ch === '/') { |
|
| 387 | 18 | $this->nextOrFail('/'); |
|
| 388 | |||
| 389 | 18 | return; |
|
| 390 | } |
||
| 391 | 2 | } |
|
| 392 | 21 | } while ($this->ch !== null); |
|
| 393 | |||
| 394 | 3 | $this->throwSyntaxError('Unterminated block comment'); |
|
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Skip a comment, whether inline or block-level, assuming this is one. |
||
| 399 | */ |
||
| 400 | 57 | private function comment() |
|
| 401 | { |
||
| 402 | // Comments always begin with a / character. |
||
| 403 | 57 | $this->nextOrFail('/'); |
|
| 404 | |||
| 405 | 57 | if ($this->ch === '/') { |
|
| 406 | 36 | $this->inlineComment(); |
|
| 407 | 46 | } elseif ($this->ch === '*') { |
|
| 408 | 21 | $this->blockComment(); |
|
| 409 | 12 | } else { |
|
| 410 | 3 | $this->throwSyntaxError('Unrecognized comment'); |
|
| 411 | } |
||
| 412 | 51 | } |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Skip whitespace and comments. |
||
| 416 | * |
||
| 417 | * Note that we're detecting comments by only a single / character. |
||
| 418 | * This works since regular expressions are not valid JSON(5), but this will |
||
| 419 | * break if there are other valid values that begin with a / character! |
||
| 420 | */ |
||
| 421 | 223 | private function white() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Matches true, false, null, etc |
||
| 436 | */ |
||
| 437 | 67 | private function word() |
|
| 438 | { |
||
| 439 | 67 | switch ($this->ch) { |
|
| 440 | 67 | case 't': |
|
| 441 | 21 | $this->nextOrFail('t'); |
|
| 442 | 21 | $this->nextOrFail('r'); |
|
| 443 | 21 | $this->nextOrFail('u'); |
|
| 444 | 21 | $this->nextOrFail('e'); |
|
| 445 | 21 | return true; |
|
| 446 | 52 | case 'f': |
|
| 447 | 9 | $this->nextOrFail('f'); |
|
| 448 | 9 | $this->nextOrFail('a'); |
|
| 449 | 9 | $this->nextOrFail('l'); |
|
| 450 | 9 | $this->nextOrFail('s'); |
|
| 451 | 9 | $this->nextOrFail('e'); |
|
| 452 | 9 | return false; |
|
| 453 | 43 | case 'n': |
|
| 454 | 15 | $this->nextOrFail('n'); |
|
| 455 | 15 | $this->nextOrFail('u'); |
|
| 456 | 15 | $this->nextOrFail('l'); |
|
| 457 | 15 | $this->nextOrFail('l'); |
|
| 458 | 15 | return null; |
|
| 459 | 28 | case 'I': |
|
| 460 | 15 | $this->nextOrFail('I'); |
|
| 461 | 15 | $this->nextOrFail('n'); |
|
| 462 | 12 | $this->nextOrFail('f'); |
|
| 463 | 12 | $this->nextOrFail('i'); |
|
| 464 | 12 | $this->nextOrFail('n'); |
|
| 465 | 12 | $this->nextOrFail('i'); |
|
| 466 | 12 | $this->nextOrFail('t'); |
|
| 467 | 12 | $this->nextOrFail('y'); |
|
| 468 | 12 | return INF; |
|
| 469 | 13 | case 'N': |
|
| 470 | 6 | $this->nextOrFail('N'); |
|
| 471 | 6 | $this->nextOrFail('a'); |
|
| 472 | 3 | $this->nextOrFail('N'); |
|
| 473 | 3 | return NAN; |
|
| 474 | 4 | } |
|
| 475 | |||
| 476 | 7 | $this->throwSyntaxError('Unexpected ' . self::renderChar($this->ch)); |
|
| 477 | } |
||
| 478 | |||
| 479 | 27 | private function arr() |
|
| 480 | { |
||
| 481 | 27 | $arr = []; |
|
| 482 | |||
| 483 | 27 | if ($this->ch === '[') { |
|
| 484 | 27 | if (++$this->depth > $this->maxDepth) { |
|
| 485 | 3 | $this->throwSyntaxError('Maximum stack depth exceeded'); |
|
| 486 | } |
||
| 487 | |||
| 488 | 27 | $this->nextOrFail('['); |
|
| 489 | 27 | $this->white(); |
|
| 490 | 27 | while ($this->ch !== null) { |
|
| 491 | 27 | if ($this->ch === ']') { |
|
| 492 | 9 | $this->nextOrFail(']'); |
|
| 493 | 9 | $this->depth--; |
|
| 494 | 9 | return $arr; // Potentially empty array |
|
| 495 | } |
||
| 496 | // ES5 allows omitting elements in arrays, e.g. [,] and |
||
| 497 | // [,null]. We don't allow this in JSON5. |
||
| 498 | 27 | if ($this->ch === ',') { |
|
| 499 | 6 | $this->throwSyntaxError('Missing array element'); |
|
| 500 | } |
||
| 501 | |||
| 502 | 21 | $arr[] = $this->value(); |
|
| 503 | |||
| 504 | 18 | $this->white(); |
|
| 505 | // If there's no comma after this value, this needs to |
||
| 506 | // be the end of the array. |
||
| 507 | 18 | if ($this->ch !== ',') { |
|
| 508 | 9 | $this->nextOrFail(']'); |
|
| 509 | 6 | $this->depth--; |
|
| 510 | 6 | return $arr; |
|
| 511 | } |
||
| 512 | 9 | $this->nextOrFail(','); |
|
| 513 | 9 | $this->white(); |
|
| 514 | 6 | } |
|
| 515 | } |
||
| 516 | |||
| 517 | $this->throwSyntaxError('Bad array'); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Parse an object value |
||
| 522 | */ |
||
| 523 | 72 | private function obj() |
|
| 524 | { |
||
| 525 | 72 | $object = $this->associative ? [] : new \stdClass; |
|
| 526 | |||
| 527 | 72 | if ($this->ch === '{') { |
|
| 528 | 72 | if (++$this->depth > $this->maxDepth) { |
|
| 529 | 3 | $this->throwSyntaxError('Maximum stack depth exceeded'); |
|
| 530 | } |
||
| 531 | |||
| 532 | 72 | $this->nextOrFail('{'); |
|
| 533 | 72 | $this->white(); |
|
| 534 | 72 | while ($this->ch) { |
|
| 535 | 72 | if ($this->ch === '}') { |
|
| 536 | 18 | $this->nextOrFail('}'); |
|
| 537 | 18 | $this->depth--; |
|
| 538 | 18 | return $object; // Potentially empty object |
|
| 539 | } |
||
| 540 | |||
| 541 | // Keys can be unquoted. If they are, they need to be |
||
| 542 | // valid JS identifiers. |
||
| 543 | 63 | if ($this->ch === '"' || $this->ch === "'") { |
|
| 544 | 24 | $key = $this->string(); |
|
| 545 | 16 | } else { |
|
| 546 | 42 | $key = $this->identifier(); |
|
| 547 | } |
||
| 548 | |||
| 549 | 54 | $this->white(); |
|
| 550 | 54 | $this->nextOrFail(':'); |
|
| 551 | 51 | if ($this->associative) { |
|
| 552 | 39 | $object[$key] = $this->value(); |
|
| 553 | 26 | } else { |
|
| 554 | 51 | $object->{$key} = $this->value(); |
|
| 555 | } |
||
| 556 | 48 | $this->white(); |
|
| 557 | // If there's no comma after this pair, this needs to be |
||
| 558 | // the end of the object. |
||
| 559 | 48 | if ($this->ch !== ',') { |
|
| 560 | 39 | $this->nextOrFail('}'); |
|
| 561 | 33 | $this->depth--; |
|
| 562 | 33 | return $object; |
|
| 563 | } |
||
| 564 | 15 | $this->nextOrFail(','); |
|
| 565 | 15 | $this->white(); |
|
| 566 | 10 | } |
|
| 567 | } |
||
| 568 | |||
| 569 | $this->throwSyntaxError('Bad object'); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Parse a JSON value. |
||
| 574 | * |
||
| 575 | * It could be an object, an array, a string, a number, |
||
| 576 | * or a word. |
||
| 577 | */ |
||
| 578 | 223 | private function value() |
|
| 597 | |||
| 598 | 92 | private function throwSyntaxError($message) |
|
| 602 | |||
| 603 | 25 | private static function renderChar($chr) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * @param string $ch |
||
| 610 | * |
||
| 611 | * @return string|null |
||
| 612 | */ |
||
| 613 | 18 | private static function getEscapee($ch) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Returns everything from $this->at onwards. |
||
| 634 | * |
||
| 635 | * Utilizes a cache so we don't have to continuously parse through UTF-8 |
||
| 636 | * data that was earlier in the string which we don't even care about. |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | 103 | private function getRemainder() |
|
| 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: