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 | 396 | 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 | // We only attempt this on PHP 7+ because 5.x doesn't parse some edge cases correctly |
||
| 83 | 396 | if (PHP_VERSION_ID >= 700000) { |
|
| 84 | $result = json_decode($source, $associative, $depth, $options); |
||
| 85 | if (json_last_error() === JSON_ERROR_NONE) { |
||
| 86 | return $result; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | // Fall back to JSON5 if that fails |
||
| 91 | 396 | $associative = $associative === true || ($associative === null && $options & JSON_OBJECT_AS_ARRAY); |
|
| 92 | 396 | $castBigIntToString = $options & JSON_BIGINT_AS_STRING; |
|
| 93 | |||
| 94 | 396 | $decoder = new self((string)$source, $associative, $depth, $castBigIntToString); |
|
|
|
|||
| 95 | |||
| 96 | 396 | $result = $decoder->value(); |
|
| 97 | 303 | $decoder->white(); |
|
| 98 | 300 | if ($decoder->ch) { |
|
| 99 | 18 | $decoder->throwSyntaxError('Syntax error'); |
|
| 100 | } |
||
| 101 | |||
| 102 | 282 | return $result; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param int $at |
||
| 107 | * |
||
| 108 | * @return string|null |
||
| 109 | */ |
||
| 110 | 396 | private function charAt($at) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Parse the next character. |
||
| 121 | * |
||
| 122 | * @return null|string |
||
| 123 | */ |
||
| 124 | 366 | private function next() |
|
| 125 | { |
||
| 126 | // Get the next character. When there are no more characters, |
||
| 127 | // return the empty string. |
||
| 128 | 366 | if ($this->ch === "\n" || ($this->ch === "\r" && $this->peek() !== "\n")) { |
|
| 129 | 282 | $this->at++; |
|
| 130 | 282 | $this->lineNumber++; |
|
| 131 | 282 | $this->columnNumber = 1; |
|
| 132 | 188 | } else { |
|
| 133 | 327 | $this->at++; |
|
| 134 | 327 | $this->columnNumber++; |
|
| 135 | } |
||
| 136 | |||
| 137 | 366 | $this->ch = $this->charAt($this->at); |
|
| 138 | |||
| 139 | 366 | return $this->ch; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Parse the next character if it matches $c or fail. |
||
| 144 | * |
||
| 145 | * @param string $c |
||
| 146 | * |
||
| 147 | * @return string|null |
||
| 148 | */ |
||
| 149 | 168 | private function nextOrFail($c) |
|
| 150 | { |
||
| 151 | 168 | if ($c !== $this->ch) { |
|
| 152 | 18 | $this->throwSyntaxError(sprintf( |
|
| 153 | 18 | 'Expected %s instead of %s', |
|
| 154 | 18 | self::renderChar($c), |
|
| 155 | 18 | self::renderChar($this->ch) |
|
| 156 | 12 | )); |
|
| 157 | } |
||
| 158 | |||
| 159 | 168 | return $this->next(); |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the next character without consuming it or |
||
| 164 | * assigning it to the ch variable. |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | 36 | private function peek() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Attempt to match a regular expression at the current position on the current line. |
||
| 175 | * |
||
| 176 | * This function will not match across multiple lines. |
||
| 177 | * |
||
| 178 | * @param string $regex |
||
| 179 | * |
||
| 180 | * @return string|null |
||
| 181 | */ |
||
| 182 | 210 | private function match($regex) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Parse an identifier. |
||
| 207 | * |
||
| 208 | * Normally, reserved words are disallowed here, but we |
||
| 209 | * only use this for unquoted object keys, where reserved words are allowed, |
||
| 210 | * so we don't check for those here. References: |
||
| 211 | * - http://es5.github.com/#x7.6 |
||
| 212 | * - https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables |
||
| 213 | * - http://docstore.mik.ua/orelly/webprog/jscript/ch02_07.htm |
||
| 214 | */ |
||
| 215 | 42 | private function identifier() |
|
| 233 | |||
| 234 | 210 | private function number() |
|
| 313 | |||
| 314 | 93 | private function string() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Skip an inline comment, assuming this is one. |
||
| 360 | * |
||
| 361 | * The current character should be the second / character in the // pair that begins this inline comment. |
||
| 362 | * To finish the inline comment, we look for a newline or the end of the text. |
||
| 363 | */ |
||
| 364 | 36 | private function inlineComment() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Skip a block comment, assuming this is one. |
||
| 378 | * |
||
| 379 | * The current character should be the * character in the /* pair that begins this block comment. |
||
| 380 | * To finish the block comment, we look for an ending */ pair of characters, |
||
| 381 | * but we also watch for the end of text before the comment is terminated. |
||
| 382 | */ |
||
| 383 | 21 | private function blockComment() |
|
| 384 | { |
||
| 385 | do { |
||
| 386 | 21 | $this->next(); |
|
| 387 | 21 | while ($this->ch === '*') { |
|
| 388 | 18 | $this->nextOrFail('*'); |
|
| 389 | 18 | if ($this->ch === '/') { |
|
| 390 | 18 | $this->nextOrFail('/'); |
|
| 391 | |||
| 392 | 18 | return; |
|
| 393 | } |
||
| 394 | 2 | } |
|
| 395 | 21 | } while ($this->ch !== null); |
|
| 396 | |||
| 397 | 3 | $this->throwSyntaxError('Unterminated block comment'); |
|
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Skip a comment, whether inline or block-level, assuming this is one. |
||
| 402 | */ |
||
| 403 | 57 | private function comment() |
|
| 404 | { |
||
| 405 | // Comments always begin with a / character. |
||
| 406 | 57 | $this->nextOrFail('/'); |
|
| 407 | |||
| 408 | 57 | if ($this->ch === '/') { |
|
| 409 | 36 | $this->inlineComment(); |
|
| 410 | 46 | } elseif ($this->ch === '*') { |
|
| 411 | 21 | $this->blockComment(); |
|
| 412 | 12 | } else { |
|
| 413 | 3 | $this->throwSyntaxError('Unrecognized comment'); |
|
| 414 | } |
||
| 415 | 51 | } |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Skip whitespace and comments. |
||
| 419 | * |
||
| 420 | * Note that we're detecting comments by only a single / character. |
||
| 421 | * This works since regular expressions are not valid JSON(5), but this will |
||
| 422 | * break if there are other valid values that begin with a / character! |
||
| 423 | */ |
||
| 424 | 396 | private function white() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Matches true, false, null, etc |
||
| 439 | */ |
||
| 440 | 84 | private function word() |
|
| 441 | { |
||
| 442 | 84 | switch ($this->ch) { |
|
| 443 | 84 | case 't': |
|
| 444 | 36 | $this->nextOrFail('t'); |
|
| 445 | 36 | $this->nextOrFail('r'); |
|
| 446 | 36 | $this->nextOrFail('u'); |
|
| 447 | 36 | $this->nextOrFail('e'); |
|
| 448 | 36 | return true; |
|
| 449 | 63 | case 'f': |
|
| 450 | 18 | $this->nextOrFail('f'); |
|
| 451 | 18 | $this->nextOrFail('a'); |
|
| 452 | 18 | $this->nextOrFail('l'); |
|
| 453 | 18 | $this->nextOrFail('s'); |
|
| 454 | 18 | $this->nextOrFail('e'); |
|
| 455 | 18 | return false; |
|
| 456 | 48 | case 'n': |
|
| 457 | 18 | $this->nextOrFail('n'); |
|
| 458 | 18 | $this->nextOrFail('u'); |
|
| 459 | 18 | $this->nextOrFail('l'); |
|
| 460 | 18 | $this->nextOrFail('l'); |
|
| 461 | 18 | return null; |
|
| 462 | 30 | case 'I': |
|
| 463 | 15 | $this->nextOrFail('I'); |
|
| 464 | 15 | $this->nextOrFail('n'); |
|
| 465 | 12 | $this->nextOrFail('f'); |
|
| 466 | 12 | $this->nextOrFail('i'); |
|
| 467 | 12 | $this->nextOrFail('n'); |
|
| 468 | 12 | $this->nextOrFail('i'); |
|
| 469 | 12 | $this->nextOrFail('t'); |
|
| 470 | 12 | $this->nextOrFail('y'); |
|
| 471 | 12 | return INF; |
|
| 472 | 15 | case 'N': |
|
| 473 | 6 | $this->nextOrFail('N'); |
|
| 474 | 6 | $this->nextOrFail('a'); |
|
| 475 | 3 | $this->nextOrFail('N'); |
|
| 476 | 3 | return NAN; |
|
| 477 | 6 | } |
|
| 478 | |||
| 479 | 9 | $this->throwSyntaxError('Unexpected ' . self::renderChar($this->ch)); |
|
| 480 | } |
||
| 481 | |||
| 482 | 42 | private function arr() |
|
| 483 | { |
||
| 484 | 42 | $arr = []; |
|
| 485 | |||
| 486 | 42 | if ($this->ch === '[') { |
|
| 487 | 42 | if (++$this->depth > $this->maxDepth) { |
|
| 488 | 3 | $this->throwSyntaxError('Maximum stack depth exceeded'); |
|
| 489 | } |
||
| 490 | |||
| 491 | 42 | $this->nextOrFail('['); |
|
| 492 | 42 | $this->white(); |
|
| 493 | 42 | while ($this->ch !== null) { |
|
| 494 | 42 | if ($this->ch === ']') { |
|
| 495 | 12 | $this->nextOrFail(']'); |
|
| 496 | 12 | $this->depth--; |
|
| 497 | 12 | return $arr; // Potentially empty array |
|
| 498 | } |
||
| 499 | // ES5 allows omitting elements in arrays, e.g. [,] and |
||
| 500 | // [,null]. We don't allow this in JSON5. |
||
| 501 | 39 | if ($this->ch === ',') { |
|
| 502 | 6 | $this->throwSyntaxError('Missing array element'); |
|
| 503 | } |
||
| 504 | |||
| 505 | 33 | $arr[] = $this->value(); |
|
| 506 | |||
| 507 | 30 | $this->white(); |
|
| 508 | // If there's no comma after this value, this needs to |
||
| 509 | // be the end of the array. |
||
| 510 | 30 | if ($this->ch !== ',') { |
|
| 511 | 21 | $this->nextOrFail(']'); |
|
| 512 | 18 | $this->depth--; |
|
| 513 | 18 | return $arr; |
|
| 514 | } |
||
| 515 | 15 | $this->nextOrFail(','); |
|
| 516 | 15 | $this->white(); |
|
| 517 | 10 | } |
|
| 518 | } |
||
| 519 | |||
| 520 | $this->throwSyntaxError('Bad array'); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Parse an object value |
||
| 525 | */ |
||
| 526 | 90 | private function obj() |
|
| 527 | { |
||
| 528 | 90 | $object = $this->associative ? [] : new \stdClass; |
|
| 529 | |||
| 530 | 90 | if ($this->ch === '{') { |
|
| 531 | 90 | if (++$this->depth > $this->maxDepth) { |
|
| 532 | 3 | $this->throwSyntaxError('Maximum stack depth exceeded'); |
|
| 533 | } |
||
| 534 | |||
| 535 | 90 | $this->nextOrFail('{'); |
|
| 536 | 90 | $this->white(); |
|
| 537 | 90 | while ($this->ch) { |
|
| 538 | 90 | if ($this->ch === '}') { |
|
| 539 | 21 | $this->nextOrFail('}'); |
|
| 540 | 21 | $this->depth--; |
|
| 541 | 21 | return $object; // Potentially empty object |
|
| 542 | } |
||
| 543 | |||
| 544 | // Keys can be unquoted. If they are, they need to be |
||
| 545 | // valid JS identifiers. |
||
| 546 | 78 | if ($this->ch === '"' || $this->ch === "'") { |
|
| 547 | 39 | $key = $this->string(); |
|
| 548 | 26 | } else { |
|
| 549 | 42 | $key = $this->identifier(); |
|
| 550 | } |
||
| 551 | |||
| 552 | 69 | $this->white(); |
|
| 553 | 69 | $this->nextOrFail(':'); |
|
| 554 | 66 | if ($this->associative) { |
|
| 555 | 45 | $object[$key] = $this->value(); |
|
| 556 | 30 | } else { |
|
| 557 | 66 | $object->{$key} = $this->value(); |
|
| 558 | } |
||
| 559 | 63 | $this->white(); |
|
| 560 | // If there's no comma after this pair, this needs to be |
||
| 561 | // the end of the object. |
||
| 562 | 63 | if ($this->ch !== ',') { |
|
| 563 | 54 | $this->nextOrFail('}'); |
|
| 564 | 48 | $this->depth--; |
|
| 565 | 48 | return $object; |
|
| 566 | } |
||
| 567 | 21 | $this->nextOrFail(','); |
|
| 568 | 21 | $this->white(); |
|
| 569 | 14 | } |
|
| 570 | } |
||
| 571 | |||
| 572 | $this->throwSyntaxError('Bad object'); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Parse a JSON value. |
||
| 577 | * |
||
| 578 | * It could be an object, an array, a string, a number, |
||
| 579 | * or a word. |
||
| 580 | */ |
||
| 581 | 396 | private function value() |
|
| 600 | |||
| 601 | 114 | private function throwSyntaxError($message) |
|
| 605 | |||
| 606 | 27 | private static function renderChar($chr) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $ch |
||
| 613 | * |
||
| 614 | * @return string|null |
||
| 615 | */ |
||
| 616 | 18 | private static function getEscapee($ch) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Returns everything from $this->at onwards. |
||
| 637 | * |
||
| 638 | * Utilizes a cache so we don't have to continuously parse through UTF-8 |
||
| 639 | * data that was earlier in the string which we don't even care about. |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | 210 | private function getRemainder() |
|
| 655 | } |
||
| 656 |
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: