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 declare(strict_types = 1); |
||
21 | class ObjectHydrator implements Hydrator |
||
22 | { |
||
23 | /** |
||
24 | * @var AnySchema |
||
25 | */ |
||
26 | private $anySchema; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $is32Bit; |
||
32 | |||
33 | /** |
||
34 | * @var DateTimeSerializer |
||
35 | */ |
||
36 | private $dateTimeSerializer; |
||
37 | |||
38 | /** |
||
39 | * @var ClassNameResolver |
||
40 | */ |
||
41 | private $classNameResolver; |
||
42 | |||
43 | /** |
||
44 | * ObjectHydrator constructor. |
||
45 | * |
||
46 | * @param ClassNameResolver $classNameResolver |
||
47 | * @param DateTimeSerializer $dateTimeSerializer |
||
48 | * @param bool $is32Bit |
||
49 | */ |
||
50 | public function __construct( |
||
61 | |||
62 | /** |
||
63 | * @param mixed $data |
||
64 | * @param Schema $schema |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function hydrate($data, Schema $schema) |
||
72 | |||
73 | /** |
||
74 | * @param mixed $data |
||
75 | * @param Schema $schema |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function dehydrate($data, Schema $schema) |
||
83 | |||
84 | /** |
||
85 | * @param mixed $node |
||
86 | * @param Schema $schema |
||
87 | * |
||
88 | * @return mixed |
||
89 | */ |
||
90 | private function hydrateNode($node, Schema $schema) |
||
132 | |||
133 | /** |
||
134 | * @param mixed $node |
||
135 | * @param Schema $schema |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | private function dehydrateNode($node, Schema $schema) |
||
181 | |||
182 | /** |
||
183 | * Cast a scalar value using the schema. |
||
184 | * |
||
185 | * @param mixed $value |
||
186 | * @param ScalarSchema $schema |
||
187 | * |
||
188 | * @return float|int|string|\DateTimeInterface |
||
189 | * @throws UnsupportedException |
||
190 | */ |
||
191 | private function castScalarValue($value, ScalarSchema $schema) |
||
211 | |||
212 | /** |
||
213 | * @param mixed $node |
||
214 | * @param Schema $schema |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | private function shouldTreatAsObject($node, Schema $schema): bool |
||
222 | |||
223 | /** |
||
224 | * @param mixed $node |
||
225 | * @param Schema $schema |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | private function shouldTreatAsArray($node, Schema $schema): bool |
||
233 | } |
||
234 |
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.