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); |
||
20 | class Hydrator |
||
21 | { |
||
22 | /** |
||
23 | * @var ContainerInterface |
||
24 | */ |
||
25 | protected $container; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $options; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $hydrators = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $annotations = []; |
||
41 | |||
42 | /** |
||
43 | * @var HandlerInterface[] |
||
44 | */ |
||
45 | protected $annotationHandlers = []; |
||
46 | |||
47 | /** |
||
48 | * @var Reader |
||
49 | */ |
||
50 | protected $annotationReader; |
||
51 | |||
52 | /** |
||
53 | * @param ContainerInterface $container |
||
54 | * @param array $options |
||
55 | */ |
||
56 | public function __construct(ContainerInterface $container, array $options) |
||
74 | 8 | ||
75 | 8 | protected function setUpAnnotations() |
|
85 | 8 | ||
86 | public function preheat(string $scanTarget, string $namespace) |
||
114 | 2 | ||
115 | /** |
||
116 | * @param string $class |
||
117 | * @param array $json |
||
118 | 2 | * @return ResourceInterface |
|
119 | */ |
||
120 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
|
132 | 4 | ||
133 | /** |
||
134 | 4 | * @param string $class |
|
135 | 4 | * @param array $json |
|
136 | * @return ResourceInterface |
||
137 | */ |
||
138 | 4 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
147 | |||
148 | /** |
||
149 | * @param array $json |
||
150 | 4 | * @param ResourceInterface $object |
|
151 | * @return array |
||
152 | 4 | */ |
|
153 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
166 | |||
167 | 4 | protected function getEmptyOrResource(string $class, array $json): string |
|
191 | |||
192 | 4 | /** |
|
193 | 4 | * @param string $class |
|
194 | 4 | * @param ResourceInterface $object |
|
195 | * @return array |
||
196 | */ |
||
197 | 4 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
209 | 2 | ||
210 | /** |
||
211 | 2 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
|
212 | 2 | * @param string $class |
|
213 | * @param ResourceInterface $object |
||
214 | * @return array |
||
215 | 2 | */ |
|
216 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
226 | |||
227 | /** |
||
228 | 2 | * @param array $json |
|
229 | * @param ResourceInterface $object |
||
230 | 2 | * @return array |
|
231 | 2 | */ |
|
232 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
|
245 | |||
246 | 2 | /** |
|
247 | 2 | * @param ResourceInterface $object |
|
248 | 2 | * @param string $annotationClass |
|
249 | 2 | * @return null|AnnotationInterface |
|
250 | */ |
||
251 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
||
282 | |||
283 | /** |
||
284 | * @param string $resource |
||
285 | 4 | * @param ResourceInterface $object |
|
286 | 4 | * @return ResourceInterface |
|
287 | 4 | */ |
|
288 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
||
298 | |||
299 | /** |
||
300 | 1 | * @param string $class |
|
301 | * @return HydratorInterface |
||
302 | 1 | */ |
|
303 | protected function getHydrator(string $class): HydratorInterface |
||
321 | } |
||
322 |