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:
1 | <?php |
||
7 | final class ObjectNode extends AbstractParentNode implements ObjectNodeSerializeInterface |
||
8 | { |
||
9 | /** |
||
10 | * @param bool $allowSerializeEmpty |
||
11 | * @return ObjectNode |
||
12 | */ |
||
13 | 17 | public static function create(bool $allowSerializeEmpty = false): ObjectNode |
|
20 | |||
21 | /** |
||
22 | * @param string $key |
||
23 | * @param AbstractNode $node |
||
24 | * |
||
25 | * @return $this |
||
26 | * |
||
27 | * @throws \InvalidArgumentException |
||
28 | */ |
||
29 | 17 | public function add($key, AbstractNode $node) |
|
41 | |||
42 | /** |
||
43 | * @return \stdClass |
||
44 | */ |
||
45 | 1 | public function serializeEmpty(): \stdClass |
|
49 | |||
50 | /** |
||
51 | * @return \stdClass|null |
||
52 | */ |
||
53 | 16 | View Code Duplication | public function serialize() |
66 | |||
67 | /** |
||
68 | * @param \stdClass $serialized |
||
69 | * @param string $key |
||
70 | * @param AbstractNode $child |
||
71 | */ |
||
72 | 16 | private function serializeChild(\stdClass $serialized, string $key, AbstractNode $child) |
|
80 | |||
81 | /** |
||
82 | * @param boolean $beautify |
||
83 | * @return string |
||
84 | */ |
||
85 | 16 | View Code Duplication | public function json(bool $beautify = false): string |
97 | } |
||
98 |
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.