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); |
||
15 | class Hydrator |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $options; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $hydrators = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $annotations = []; |
||
31 | |||
32 | /** |
||
33 | * @var HandlerInterface[] |
||
34 | */ |
||
35 | protected $annotationHandlers = []; |
||
36 | |||
37 | /** |
||
38 | * @var Reader |
||
39 | */ |
||
40 | protected $annotationReader; |
||
41 | |||
42 | /** |
||
43 | * @param array $options |
||
44 | */ |
||
45 | 4 | public function __construct(array $options) |
|
46 | { |
||
47 | 4 | $this->options = $options; |
|
48 | |||
49 | 4 | $reader = new AnnotationReader(); |
|
50 | 4 | if (isset($this->options[Options::ANNOTATION_CACHE]) && |
|
51 | 4 | $this->options[Options::ANNOTATION_CACHE] instanceof Cache |
|
52 | ) { |
||
53 | 1 | $reader = new CachedReader( |
|
54 | $reader, |
||
55 | 1 | $this->options[Options::ANNOTATION_CACHE] |
|
56 | ); |
||
57 | } |
||
58 | 4 | $this->annotationReader = $reader; |
|
59 | |||
60 | 4 | $this->setUpAnnotations(); |
|
61 | 4 | $this->addSelfToExtraProperties(); |
|
62 | 4 | } |
|
63 | |||
64 | 4 | protected function setUpAnnotations() |
|
65 | { |
||
66 | 4 | if (!isset($this->options[Options::ANNOTATIONS])) { |
|
67 | return; |
||
68 | } |
||
69 | |||
70 | 4 | foreach ($this->options[Options::ANNOTATIONS] as $annotationClass => $handler) { |
|
71 | 4 | $this->annotationHandlers[$annotationClass] = new $handler($this); |
|
72 | } |
||
73 | 4 | } |
|
74 | |||
75 | 4 | protected function addSelfToExtraProperties() |
|
76 | { |
||
77 | 4 | $this->options[Options::EXTRA_PROPERTIES]['hydrator'] = $this; |
|
78 | 4 | } |
|
79 | |||
80 | /** |
||
81 | * @param string $class |
||
82 | * @param array $json |
||
83 | * @return ResourceInterface |
||
84 | */ |
||
85 | 4 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
97 | |||
98 | /** |
||
99 | * @param string $class |
||
100 | * @param array $json |
||
101 | * @return ResourceInterface |
||
102 | */ |
||
103 | 4 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
104 | { |
||
105 | 4 | $hydrator = $this->getHydrator($class); |
|
106 | 4 | $object = new $class(); |
|
107 | 4 | $json = $this->hydrateApplyAnnotations($json, $object); |
|
108 | 4 | $resource = $hydrator->hydrate($json, $object); |
|
109 | 4 | if ($resource instanceof AbstractResource) { |
|
110 | $resource->setExtraProperties($this->options[Options::EXTRA_PROPERTIES]); |
||
111 | } |
||
112 | 4 | return $resource; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param array $json |
||
117 | * @param ResourceInterface $object |
||
118 | * @return array |
||
119 | */ |
||
120 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
121 | { |
||
122 | 4 | foreach ($this->annotationHandlers as $annotationClass => $handler) { |
|
123 | 4 | $annotation = $this->getAnnotation($object, $annotationClass); |
|
124 | 4 | if ($annotation === null) { |
|
125 | 4 | continue; |
|
126 | } |
||
127 | |||
128 | 4 | $json = $handler->hydrate($annotation, $json, $object); |
|
129 | } |
||
130 | |||
131 | 4 | return $json; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $class |
||
136 | * @param ResourceInterface $object |
||
137 | * @return array |
||
138 | */ |
||
139 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
140 | { |
||
141 | 2 | $fullClassName = implode( |
|
142 | 2 | '\\', |
|
143 | [ |
||
144 | $this->options[Options::NAMESPACE], |
||
145 | 2 | $this->options[Options::NAMESPACE_SUFFIX], |
|
146 | 2 | $class, |
|
147 | ] |
||
148 | ); |
||
149 | 2 | return $this->extractFQCN($fullClassName, $object); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
154 | * @param string $class |
||
155 | * @param ResourceInterface $object |
||
156 | * @return array |
||
157 | */ |
||
158 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
164 | |||
165 | /** |
||
166 | * @param array $json |
||
167 | * @param ResourceInterface $object |
||
168 | * @return array |
||
169 | */ |
||
170 | 2 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
183 | |||
184 | /** |
||
185 | * @param ResourceInterface $object |
||
186 | * @param string $annotationClass |
||
187 | * @return null|AnnotationInterface |
||
188 | */ |
||
189 | 4 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
220 | |||
221 | /** |
||
222 | * @param string $resource |
||
223 | * @param ResourceInterface $object |
||
224 | * @return ResourceInterface |
||
225 | */ |
||
226 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
236 | |||
237 | /** |
||
238 | * @param string $class |
||
239 | * @return HydratorInterface |
||
240 | */ |
||
241 | 4 | protected function getHydrator(string $class): HydratorInterface |
|
259 | } |
||
260 |
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.