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 JsonApiSerializer 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 JsonApiSerializer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class JsonApiSerializer extends ArraySerializer |
||
19 | { |
||
20 | protected $baseUrl; |
||
21 | protected $rootObjects; |
||
22 | |||
23 | /** |
||
24 | * JsonApiSerializer constructor. |
||
25 | * |
||
26 | * @param string $baseUrl |
||
27 | */ |
||
28 | 25 | public function __construct($baseUrl = null) |
|
33 | |||
34 | /** |
||
35 | * Serialize a collection. |
||
36 | * |
||
37 | * @param string $resourceKey |
||
38 | * @param array $data |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | 17 | public function collection($resourceKey, array $data) |
|
52 | |||
53 | /** |
||
54 | * Serialize an item. |
||
55 | * |
||
56 | * @param string $resourceKey |
||
57 | * @param array $data |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | 25 | public function item($resourceKey, array $data) |
|
83 | |||
84 | /** |
||
85 | * Serialize the paginator. |
||
86 | * |
||
87 | * @param PaginatorInterface $paginator |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | 3 | public function paginator(PaginatorInterface $paginator) |
|
121 | |||
122 | /** |
||
123 | * Serialize the meta. |
||
124 | * |
||
125 | * @param array $meta |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 24 | public function meta(array $meta) |
|
144 | |||
145 | /** |
||
146 | * @return array |
||
147 | */ |
||
148 | 2 | public function null() |
|
154 | |||
155 | /** |
||
156 | * Serialize the included data. |
||
157 | * |
||
158 | * @param ResourceInterface $resource |
||
159 | * @param array $data |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 24 | public function includedData(ResourceInterface $resource, array $data) |
|
189 | |||
190 | /** |
||
191 | * Indicates if includes should be side-loaded. |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 25 | public function sideloadIncludes() |
|
199 | |||
200 | /** |
||
201 | * @param array $data |
||
202 | * @param array $includedData |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | 24 | public function injectData($data, $includedData) |
|
216 | |||
217 | /** |
||
218 | * Hook to manipulate the final sideloaded includes. |
||
219 | * The JSON API specification does not allow the root object to be included |
||
220 | * into the sideloaded `included`-array. We have to make sure it is |
||
221 | * filtered out, in case some object links to the root object in a |
||
222 | * relationship. |
||
223 | * |
||
224 | * @param array $includedData |
||
225 | * @param array $data |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | 24 | public function filterIncludes($includedData, $data) |
|
246 | |||
247 | /** |
||
248 | * Filter function to delete root objects from array. |
||
249 | * |
||
250 | * @param array $object |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | 13 | protected function filterRootObject($object) |
|
258 | |||
259 | /** |
||
260 | * Set the root objects of the JSON API tree. |
||
261 | * |
||
262 | * @param array $objects |
||
263 | */ |
||
264 | protected function setRootObjects(array $objects = []) |
||
270 | |||
271 | /** |
||
272 | * Determines whether an object is a root object of the JSON API tree. |
||
273 | * |
||
274 | * @param array $object |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | 13 | protected function isRootObject($object) |
|
283 | |||
284 | /** |
||
285 | * @param array $data |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 15 | protected function isCollection($data) |
|
294 | |||
295 | /** |
||
296 | * @param array $data |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | 15 | protected function isNull($data) |
|
304 | |||
305 | /** |
||
306 | * @param array $data |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | 14 | protected function isEmpty($data) |
|
314 | |||
315 | /** |
||
316 | * @param array $data |
||
317 | * @param array $relationships |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | 15 | protected function fillRelationships($data, $relationships) |
|
335 | |||
336 | /** |
||
337 | * @param array $includedData |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | 24 | protected function parseRelationships($includedData) |
|
353 | |||
354 | /** |
||
355 | * @param array $data |
||
356 | * |
||
357 | * @return integer |
||
358 | */ |
||
359 | 25 | protected function getIdFromData(array $data) |
|
368 | |||
369 | /** |
||
370 | * Keep all sideloaded inclusion data on the top level. |
||
371 | * |
||
372 | * @param array $data |
||
373 | * |
||
374 | * @return array |
||
375 | */ |
||
376 | 24 | protected function pullOutNestedIncludedData(array $data) |
|
400 | |||
401 | /** |
||
402 | * Whether or not the serializer should include `links` for resource objects. |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | 24 | protected function shouldIncludeLinks() |
|
410 | |||
411 | /** |
||
412 | * Check if the objects are part of a collection or not |
||
413 | * |
||
414 | * @param $includeObject |
||
415 | * |
||
416 | * @return array |
||
417 | */ |
||
418 | 13 | private function createIncludeObjects($includeObject) |
|
430 | |||
431 | /** |
||
432 | * Sets the RootObjects, either as collection or not. |
||
433 | * |
||
434 | * @param $data |
||
435 | */ |
||
436 | 13 | private function createRootObjects($data) |
|
444 | |||
445 | |||
446 | /** |
||
447 | * Loops over the relationships of the provided data and formats it |
||
448 | * |
||
449 | * @param $data |
||
450 | * @param $relationship |
||
451 | * @param $nestedDepth |
||
452 | * |
||
453 | * @return array |
||
454 | */ |
||
455 | 7 | private function fillRelationshipAsCollection($data, $relationship, $nestedDepth) |
|
463 | |||
464 | |||
465 | /** |
||
466 | * @param $data |
||
467 | * @param $relationship |
||
468 | * @param $key |
||
469 | * |
||
470 | * @return array |
||
471 | */ |
||
472 | 10 | private function FillRelationshipAsSingleResource($data, $relationship, $key) |
|
488 | |||
489 | /** |
||
490 | * @param $includeKey |
||
491 | * @param $relationships |
||
492 | * @param $includeObject |
||
493 | * @param $key |
||
494 | * |
||
495 | * @return array |
||
496 | */ |
||
497 | 15 | private function buildRelationships($includeKey, $relationships, $includeObject, $key) |
|
524 | |||
525 | /** |
||
526 | * @param $includeKey |
||
527 | * @param $relationships |
||
528 | * |
||
529 | * @return array |
||
530 | */ |
||
531 | 15 | private function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) |
|
540 | |||
541 | /** |
||
542 | * @param $includeObject |
||
543 | * @param $relationship |
||
544 | * |
||
545 | * @return array |
||
546 | */ |
||
547 | 7 | private function addIncludedDataToRelationship($includeObject, $relationship) |
|
558 | } |
||
559 |