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 JsonEncoder 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 JsonEncoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class JsonEncoder |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Encode a value as JSON array. |
||
| 25 | */ |
||
| 26 | const JSON_ARRAY = 1; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Encode a value as JSON object. |
||
| 30 | */ |
||
| 31 | const JSON_OBJECT = 2; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Encode a value as JSON string. |
||
| 35 | */ |
||
| 36 | const JSON_STRING = 3; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Encode a value as JSON integer or float. |
||
| 40 | */ |
||
| 41 | const JSON_NUMBER = 4; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var JsonValidator |
||
| 45 | */ |
||
| 46 | private $validator; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $arrayEncoding = self::JSON_ARRAY; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | private $numericEncoding = self::JSON_STRING; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $gtLtEscaped = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | private $ampersandEscaped = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | private $singleQuoteEscaped = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | private $doubleQuoteEscaped = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | private $slashEscaped = true; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | private $unicodeEscaped = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | private $prettyPrinting = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $terminatedWithLineFeed = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | private $maxDepth = 512; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates a new encoder. |
||
| 105 | * |
||
| 106 | * @param null|JsonValidator $validator |
||
| 107 | */ |
||
| 108 | 61 | public function __construct(JsonValidator $validator = null) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Encodes data as JSON. |
||
| 115 | * |
||
| 116 | * If a schema is passed, the value is validated against that schema before |
||
| 117 | * encoding. The schema may be passed as file path or as object returned |
||
| 118 | * from `JsonDecoder::decodeFile($schemaFile)`. |
||
| 119 | * |
||
| 120 | * You can adjust the decoding with the various setters in this class. |
||
| 121 | * |
||
| 122 | * @param mixed $data The data to encode |
||
| 123 | * @param string|object $schema The schema file or object |
||
|
|
|||
| 124 | * |
||
| 125 | * @return string The JSON string |
||
| 126 | * |
||
| 127 | * @throws EncodingFailedException If the data could not be encoded |
||
| 128 | * @throws ValidationFailedException If the data fails schema validation |
||
| 129 | * @throws InvalidSchemaException If the schema is invalid |
||
| 130 | */ |
||
| 131 | 53 | public function encode($data, $schema = null) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Encodes data into a JSON file. |
||
| 239 | * |
||
| 240 | * @param mixed $data The data to encode |
||
| 241 | * @param string $path The path where the JSON file will be stored |
||
| 242 | * @param string|object $schema The schema file or object |
||
| 243 | * |
||
| 244 | * @throws EncodingFailedException If the data could not be encoded |
||
| 245 | * @throws ValidationFailedException If the data fails schema validation |
||
| 246 | * @throws InvalidSchemaException If the schema is invalid |
||
| 247 | * |
||
| 248 | * @see encode |
||
| 249 | */ |
||
| 250 | 6 | public function encodeFile($data, $path, $schema = null) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Returns the encoding of non-associative arrays. |
||
| 313 | * |
||
| 314 | * @return int One of the constants {@link JSON_OBJECT} and {@link JSON_ARRAY} |
||
| 315 | */ |
||
| 316 | public function getArrayEncoding() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Sets the encoding of non-associative arrays. |
||
| 323 | * |
||
| 324 | * By default, non-associative arrays are decoded as JSON arrays. |
||
| 325 | * |
||
| 326 | * @param int $encoding One of the constants {@link JSON_OBJECT} and {@link JSON_ARRAY} |
||
| 327 | * |
||
| 328 | * @throws \InvalidArgumentException If the passed encoding is invalid |
||
| 329 | */ |
||
| 330 | 5 | View Code Duplication | public function setArrayEncoding($encoding) |
| 342 | |||
| 343 | /** |
||
| 344 | * Returns the encoding of numeric strings. |
||
| 345 | * |
||
| 346 | * @return int One of the constants {@link JSON_STRING} and {@link JSON_NUMBER} |
||
| 347 | */ |
||
| 348 | public function getNumericEncoding() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Sets the encoding of numeric strings. |
||
| 355 | * |
||
| 356 | * By default, non-associative arrays are decoded as JSON strings. |
||
| 357 | * |
||
| 358 | * @param int $encoding One of the constants {@link JSON_STRING} and {@link JSON_NUMBER} |
||
| 359 | * |
||
| 360 | * @throws \InvalidArgumentException If the passed encoding is invalid |
||
| 361 | */ |
||
| 362 | 6 | View Code Duplication | public function setNumericEncoding($encoding) |
| 374 | |||
| 375 | /** |
||
| 376 | * Returns whether ampersands (&) are escaped. |
||
| 377 | * |
||
| 378 | * If `true`, ampersands will be escaped as "\u0026". |
||
| 379 | * |
||
| 380 | * By default, ampersands are not escaped. |
||
| 381 | * |
||
| 382 | * @return bool Whether ampersands are escaped |
||
| 383 | */ |
||
| 384 | public function isAmpersandEscaped() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Sets whether ampersands (&) should be escaped. |
||
| 391 | * |
||
| 392 | * If `true`, ampersands will be escaped as "\u0026". |
||
| 393 | * |
||
| 394 | * By default, ampersands are not escaped. |
||
| 395 | * |
||
| 396 | * @param bool $enabled Whether ampersands should be escaped |
||
| 397 | */ |
||
| 398 | 2 | public function setEscapeAmpersand($enabled) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Returns whether double quotes (") are escaped. |
||
| 405 | * |
||
| 406 | * If `true`, double quotes will be escaped as "\u0022". |
||
| 407 | * |
||
| 408 | * By default, double quotes are not escaped. |
||
| 409 | * |
||
| 410 | * @return bool Whether double quotes are escaped |
||
| 411 | */ |
||
| 412 | public function isDoubleQuoteEscaped() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Sets whether double quotes (") should be escaped. |
||
| 419 | * |
||
| 420 | * If `true`, double quotes will be escaped as "\u0022". |
||
| 421 | * |
||
| 422 | * By default, double quotes are not escaped. |
||
| 423 | * |
||
| 424 | * @param bool $enabled Whether double quotes should be escaped |
||
| 425 | */ |
||
| 426 | 2 | public function setEscapeDoubleQuote($enabled) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Returns whether single quotes (') are escaped. |
||
| 433 | * |
||
| 434 | * If `true`, single quotes will be escaped as "\u0027". |
||
| 435 | * |
||
| 436 | * By default, single quotes are not escaped. |
||
| 437 | * |
||
| 438 | * @return bool Whether single quotes are escaped |
||
| 439 | */ |
||
| 440 | public function isSingleQuoteEscaped() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Sets whether single quotes (") should be escaped. |
||
| 447 | * |
||
| 448 | * If `true`, single quotes will be escaped as "\u0027". |
||
| 449 | * |
||
| 450 | * By default, single quotes are not escaped. |
||
| 451 | * |
||
| 452 | * @param bool $enabled Whether single quotes should be escaped |
||
| 453 | */ |
||
| 454 | 2 | public function setEscapeSingleQuote($enabled) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Returns whether forward slashes (/) are escaped. |
||
| 461 | * |
||
| 462 | * If `true`, forward slashes will be escaped as "\/". |
||
| 463 | * |
||
| 464 | * By default, forward slashes are not escaped. |
||
| 465 | * |
||
| 466 | * @return bool Whether forward slashes are escaped |
||
| 467 | */ |
||
| 468 | public function isSlashEscaped() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets whether forward slashes (") should be escaped. |
||
| 475 | * |
||
| 476 | * If `true`, forward slashes will be escaped as "\/". |
||
| 477 | * |
||
| 478 | * By default, forward slashes are not escaped. |
||
| 479 | * |
||
| 480 | * @param bool $enabled Whether forward slashes should be escaped |
||
| 481 | */ |
||
| 482 | 2 | public function setEscapeSlash($enabled) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Returns whether greater than/less than symbols (>, <) are escaped. |
||
| 489 | * |
||
| 490 | * If `true`, greater than will be escaped as "\u003E" and less than as |
||
| 491 | * "\u003C". |
||
| 492 | * |
||
| 493 | * By default, greater than/less than symbols are not escaped. |
||
| 494 | * |
||
| 495 | * @return bool Whether greater than/less than symbols are escaped |
||
| 496 | */ |
||
| 497 | public function isGtLtEscaped() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Sets whether greater than/less than symbols (>, <) should be escaped. |
||
| 504 | * |
||
| 505 | * If `true`, greater than will be escaped as "\u003E" and less than as |
||
| 506 | * "\u003C". |
||
| 507 | * |
||
| 508 | * By default, greater than/less than symbols are not escaped. |
||
| 509 | * |
||
| 510 | * @param bool $enabled Whether greater than/less than should be escaped |
||
| 511 | */ |
||
| 512 | 2 | public function setEscapeGtLt($enabled) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Returns whether unicode characters are escaped. |
||
| 519 | * |
||
| 520 | * If `true`, unicode characters will be escaped as hexadecimals strings. |
||
| 521 | * For example, "ü" will be escaped as "\u00fc". |
||
| 522 | * |
||
| 523 | * By default, unicode characters are escaped. |
||
| 524 | * |
||
| 525 | * @return bool Whether unicode characters are escaped |
||
| 526 | */ |
||
| 527 | public function isUnicodeEscaped() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Sets whether unicode characters should be escaped. |
||
| 534 | * |
||
| 535 | * If `true`, unicode characters will be escaped as hexadecimals strings. |
||
| 536 | * For example, "ü" will be escaped as "\u00fc". |
||
| 537 | * |
||
| 538 | * By default, unicode characters are escaped. |
||
| 539 | * |
||
| 540 | * @param bool $enabled Whether unicode characters should be escaped |
||
| 541 | */ |
||
| 542 | 2 | public function setEscapeUnicode($enabled) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Returns whether JSON strings are formatted for better readability. |
||
| 549 | * |
||
| 550 | * If `true`, line breaks will be added after object properties and array |
||
| 551 | * entries. Each new nesting level will be indented by four spaces. |
||
| 552 | * |
||
| 553 | * By default, pretty printing is not enabled. |
||
| 554 | * |
||
| 555 | * @return bool Whether JSON strings are formatted |
||
| 556 | */ |
||
| 557 | public function isPrettyPrinting() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Sets whether JSON strings should be formatted for better readability. |
||
| 564 | * |
||
| 565 | * If `true`, line breaks will be added after object properties and array |
||
| 566 | * entries. Each new nesting level will be indented by four spaces. |
||
| 567 | * |
||
| 568 | * By default, pretty printing is not enabled. |
||
| 569 | * |
||
| 570 | * @param bool $prettyPrinting Whether JSON strings should be formatted |
||
| 571 | */ |
||
| 572 | 2 | public function setPrettyPrinting($prettyPrinting) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Returns whether JSON strings are terminated with a line feed. |
||
| 579 | * |
||
| 580 | * By default, JSON strings are not terminated with a line feed. |
||
| 581 | * |
||
| 582 | * @return bool Whether JSON strings are terminated with a line feed |
||
| 583 | */ |
||
| 584 | public function isTerminatedWithLineFeed() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Sets whether JSON strings should be terminated with a line feed. |
||
| 591 | * |
||
| 592 | * By default, JSON strings are not terminated with a line feed. |
||
| 593 | * |
||
| 594 | * @param bool $enabled Whether JSON strings should be terminated with a |
||
| 595 | * line feed |
||
| 596 | */ |
||
| 597 | 2 | public function setTerminateWithLineFeed($enabled) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Returns the maximum recursion depth. |
||
| 604 | * |
||
| 605 | * A depth of zero means that objects are not allowed. A depth of one means |
||
| 606 | * only one level of objects or arrays is allowed. |
||
| 607 | * |
||
| 608 | * @return int The maximum recursion depth |
||
| 609 | */ |
||
| 610 | public function getMaxDepth() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Sets the maximum recursion depth. |
||
| 617 | * |
||
| 618 | * If the depth is exceeded during encoding, an {@link EncodingFailedException} |
||
| 619 | * will be thrown. |
||
| 620 | * |
||
| 621 | * A depth of zero means that objects are not allowed. A depth of one means |
||
| 622 | * only one level of objects or arrays is allowed. |
||
| 623 | * |
||
| 624 | * @param int $maxDepth The maximum recursion depth |
||
| 625 | * |
||
| 626 | * @throws \InvalidArgumentException If the depth is not an integer greater |
||
| 627 | * than or equal to zero |
||
| 628 | */ |
||
| 629 | 6 | View Code Duplication | public function setMaxDepth($maxDepth) |
| 647 | } |
||
| 648 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.