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); |
||
17 | class Hydrator |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $options; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $hydrators = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $annotations = []; |
||
33 | |||
34 | /** |
||
35 | * @var HandlerInterface[] |
||
36 | */ |
||
37 | protected $annotationHandlers = []; |
||
38 | |||
39 | /** |
||
40 | * @var Reader |
||
41 | */ |
||
42 | protected $annotationReader; |
||
43 | |||
44 | /** |
||
45 | * @param array $options |
||
46 | */ |
||
47 | 6 | public function __construct(array $options) |
|
65 | |||
66 | 6 | protected function setUpAnnotations() |
|
76 | |||
77 | 6 | protected function addSelfToExtraProperties() |
|
81 | |||
82 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
110 | |||
111 | /** |
||
112 | * @param string $class |
||
113 | * @param array $json |
||
114 | * @return ResourceInterface |
||
115 | */ |
||
116 | 4 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
128 | |||
129 | /** |
||
130 | * @param string $class |
||
131 | * @param array $json |
||
132 | * @return ResourceInterface |
||
133 | */ |
||
134 | 4 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
145 | |||
146 | /** |
||
147 | * @param array $json |
||
148 | * @param ResourceInterface $object |
||
149 | * @return array |
||
150 | */ |
||
151 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
164 | |||
165 | /** |
||
166 | * @param string $class |
||
167 | * @param ResourceInterface $object |
||
168 | * @return array |
||
169 | */ |
||
170 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
182 | |||
183 | /** |
||
184 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
185 | * @param string $class |
||
186 | * @param ResourceInterface $object |
||
187 | * @return array |
||
188 | */ |
||
189 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
195 | |||
196 | /** |
||
197 | * @param array $json |
||
198 | * @param ResourceInterface $object |
||
199 | * @return array |
||
200 | */ |
||
201 | 2 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
214 | |||
215 | /** |
||
216 | * @param ResourceInterface $object |
||
217 | * @param string $annotationClass |
||
218 | * @return null|AnnotationInterface |
||
219 | */ |
||
220 | 4 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
251 | |||
252 | /** |
||
253 | * @param string $resource |
||
254 | * @param ResourceInterface $object |
||
255 | * @return ResourceInterface |
||
256 | */ |
||
257 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
267 | |||
268 | /** |
||
269 | * @param string $class |
||
270 | * @return HydratorInterface |
||
271 | */ |
||
272 | 6 | protected function getHydrator(string $class): HydratorInterface |
|
290 | } |
||
291 |