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 | 29 | 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 | 19 | 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 | 29 | public function item($resourceKey, array $data) |
|
91 | |||
92 | /** |
||
93 | * Serialize the paginator. |
||
94 | * |
||
95 | * @param PaginatorInterface $paginator |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | 3 | public function paginator(PaginatorInterface $paginator) |
|
129 | |||
130 | /** |
||
131 | * Serialize the meta. |
||
132 | * |
||
133 | * @param array $meta |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 28 | public function meta(array $meta) |
|
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | 2 | public function null() |
|
162 | |||
163 | /** |
||
164 | * Serialize the included data. |
||
165 | * |
||
166 | * @param ResourceInterface $resource |
||
167 | * @param array $data |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 28 | public function includedData(ResourceInterface $resource, array $data) |
|
197 | |||
198 | /** |
||
199 | * Indicates if includes should be side-loaded. |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 29 | public function sideloadIncludes() |
|
204 | { |
||
205 | 29 | return true; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param array $data |
||
210 | * @param array $includedData |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | 28 | public function injectData($data, $includedData) |
|
224 | |||
225 | /** |
||
226 | * Hook to manipulate the final sideloaded includes. |
||
227 | * The JSON API specification does not allow the root object to be included |
||
228 | * into the sideloaded `included`-array. We have to make sure it is |
||
229 | * filtered out, in case some object links to the root object in a |
||
230 | * relationship. |
||
231 | * |
||
232 | * @param array $includedData |
||
233 | * @param array $data |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | 28 | public function filterIncludes($includedData, $data) |
|
254 | |||
255 | /** |
||
256 | * Filter function to delete root objects from array. |
||
257 | * |
||
258 | * @param array $object |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | 15 | protected function filterRootObject($object) |
|
266 | |||
267 | /** |
||
268 | * Set the root objects of the JSON API tree. |
||
269 | * |
||
270 | * @param array $objects |
||
271 | */ |
||
272 | protected function setRootObjects(array $objects = []) |
||
278 | |||
279 | /** |
||
280 | * Determines whether an object is a root object of the JSON API tree. |
||
281 | * |
||
282 | * @param array $object |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 15 | protected function isRootObject($object) |
|
291 | |||
292 | /** |
||
293 | * @param array $data |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | 17 | protected function isCollection($data) |
|
302 | |||
303 | /** |
||
304 | * @param array $data |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | 17 | protected function isNull($data) |
|
312 | |||
313 | /** |
||
314 | * @param array $data |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | 16 | protected function isEmpty($data) |
|
322 | |||
323 | /** |
||
324 | * @param array $data |
||
325 | * @param array $relationships |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 17 | protected function fillRelationships($data, $relationships) |
|
343 | |||
344 | /** |
||
345 | * @param array $includedData |
||
346 | * |
||
347 | * @return array |
||
348 | */ |
||
349 | 28 | protected function parseRelationships($includedData) |
|
361 | |||
362 | /** |
||
363 | * @param array $data |
||
364 | * |
||
365 | * @return integer |
||
366 | */ |
||
367 | 29 | protected function getIdFromData(array $data) |
|
376 | |||
377 | /** |
||
378 | * Keep all sideloaded inclusion data on the top level. |
||
379 | * |
||
380 | * @param array $data |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | 28 | protected function pullOutNestedIncludedData(array $data) |
|
408 | |||
409 | /** |
||
410 | * Whether or not the serializer should include `links` for resource objects. |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | 28 | protected function shouldIncludeLinks() |
|
418 | |||
419 | /** |
||
420 | * Check if the objects are part of a collection or not |
||
421 | * |
||
422 | * @param $includeObject |
||
423 | * |
||
424 | * @return array |
||
425 | */ |
||
426 | 15 | private function createIncludeObjects($includeObject) |
|
438 | |||
439 | /** |
||
440 | * Sets the RootObjects, either as collection or not. |
||
441 | * |
||
442 | * @param $data |
||
443 | */ |
||
444 | 15 | private function createRootObjects($data) |
|
452 | |||
453 | |||
454 | /** |
||
455 | * Loops over the relationships of the provided data and formats it |
||
456 | * |
||
457 | * @param $data |
||
458 | * @param $relationship |
||
459 | * @param $key |
||
460 | * |
||
461 | * @return array |
||
462 | */ |
||
463 | 9 | private function fillRelationshipAsCollection($data, $relationship, $key) |
|
480 | |||
481 | |||
482 | /** |
||
483 | * @param $data |
||
484 | * @param $relationship |
||
485 | * @param $key |
||
486 | * |
||
487 | * @return array |
||
488 | */ |
||
489 | 10 | private function fillRelationshipAsSingleResource($data, $relationship, $key) |
|
505 | |||
506 | /** |
||
507 | * @param $includeKey |
||
508 | * @param $relationships |
||
509 | * @param $includeObject |
||
510 | * @param $key |
||
511 | * |
||
512 | * @return array |
||
513 | */ |
||
514 | 17 | private function buildRelationships($includeKey, $relationships, $includeObject, $key) |
|
541 | |||
542 | /** |
||
543 | * @param $includeKey |
||
544 | * @param $relationships |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | 17 | private function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) |
|
557 | |||
558 | /** |
||
559 | * @param $includeObject |
||
560 | * @param $relationship |
||
561 | * |
||
562 | * @return array |
||
563 | */ |
||
564 | 8 | private function addIncludedDataToRelationship($includeObject, $relationship) |
|
575 | } |
||
576 |