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 | * @param string $resourceKey |
||
147 | * @return array |
||
148 | */ |
||
149 | 2 | public function null($resourceKey) |
|
155 | |||
156 | /** |
||
157 | * Serialize the included data. |
||
158 | * |
||
159 | * @param ResourceInterface $resource |
||
160 | * @param array $data |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 24 | public function includedData(ResourceInterface $resource, array $data) |
|
190 | |||
191 | /** |
||
192 | * Indicates if includes should be side-loaded. |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 25 | public function sideloadIncludes() |
|
200 | |||
201 | /** |
||
202 | * @param array|null $data |
||
203 | * @param array $includedData |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 24 | public function injectData($data, $includedData) |
|
217 | |||
218 | /** |
||
219 | * Hook to manipulate the final sideloaded includes. |
||
220 | * The JSON API specification does not allow the root object to be included |
||
221 | * into the sideloaded `included`-array. We have to make sure it is |
||
222 | * filtered out, in case some object links to the root object in a |
||
223 | * relationship. |
||
224 | * |
||
225 | * @param array $includedData |
||
226 | * @param array $data |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | 24 | public function filterIncludes($includedData, $data) |
|
247 | |||
248 | /** |
||
249 | * Filter function to delete root objects from array. |
||
250 | * |
||
251 | * @param array $object |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 13 | protected function filterRootObject($object) |
|
259 | |||
260 | /** |
||
261 | * Set the root objects of the JSON API tree. |
||
262 | * |
||
263 | * @param array $objects |
||
264 | */ |
||
265 | protected function setRootObjects(array $objects = []) |
||
271 | |||
272 | /** |
||
273 | * Determines whether an object is a root object of the JSON API tree. |
||
274 | * |
||
275 | * @param array $object |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 13 | protected function isRootObject($object) |
|
284 | |||
285 | /** |
||
286 | * @param array $data |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | 15 | protected function isCollection($data) |
|
295 | |||
296 | /** |
||
297 | * @param array $data |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | 15 | protected function isNull($data) |
|
305 | |||
306 | /** |
||
307 | * @param array $data |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | 14 | protected function isEmpty($data) |
|
315 | |||
316 | /** |
||
317 | * @param array $data |
||
318 | * @param array $relationships |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | 15 | protected function fillRelationships($data, $relationships) |
|
336 | |||
337 | /** |
||
338 | * @param array $includedData |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | 24 | protected function parseRelationships($includedData) |
|
354 | |||
355 | /** |
||
356 | * @param array $data |
||
357 | * |
||
358 | * @return integer |
||
359 | */ |
||
360 | 25 | protected function getIdFromData(array $data) |
|
369 | |||
370 | /** |
||
371 | * Keep all sideloaded inclusion data on the top level. |
||
372 | * |
||
373 | * @param array $data |
||
374 | * |
||
375 | * @return array |
||
376 | */ |
||
377 | 24 | protected function pullOutNestedIncludedData(array $data) |
|
401 | |||
402 | /** |
||
403 | * Whether or not the serializer should include `links` for resource objects. |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | 24 | protected function shouldIncludeLinks() |
|
411 | |||
412 | /** |
||
413 | * Check if the objects are part of a collection or not |
||
414 | * |
||
415 | * @param $includeObject |
||
416 | * |
||
417 | * @return array |
||
418 | */ |
||
419 | 13 | private function createIncludeObjects($includeObject) |
|
431 | |||
432 | /** |
||
433 | * Sets the RootObjects, either as collection or not. |
||
434 | * |
||
435 | * @param $data |
||
436 | */ |
||
437 | 13 | private function createRootObjects($data) |
|
445 | |||
446 | |||
447 | /** |
||
448 | * Loops over the relationships of the provided data and formats it |
||
449 | * |
||
450 | * @param $data |
||
451 | * @param $relationship |
||
452 | * @param $nestedDepth |
||
453 | * |
||
454 | * @return array |
||
455 | */ |
||
456 | 7 | private function fillRelationshipAsCollection($data, $relationship, $nestedDepth) |
|
464 | |||
465 | |||
466 | /** |
||
467 | * @param $data |
||
468 | * @param $relationship |
||
469 | * @param $key |
||
470 | * |
||
471 | * @return array |
||
472 | */ |
||
473 | 10 | private function FillRelationshipAsSingleResource($data, $relationship, $key) |
|
489 | |||
490 | /** |
||
491 | * @param $includeKey |
||
492 | * @param $relationships |
||
493 | * @param $includeObject |
||
494 | * @param $key |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | 15 | private function buildRelationships($includeKey, $relationships, $includeObject, $key) |
|
525 | |||
526 | /** |
||
527 | * @param $includeKey |
||
528 | * @param $relationships |
||
529 | * |
||
530 | * @return array |
||
531 | */ |
||
532 | 15 | private function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) |
|
541 | |||
542 | /** |
||
543 | * @param $includeObject |
||
544 | * @param $relationship |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | 7 | private function addIncludedDataToRelationship($includeObject, $relationship) |
|
559 | } |
||
560 |