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:
Complex classes like Encoder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Encoder, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Neomerx\JsonApi\Encoder; |
||
35 | class Encoder implements EncoderInterface, LoggerAwareInterface |
||
36 | { |
||
37 | use LoggerAwareTrait; |
||
38 | |||
39 | /** |
||
40 | * @var ContainerInterface |
||
41 | */ |
||
42 | private $container; |
||
43 | |||
44 | /** |
||
45 | * @var FactoryInterface |
||
46 | */ |
||
47 | private $factory; |
||
48 | |||
49 | /** |
||
50 | * @var EncoderOptions|null |
||
51 | */ |
||
52 | private $encoderOptions; |
||
53 | |||
54 | /** |
||
55 | * Links in array<string,LinkInterface> format. |
||
56 | * |
||
57 | * @var array|null |
||
58 | */ |
||
59 | private $links; |
||
60 | |||
61 | /** |
||
62 | * @var array|object|null |
||
63 | */ |
||
64 | private $meta; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $isAddJsonApiVersion; |
||
70 | |||
71 | /** |
||
72 | * @var mixed|null |
||
73 | */ |
||
74 | protected $jsonApiVersionMeta; |
||
75 | |||
76 | /** |
||
77 | * @param FactoryInterface $factory |
||
78 | * @param ContainerInterface $container |
||
79 | * @param EncoderOptions|null $encoderOptions |
||
80 | */ |
||
81 | 75 | public function __construct( |
|
92 | |||
93 | /** |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | 5 | public function withLinks(array $links) |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 3 | public function withMeta($meta) |
|
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | 1 | public function withJsonApiVersion($meta = null) |
|
123 | |||
124 | |||
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | 1 | View Code Duplication | public function withRelationshipSelfLink($resource, $relationshipName, $meta = null, $treatAsHref = false) |
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 1 | View Code Duplication | public function withRelationshipRelatedLink($resource, $relationshipName, $meta = null, $treatAsHref = false) |
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 62 | View Code Duplication | public function encodeData($data, EncodingParametersInterface $parameters = null) |
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | 3 | View Code Duplication | public function encodeIdentifiers($data, EncodingParametersInterface $parameters = null) |
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | */ |
||
176 | 2 | public function encodeError(ErrorInterface $error) |
|
180 | |||
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | 3 | public function encodeErrors($errors) |
|
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 1 | public function encodeMeta($meta) |
|
196 | |||
197 | /** |
||
198 | * @param ContainerInterface $container |
||
199 | * @param object|array|Iterator|null $data |
||
200 | * @param EncodingParametersInterface|null $parameters |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | 66 | protected function encodeDataToArray( |
|
240 | |||
241 | /** |
||
242 | * Encode array to JSON. |
||
243 | * |
||
244 | * @param array $document |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 68 | protected function encodeToJson(array $document) |
|
254 | |||
255 | /** |
||
256 | * Create encoder instance. |
||
257 | * |
||
258 | * @param array $schemas Schema providers. |
||
259 | * @param EncoderOptions|null $encodeOptions |
||
260 | * |
||
261 | * @return EncoderInterface |
||
262 | */ |
||
263 | 73 | public static function instance(array $schemas = [], EncoderOptions $encodeOptions = null) |
|
271 | |||
272 | /** |
||
273 | * @return FactoryInterface |
||
274 | */ |
||
275 | 65 | protected static function createFactory() |
|
279 | |||
280 | /** |
||
281 | * @param mixed $data |
||
282 | */ |
||
283 | 66 | protected function checkInputData($data) |
|
289 | |||
290 | /** |
||
291 | * @param object|array|Iterator|null $data |
||
292 | * @param EncodingParametersInterface|null $parameters |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 3 | protected function encodeIdentifiersToArray($data, EncodingParametersInterface $parameters = null) |
|
303 | |||
304 | /** |
||
305 | * @param ErrorInterface $error |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | 3 | protected function encodeErrorToArray(ErrorInterface $error) |
|
317 | |||
318 | /** |
||
319 | * @param $errors |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | 4 | protected function encodeErrorsToArray($errors) |
|
331 | |||
332 | /** |
||
333 | * @param $meta |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | 2 | protected function encodeMetaToArray($meta) |
|
347 | |||
348 | /** |
||
349 | * @return ContainerInterface |
||
350 | */ |
||
351 | 66 | protected function getContainer() |
|
355 | |||
356 | /** |
||
357 | * @return FactoryInterface |
||
358 | */ |
||
359 | 73 | protected function getFactory() |
|
363 | |||
364 | /** |
||
365 | * @return EncoderOptions|null |
||
366 | */ |
||
367 | 71 | protected function getEncoderOptions() |
|
371 | |||
372 | /** |
||
373 | * @return array|null |
||
374 | */ |
||
375 | 64 | protected function getLinks() |
|
379 | |||
380 | /** |
||
381 | * @return array|null|object |
||
382 | */ |
||
383 | 64 | public function getMeta() |
|
387 | |||
388 | /** |
||
389 | * @return boolean |
||
390 | */ |
||
391 | 64 | protected function isWithJsonApiVersion() |
|
395 | |||
396 | /** |
||
397 | * @return mixed|null |
||
398 | */ |
||
399 | 1 | protected function getJsonApiVersionMeta() |
|
403 | |||
404 | /** |
||
405 | * @param ContainerInterface $container |
||
406 | * @param EncodingParametersInterface|null $parameters |
||
407 | * |
||
408 | * @return ParametersAnalyzerInterface |
||
409 | */ |
||
410 | 65 | private function createParametersAnalyzer( |
|
419 | |||
420 | /** |
||
421 | * Reset encode parameters. |
||
422 | */ |
||
423 | 75 | private function resetEncodeParameters() |
|
430 | |||
431 | /** |
||
432 | * @param DocumentInterface $docWriter |
||
433 | */ |
||
434 | 65 | private function configureUrlPrefix(DocumentInterface $docWriter) |
|
439 | } |
||
440 |
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.