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 | 27 | 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 | 27 | public function item($resourceKey, array $data) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Serialize the paginator. |
||
| 92 | * |
||
| 93 | * @param PaginatorInterface $paginator |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 3 | public function paginator(PaginatorInterface $paginator) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Serialize the meta. |
||
| 130 | * |
||
| 131 | * @param array $meta |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 26 | public function meta(array $meta) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | 2 | public function null() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Serialize the included data. |
||
| 163 | * |
||
| 164 | * @param ResourceInterface $resource |
||
| 165 | * @param array $data |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 26 | public function includedData(ResourceInterface $resource, array $data) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Indicates if includes should be side-loaded. |
||
| 198 | * |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | 27 | public function sideloadIncludes() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param array $data |
||
| 208 | * @param array $includedData |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | 26 | public function injectData($data, $includedData) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Hook to manipulate the final sideloaded includes. |
||
| 225 | * The JSON API specification does not allow the root object to be included |
||
| 226 | * into the sideloaded `included`-array. We have to make sure it is |
||
| 227 | * filtered out, in case some object links to the root object in a |
||
| 228 | * relationship. |
||
| 229 | * |
||
| 230 | * @param array $includedData |
||
| 231 | * @param array $data |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 26 | public function filterIncludes($includedData, $data) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Filter function to delete root objects from array. |
||
| 255 | * |
||
| 256 | * @param array $object |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | 13 | protected function filterRootObject($object) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Set the root objects of the JSON API tree. |
||
| 267 | * |
||
| 268 | * @param array $objects |
||
| 269 | */ |
||
| 270 | protected function setRootObjects(array $objects = []) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Determines whether an object is a root object of the JSON API tree. |
||
| 279 | * |
||
| 280 | * @param array $object |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 13 | protected function isRootObject($object) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $data |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | 15 | protected function isCollection($data) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param array $data |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 15 | protected function isNull($data) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param array $data |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | 14 | protected function isEmpty($data) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param array $data |
||
| 323 | * @param array $relationships |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | 15 | protected function fillRelationships($data, $relationships) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param array $includedData |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | 26 | protected function parseRelationships($includedData) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param array $data |
||
| 362 | * |
||
| 363 | * @return integer |
||
| 364 | */ |
||
| 365 | 27 | protected function getIdFromData(array $data) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Keep all sideloaded inclusion data on the top level. |
||
| 377 | * |
||
| 378 | * @param array $data |
||
| 379 | * |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | 26 | protected function pullOutNestedIncludedData(array $data) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Whether or not the serializer should include `links` for resource objects. |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | 26 | protected function shouldIncludeLinks() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Check if the objects are part of a collection or not |
||
| 419 | * |
||
| 420 | * @param $includeObject |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | 13 | private function createIncludeObjects($includeObject) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Sets the RootObjects, either as collection or not. |
||
| 439 | * |
||
| 440 | * @param $data |
||
| 441 | */ |
||
| 442 | 13 | private function createRootObjects($data) |
|
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Loops over the relationships of the provided data and formats it |
||
| 454 | * |
||
| 455 | * @param $data |
||
| 456 | * @param $relationship |
||
| 457 | * @param $nestedDepth |
||
| 458 | * |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | 7 | private function fillRelationshipAsCollection($data, $relationship, $nestedDepth) |
|
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * @param $data |
||
| 473 | * @param $relationship |
||
| 474 | * @param $key |
||
| 475 | * |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | 10 | private function FillRelationshipAsSingleResource($data, $relationship, $key) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @param $includeKey |
||
| 497 | * @param $relationships |
||
| 498 | * @param $includeObject |
||
| 499 | * @param $key |
||
| 500 | * |
||
| 501 | * @return array |
||
| 502 | */ |
||
| 503 | 15 | private function buildRelationships($includeKey, $relationships, $includeObject, $key) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param $includeKey |
||
| 533 | * @param $relationships |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | 15 | private function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * @param $includeObject |
||
| 549 | * @param $relationship |
||
| 550 | * |
||
| 551 | * @return array |
||
| 552 | */ |
||
| 553 | 7 | private function addIncludedDataToRelationship($includeObject, $relationship) |
|
| 564 | } |
||
| 565 |