Complex classes like JsonTransformer 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 JsonTransformer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class JsonTransformer extends Transformer implements HalTransformer |
||
19 | { |
||
20 | const EMBEDDED_KEY = '_embedded'; |
||
21 | const META_KEY = '_meta'; |
||
22 | |||
23 | const LINKS_KEY = '_links'; |
||
24 | const LINKS_TEMPLATED_KEY = 'templated'; |
||
25 | const LINKS_DEPRECATION_KEY = 'deprecation'; |
||
26 | const LINKS_TYPE_KEY = 'type'; |
||
27 | const LINKS_NAME_KEY = 'name'; |
||
28 | const LINKS_PROFILE_KEY = 'profile'; |
||
29 | const LINKS_TITLE_KEY = 'title'; |
||
30 | const LINKS_HREF_LANG_KEY = 'hreflang'; |
||
31 | const LINKS_HREF = 'href'; |
||
32 | |||
33 | const MEDIA_PROFILE_KEY = 'profile'; |
||
34 | |||
35 | const SELF_LINK = 'self'; |
||
36 | const FIRST_LINK = 'first'; |
||
37 | const LAST_LINK = 'last'; |
||
38 | const PREV_LINK = 'prev'; |
||
39 | const NEXT_LINK = 'next'; |
||
40 | const LINKS_CURIES = 'curies'; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $curies = []; |
||
46 | |||
47 | /** |
||
48 | * @param array $value |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function serialize($value) |
||
68 | |||
69 | /** |
||
70 | * @param array $data |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | protected function outputStrategy(array &$data) |
||
78 | |||
79 | /** |
||
80 | * @param array $value |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | protected function serializeObject(array $value) |
||
91 | |||
92 | /** |
||
93 | * @param array $value |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | protected function preSerialization(array $value) |
||
107 | |||
108 | /** |
||
109 | * @param array $data |
||
110 | *d |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | protected function serialization(array $data) |
||
121 | |||
122 | /** |
||
123 | * @param array $data |
||
124 | */ |
||
125 | protected function setEmbeddedResources(array &$data) |
||
134 | |||
135 | /** |
||
136 | * @param array $data |
||
137 | * @param array $value |
||
138 | * @param string $propertyName |
||
139 | */ |
||
140 | protected function setEmbeddedForResource(array &$data, array &$value, $propertyName) |
||
141 | { |
||
142 | if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY])) { |
||
143 | $type = $value[Serializer::CLASS_IDENTIFIER_KEY]; |
||
144 | if (\is_scalar($type) && !empty($this->mappings[$type])) { |
||
145 | $idProperties = $this->mappings[$type]->getIdProperties(); |
||
146 | CuriesHelper::addCurieForResource($this->mappings, $this->curies, $type); |
||
147 | |||
148 | if (false === \in_array($propertyName, $idProperties)) { |
||
149 | $data[self::EMBEDDED_KEY][$propertyName] = $value; |
||
150 | |||
151 | list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues( |
||
152 | $this->mappings, |
||
153 | $value, |
||
154 | $type |
||
155 | ); |
||
156 | |||
157 | $this->addEmbeddedResourceLinks($data, $propertyName, $idProperties, $idValues, $type); |
||
158 | $this->addEmbeddedResourceAdditionalLinks($data, $value, $propertyName, $type); |
||
159 | $this->addEmbeddedResourceLinkToLinks($data, $propertyName, $idProperties, $idValues, $type); |
||
160 | |||
161 | unset($data[$propertyName]); |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | $data[$propertyName] = $value; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param array $data |
||
171 | * @param string $propertyName |
||
172 | * @param array $idProperties |
||
173 | * @param array $idValues |
||
174 | * @param string $type |
||
175 | */ |
||
176 | protected function addEmbeddedResourceLinks( |
||
177 | array &$data, |
||
178 | $propertyName, |
||
179 | array &$idProperties, |
||
180 | array &$idValues, |
||
181 | $type |
||
182 | ) { |
||
183 | $href = self::buildUrl( |
||
184 | $this->mappings, |
||
185 | $idProperties, |
||
186 | $idValues, |
||
187 | $this->mappings[$type]->getResourceUrl(), |
||
188 | $type |
||
189 | ); |
||
190 | |||
191 | if ($href != $this->mappings[$type]->getResourceUrl()) { |
||
192 | $data[self::EMBEDDED_KEY][$propertyName][self::LINKS_KEY][self::SELF_LINK][self::LINKS_HREF] = $href; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param array $data |
||
198 | * @param array $value |
||
199 | * @param string $propertyName |
||
200 | * @param string $type |
||
201 | */ |
||
202 | protected function addEmbeddedResourceAdditionalLinks(array &$data, array &$value, $propertyName, $type) |
||
215 | |||
216 | /** |
||
217 | * @param array $copy |
||
218 | * @param string $type |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function getResponseAdditionalLinks(array $copy, $type) |
||
252 | |||
253 | /** |
||
254 | * @param string $type |
||
255 | * @param string $propertyName |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function getPropertyNameWithCurie($type, $propertyName) |
||
272 | |||
273 | /** |
||
274 | * @param array $data |
||
275 | * @param string $propertyName |
||
276 | * @param array $idProperties |
||
277 | * @param array $idValues |
||
278 | * @param string $type |
||
279 | */ |
||
280 | protected function addEmbeddedResourceLinkToLinks( |
||
299 | |||
300 | /** |
||
301 | * @param array $data |
||
302 | * @param array $value |
||
303 | * @param string $propertyName |
||
304 | */ |
||
305 | protected function setEmbeddedForResourceArray(array &$data, array &$value, $propertyName) |
||
315 | |||
316 | /** |
||
317 | * @param array $data |
||
318 | * @param string $propertyName |
||
319 | * @param array $arrayValue |
||
320 | */ |
||
321 | protected function setEmbeddedArrayValue(array &$data, $propertyName, array &$arrayValue) |
||
337 | |||
338 | /** |
||
339 | * @param mixed $inArrayValue |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function isResourceInArray($inArrayValue) |
||
347 | |||
348 | /** |
||
349 | * @param array $data |
||
350 | * @param string $propertyName |
||
351 | * @param string $type |
||
352 | * @param string $inArrayProperty |
||
353 | * @param array $inArrayValue |
||
354 | */ |
||
355 | protected function addArrayValueResourceToEmbedded( |
||
356 | array &$data, |
||
357 | $propertyName, |
||
358 | $type, |
||
359 | $inArrayProperty, |
||
360 | array &$inArrayValue |
||
361 | ) { |
||
362 | list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues( |
||
363 | $this->mappings, |
||
364 | $inArrayValue, |
||
365 | $type |
||
366 | ); |
||
367 | |||
368 | $href = self::buildUrl( |
||
369 | $this->mappings, |
||
370 | $idProperties, |
||
371 | $idValues, |
||
372 | $this->mappings[$type]->getResourceUrl(), |
||
373 | $type |
||
374 | ); |
||
375 | |||
376 | if ($href != $this->mappings[$type]->getResourceUrl()) { |
||
377 | $data[self::EMBEDDED_KEY][$propertyName][$inArrayProperty][self::LINKS_KEY][self::SELF_LINK][self::LINKS_HREF] = $href; |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param array $data |
||
383 | */ |
||
384 | protected function setResponseLinks(array &$data) |
||
401 | |||
402 | /** |
||
403 | * @param array $data |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | protected function postSerialization(array &$data) |
||
418 | |||
419 | /** |
||
420 | * @param array $response |
||
421 | */ |
||
422 | protected function setResponseMeta(array &$response) |
||
428 | |||
429 | /** |
||
430 | * @param \NilPortugues\Api\Mapping\Mapping[] $mappings |
||
431 | * @param $idProperties |
||
432 | * @param $idValues |
||
433 | * @param $url |
||
434 | * @param $type |
||
435 | * |
||
436 | * @return mixed |
||
437 | */ |
||
438 | protected static function buildUrl(array &$mappings, $idProperties, $idValues, $url, $type) |
||
462 | |||
463 | /** |
||
464 | * @param $idPropertyName |
||
465 | * @param $idValues |
||
466 | * @param $url |
||
467 | * |
||
468 | * @return mixed |
||
469 | */ |
||
470 | protected static function secondPassBuildUrl($idPropertyName, $idValues, $url) |
||
491 | |||
492 | /** |
||
493 | * @param $original |
||
494 | * @param $idValues |
||
495 | * @param $url |
||
496 | * |
||
497 | * @return mixed |
||
498 | */ |
||
499 | protected static function toCamelCase($original, $idValues, $url) |
||
500 | { |
||
501 | foreach ($original as &$o) { |
||
502 | $o = '{'.self::underscoreToCamelCase(self::camelCaseToUnderscore($o)).'}'; |
||
503 | } |
||
504 | |||
505 | return \str_replace($original, $idValues, $url); |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * @param $original |
||
510 | * @param $idValues |
||
511 | * @param $url |
||
512 | * |
||
513 | * @return mixed |
||
514 | */ |
||
515 | protected static function toLowerFirstCamelCase($original, $idValues, $url) |
||
525 | |||
526 | /** |
||
527 | * @param $original |
||
528 | * @param $idValues |
||
529 | * @param $url |
||
530 | * |
||
531 | * @return mixed |
||
532 | */ |
||
533 | protected static function toUnderScore($original, $idValues, $url) |
||
534 | { |
||
535 | foreach ($original as &$o) { |
||
536 | $o = '{'.self::camelCaseToUnderscore($o).'}'; |
||
537 | } |
||
538 | |||
539 | return \str_replace($original, $idValues, $url); |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Transforms a given string from camelCase to under_score style. |
||
544 | * |
||
545 | * @param string $camel |
||
546 | * @param string $splitter |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | protected static function camelCaseToUnderscore($camel, $splitter = '_') |
||
560 | |||
561 | /** |
||
562 | * Converts a underscore string to camelCase. |
||
563 | * |
||
564 | * @param string $string |
||
565 | * |
||
566 | * @return string |
||
567 | */ |
||
568 | protected static function underscoreToCamelCase($string) |
||
572 | } |
||
573 |