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 | /** @var string|null */ |
||
21 | protected $baseUrl; |
||
22 | |||
23 | /** @var array */ |
||
24 | protected $rootObjects; |
||
25 | |||
26 | 19 | public function __construct(? string $baseUrl = null) |
|
31 | |||
32 | /** |
||
33 | * Serialize a collection. |
||
34 | * |
||
35 | * @param string $resourceKey |
||
36 | * @param array $data |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | 13 | public function collection($resourceKey, array $data) : array |
|
50 | |||
51 | /** |
||
52 | * Serialize an item. |
||
53 | * |
||
54 | * @param string $resourceKey |
||
55 | * @param array $data |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | 12 | public function serializeItem($resourceKey, array $data) : array |
|
99 | |||
100 | /** |
||
101 | * Serialize the paginator. |
||
102 | * |
||
103 | * @param PaginatorInterface $paginator |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function serializePaginator(PaginatorInterface $paginator) : array |
||
137 | |||
138 | 19 | public function meta(array $meta) : array |
|
153 | |||
154 | 1 | public function null() : array |
|
160 | |||
161 | 19 | public function includedData(ResourceInterface $resource, array $data) : array |
|
178 | |||
179 | /** |
||
180 | * Indicates if includes should be side-loaded. |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | 19 | public function sideloadIncludes() |
|
188 | |||
189 | /** |
||
190 | * @param array $data |
||
191 | * @param array $includedData |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | 19 | public function injectData($data, $includedData) : array |
|
205 | |||
206 | /** |
||
207 | * Hook to manipulate the final sideloaded includes. |
||
208 | * The JSON API specification does not allow the root object to be included |
||
209 | * into the sideloaded `included`-array. We have to make sure it is |
||
210 | * filtered out, in case some object links to the root object in a |
||
211 | * relationship. |
||
212 | * |
||
213 | * @param array $includedData |
||
214 | * @param array $data |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | 19 | public function filterIncludes($includedData, $data) : array |
|
235 | |||
236 | /** |
||
237 | * Get the mandatory fields for the serializer |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | 2 | public function getMandatoryFields() : array |
|
245 | |||
246 | /** |
||
247 | * Filter function to delete root objects from array. |
||
248 | * |
||
249 | * @param array $object |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | 3 | protected function filterRootObject($object) : bool |
|
257 | |||
258 | /** |
||
259 | * Set the root objects of the JSON API tree. |
||
260 | * |
||
261 | * @param array $objects |
||
262 | */ |
||
263 | 3 | protected function setRootObjects(array $objects = []) : void |
|
269 | |||
270 | /** |
||
271 | * Determines whether an object is a root object of the JSON API tree. |
||
272 | * |
||
273 | * @param array $object |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 3 | protected function isRootObject($object) : bool |
|
282 | |||
283 | /** |
||
284 | * @param array|null $data |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | 11 | protected function isCollection($data) : bool |
|
297 | |||
298 | /** |
||
299 | * @param array|null $data |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | 7 | protected function isNull($data) : bool |
|
311 | |||
312 | /** |
||
313 | * @param array|null $data |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | 6 | protected function isEmpty($data) : bool |
|
325 | |||
326 | /** |
||
327 | * @param array $data |
||
328 | * @param array $relationships |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | 7 | protected function fillRelationships($data, $relationships) : array |
|
346 | |||
347 | /** |
||
348 | * @param array $includedData |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | 19 | protected function parseRelationships($includedData) : array |
|
367 | |||
368 | /** |
||
369 | * @param array $data |
||
370 | * |
||
371 | * @return integer |
||
372 | */ |
||
373 | 12 | protected function getIdFromData(array $data) : int |
|
382 | |||
383 | /** |
||
384 | * Keep all sideloaded inclusion data on the top level. |
||
385 | * |
||
386 | * @param array $data |
||
387 | * |
||
388 | * @return array |
||
389 | */ |
||
390 | 19 | protected function pullOutNestedIncludedData(array $data) : array |
|
405 | |||
406 | /** |
||
407 | * Whether or not the serializer should include `links` for resource objects. |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | 18 | protected function shouldIncludeLinks() : bool |
|
415 | |||
416 | /** |
||
417 | * Check if the objects are part of a collection or not |
||
418 | * |
||
419 | * @param array $includeObject |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | 5 | private function createIncludeObjects($includeObject) : array |
|
435 | |||
436 | /** |
||
437 | * Sets the RootObjects, either as collection or not. |
||
438 | * |
||
439 | * @param array $data |
||
440 | */ |
||
441 | 3 | private function createRootObjects(array $data) : void |
|
449 | |||
450 | 3 | private function fillRelationshipAsCollection($data, $relationship, $key) : array |
|
458 | |||
459 | 4 | private function fillRelationshipAsSingleResource($data, $relationship, $key) : array |
|
465 | |||
466 | 7 | private function buildRelationships(string $includeKey, array $relationships, array $includeObject, string $key) : array |
|
492 | |||
493 | 7 | private function addIncludeKeyToRelationsIfNotSet(string $includeKey, array $relationships) : array |
|
502 | |||
503 | /** |
||
504 | * @param array $includeObject |
||
505 | * @param array $relationship |
||
506 | * |
||
507 | * @return array |
||
508 | */ |
||
509 | 5 | private function addIncludedDataToRelationship(array $includeObject, array $relationship) : array |
|
520 | |||
521 | 18 | public function injectAvailableIncludeData($data, $availableIncludes) : array |
|
542 | |||
543 | 5 | private function addRelationshipLinks(array $resource, string $relationshipKey) : array |
|
561 | |||
562 | 5 | private function serializeIncludedObjectsWithCacheKey(array $includeObjects, array $linkedIds, array $serializedData) : array |
|
575 | } |
||
576 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.