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 Json 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 Json, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | final class Json |
||
17 | { |
||
18 | /** |
||
19 | * Override class names that JsonMapper uses to create objects. |
||
20 | * Useful when your setter methods accept abstract classes or interfaces. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | public $classMap = []; |
||
25 | |||
26 | /** |
||
27 | * Callback used when an undefined property is found. |
||
28 | * |
||
29 | * Works only when $bExceptionOnUndefinedProperty is disabled. |
||
30 | * |
||
31 | * Parameters to this function are: |
||
32 | * 1. Object that is being filled |
||
33 | * 2. Name of the unknown JSON property |
||
34 | * 3. JSON value of the property |
||
35 | * |
||
36 | * @var callable |
||
37 | */ |
||
38 | public $undefinedPropertyHandler; |
||
39 | |||
40 | /** |
||
41 | * Runtime cache for inspected classes. This is particularly effective if |
||
42 | * mapArray() is called with a large number of objects |
||
43 | * |
||
44 | * @var array property inspection result cache |
||
45 | */ |
||
46 | private $arInspectedClasses = []; |
||
47 | |||
48 | /** |
||
49 | * Map data all data in $json into the given $object instance. |
||
50 | * |
||
51 | * @param iterable $json JSON object structure from json_decode() |
||
52 | * @param object|string $object Object to map $json data into |
||
53 | * |
||
54 | * @phpstan-param object|class-string $object Object to map $json data into |
||
55 | * |
||
56 | * @return mixed mapped object is returned |
||
57 | * |
||
58 | * @see mapArray() |
||
59 | */ |
||
60 | 6 | public function map($json, $object) |
|
225 | |||
226 | /** |
||
227 | * Map an array |
||
228 | * |
||
229 | * @param array $json JSON array structure from json_decode() |
||
230 | * @param mixed $array Array or ArrayObject that gets filled with |
||
231 | * data from $json |
||
232 | * @param string|null $class Class name for children objects. |
||
233 | * All children will get mapped onto this type. |
||
234 | * Supports class names and simple types |
||
235 | * like "string" and nullability "string|null". |
||
236 | * Pass "null" to not convert any values |
||
237 | * @param string $parent_key defines the key this array belongs to |
||
238 | * in order to aid debugging |
||
239 | * |
||
240 | * @pslam-param null|class-string $class |
||
241 | * |
||
242 | * @return mixed Mapped $array is returned |
||
243 | */ |
||
244 | 4 | public function mapArray($json, $array, $class = null, $parent_key = '') |
|
318 | |||
319 | 3 | /** |
|
320 | * Convert a type name to a fully namespaced type name. |
||
321 | * |
||
322 | 6 | * @param string|null $type Type name (simple type or class name) |
|
323 | * @param string $strNs Base namespace that gets prepended to the type name |
||
324 | 6 | * |
|
325 | * @return string|null Fully-qualified type name with namespace |
||
326 | 6 | */ |
|
327 | private function getFullNamespace($type, $strNs) |
||
353 | |||
354 | /** |
||
355 | 6 | * Try to find out if a property exists in a given class. |
|
356 | * Checks property first, falls back to setter method. |
||
357 | 6 | * |
|
358 | 6 | * @param \ReflectionClass<object> $rc Reflection class to check |
|
359 | 3 | * @param string $name Property name |
|
360 | * |
||
361 | * @return array First value: if the property exists |
||
362 | 6 | * Second value: the accessor to use ( |
|
363 | 6 | * Array-Key-String or ReflectionMethod or ReflectionProperty, or null) |
|
364 | 1 | * Third value: type of the property |
|
365 | */ |
||
366 | private function inspectProperty(\ReflectionClass $rc, $name): array |
||
436 | |||
437 | /** |
||
438 | * Copied from PHPUnit 3.7.29, Util/Test.php |
||
439 | * |
||
440 | * @param string $docblock Full method docblock |
||
441 | * |
||
442 | * @return array |
||
443 | */ |
||
444 | private static function parseAnnotations($docblock): array |
||
464 | |||
465 | /** |
||
466 | * Removes - and _ and makes the next letter uppercase |
||
467 | * |
||
468 | * @param string $name Property name |
||
469 | * |
||
470 | * @return string CamelCasedVariableName |
||
471 | 6 | */ |
|
472 | private function getCamelCaseName($name): string |
||
480 | 6 | ||
481 | /** |
||
482 | * Since hyphens cannot be used in variables we have to uppercase them. |
||
483 | * |
||
484 | * Technically you may use them, but they are awkward to access. |
||
485 | * |
||
486 | * @param string $name Property name |
||
487 | * |
||
488 | * @return string Name without hyphen |
||
489 | */ |
||
490 | private function getSafeName($name): string |
||
501 | 6 | ||
502 | /** |
||
503 | * Set a property on a given object to a given value. |
||
504 | * |
||
505 | * Checks if the setter or the property are public are made before |
||
506 | * calling this method. |
||
507 | * |
||
508 | 6 | * @param \Arrayy\Arrayy|object $object Object to set property on |
|
509 | * @param \ReflectionMethod|\ReflectionProperty|string $accessor Array-Key-String or ReflectionMethod or ReflectionProperty |
||
510 | * @param mixed $value Value of property |
||
511 | * |
||
512 | * @return void |
||
513 | */ |
||
514 | private function setProperty( |
||
528 | 6 | ||
529 | /** |
||
530 | 6 | * Get the mapped class/type name for this class. |
|
531 | * Returns the incoming classname if not mapped. |
||
532 | 6 | * |
|
533 | * @param string|null $type Type name to map |
||
534 | * @param mixed $jsonValue Constructor parameter (the json value) |
||
535 | * |
||
536 | 6 | * @return string|null The mapped type/class name |
|
537 | * |
||
538 | * @phpstan-return class-string|string|null |
||
539 | 6 | */ |
|
540 | private function getMappedType($type, $jsonValue = null) |
||
568 | |||
569 | /** |
||
570 | 5 | * Checks if the given type is a "simple type" |
|
571 | 5 | * |
|
572 | 5 | * @param string $type type name from gettype() |
|
573 | 4 | * |
|
574 | 5 | * @return bool True if it is a simple PHP type |
|
575 | * |
||
576 | * @see isScalarType() |
||
577 | */ |
||
578 | private function isSimpleType($type): bool |
||
595 | |||
596 | /** |
||
597 | * Checks if the object is of this type or has this type as one of its parents |
||
598 | * |
||
599 | * @param string $type class name of type being required |
||
600 | * @param mixed $value Some PHP value to be tested |
||
601 | * |
||
602 | * @return bool True if $object has type of $type |
||
603 | */ |
||
604 | 4 | private function isObjectOfSameType($type, $value): bool |
|
612 | |||
613 | /** |
||
614 | * Checks if the given type is a type that is not nested |
||
615 | * (simple type except array and object) |
||
616 | * |
||
617 | * @param string $type type name from gettype() |
||
618 | * |
||
619 | * @return bool True if it is a non-nested PHP type |
||
620 | * |
||
621 | * @see isSimpleType() |
||
622 | 4 | */ |
|
623 | private function isScalarType($type): bool |
||
632 | |||
633 | /** |
||
634 | 6 | * Returns true if type is an array of elements |
|
635 | * (bracket notation) |
||
636 | 6 | * |
|
637 | * @param string $strType type to be matched |
||
638 | * |
||
639 | * @return bool |
||
640 | */ |
||
641 | private function isArrayOfType($strType): bool |
||
645 | |||
646 | 4 | /** |
|
647 | * Checks if the given type is nullable |
||
648 | 4 | * |
|
649 | 3 | * @param string $type type name from the phpdoc param |
|
650 | * |
||
651 | * @return bool True if it is nullable |
||
652 | 4 | */ |
|
653 | 4 | private function isNullable($type): bool |
|
657 | |||
658 | /** |
||
659 | * Remove the 'null' section of a type |
||
660 | * |
||
661 | * @param string|null $type type name from the phpdoc param |
||
662 | * |
||
663 | * @return string|null The new type value |
||
664 | */ |
||
665 | private function removeNullable($type) |
||
677 | |||
678 | /** |
||
679 | * Create a new object of the given type. |
||
680 | 4 | * |
|
681 | * This method exists to be overwritten in child classes, |
||
682 | * so you can do dependency injection or so. |
||
683 | * |
||
684 | 4 | * @param object|string $class Class name to instantiate |
|
685 | 4 | * @param bool $useParameter Pass $parameter to the constructor or not |
|
686 | * @param mixed $jsonValue Constructor parameter (the json value) |
||
687 | 4 | * |
|
688 | * @phpstan-param object|class-string $class |
||
689 | 4 | * |
|
690 | * @return object Freshly created object |
||
691 | * |
||
692 | * @internal |
||
693 | */ |
||
694 | 4 | private static function createInstance( |
|
715 | } |
||
716 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.