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 |
||
12 | final class JsonxTypeDecoder implements TypeDecoderInterface |
||
13 | { |
||
14 | const DATATYPE_OBJECT = 'object'; |
||
15 | const DATATYPE_ARRAY = 'array'; |
||
16 | const DATATYPE_BOOLEAN = 'boolean'; |
||
17 | const DATATYPE_STRING = 'string'; |
||
18 | const DATATYPE_NUMBER = 'number'; |
||
19 | const DATATYPE_NULL = 'null'; |
||
20 | |||
21 | /** |
||
22 | * @return string |
||
23 | */ |
||
24 | 4 | public function getContentType(): string |
|
28 | |||
29 | /** |
||
30 | * @param string $data |
||
31 | * |
||
32 | * @return array |
||
33 | * |
||
34 | * @throws DeserializerRuntimeException |
||
35 | */ |
||
36 | 5 | public function decode(string $data): array |
|
46 | |||
47 | /** |
||
48 | * @param \DOMNode $node |
||
49 | * |
||
50 | * @return array|bool|string|int|float|null |
||
51 | */ |
||
52 | 4 | private function decodeNode(\DOMNode $node) |
|
88 | |||
89 | /** |
||
90 | * @param \DOMNode $node |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | 2 | View Code Duplication | private function decodeObjectNode(\DOMNode $node): array |
107 | |||
108 | /** |
||
109 | * @param \DOMNode $node |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 1 | View Code Duplication | private function decodeArrayNode(\DOMNode $node): array |
126 | |||
127 | /** |
||
128 | * @param \DOMNode $node |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | 2 | private function decodeBooleanNode(\DOMNode $node): bool |
|
136 | |||
137 | /** |
||
138 | * @param \DOMNode $node |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 2 | private function decodeStringNode(\DOMNode $node): string |
|
146 | |||
147 | /** |
||
148 | * @param \DOMNoDOMNodedeList $node |
||
149 | * |
||
150 | * @return int|float |
||
151 | */ |
||
152 | 2 | private function decodeNumberNode(\DOMNode $node) |
|
162 | } |
||
163 |
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.