Complex classes like JsonSerializer 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 JsonSerializer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class JsonSerializer |
||
16 | { |
||
17 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
18 | const CLOSURE_IDENTIFIER_KEY = '@closure'; |
||
19 | const UTF8ENCODED_IDENTIFIER_KEY = '@utf8encoded'; |
||
20 | const SCALAR_IDENTIFIER_KEY = '@scalar'; |
||
21 | const FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
22 | |||
23 | const KEY_UTF8ENCODED = 1; |
||
24 | const VALUE_UTF8ENCODED = 2; |
||
25 | |||
26 | /** |
||
27 | * Storage for object |
||
28 | * |
||
29 | * Used for recursion |
||
30 | * |
||
31 | * @var SplObjectStorage |
||
32 | */ |
||
33 | protected $objectStorage; |
||
34 | |||
35 | /** |
||
36 | * Object mapping for recursion |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $objectMapping = []; |
||
41 | |||
42 | /** |
||
43 | * Object mapping index |
||
44 | * |
||
45 | * @var integer |
||
46 | */ |
||
47 | protected $objectMappingIndex = 0; |
||
48 | |||
49 | /** |
||
50 | * Support PRESERVE_ZERO_FRACTION json option |
||
51 | * |
||
52 | * @var boolean |
||
53 | */ |
||
54 | protected $preserveZeroFractionSupport; |
||
55 | |||
56 | /** |
||
57 | * Map of custom object serializers |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $customObjectSerializerMap; |
||
62 | |||
63 | /** |
||
64 | * JsonSerializer constructor. |
||
65 | * @param ClosureSerializerInterface|null $closureSerializer |
||
66 | */ |
||
67 | public function __construct(ClosureSerializerInterface $closureSerializer = null) |
||
77 | |||
78 | /** |
||
79 | * @param $serializer |
||
80 | * @return JsonSerializer |
||
81 | */ |
||
82 | public function registerEntitySerializer(EntitySerializer $serializer) |
||
88 | |||
89 | /** |
||
90 | * @param $className |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function hasEntitySerializer($className) |
||
97 | |||
98 | /** |
||
99 | * @param $entity |
||
100 | * @return EntitySerializer|null |
||
101 | */ |
||
102 | protected function resolveEntitySerializer($entity) |
||
112 | |||
113 | /** |
||
114 | * @param $className |
||
115 | * @return EntitySerializer|null |
||
116 | */ |
||
117 | protected function resolveEntitySerializerByDirectClassName($className) |
||
131 | |||
132 | /** |
||
133 | * @param $object |
||
134 | * @return EntitySerializer|null |
||
135 | */ |
||
136 | protected function resolveEntitySerializerByInheritance($object) |
||
148 | |||
149 | /** |
||
150 | * Serialize the value in JSON |
||
151 | * |
||
152 | * @param mixed $value |
||
153 | * @return string JSON encoded |
||
154 | * @throws JsonSerializerException |
||
155 | */ |
||
156 | public function serialize($value) |
||
175 | |||
176 | /** |
||
177 | * Calculate encoding options |
||
178 | * |
||
179 | * @return integer |
||
180 | */ |
||
181 | protected function calculateEncodeOptions() |
||
189 | |||
190 | /** |
||
191 | * @param mixed $serializedData |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
236 | |||
237 | /** |
||
238 | * Execute post-encoding actions |
||
239 | * |
||
240 | * @param string $encoded |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function processEncodedValue($encoded) |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Parse the data to be json encoded |
||
254 | * |
||
255 | * @param mixed $value |
||
256 | * @return mixed |
||
257 | * @throws JsonSerializerException |
||
258 | */ |
||
259 | protected function serializeData($value) |
||
278 | |||
279 | /** |
||
280 | * Extract the data from an object |
||
281 | * |
||
282 | * @param object $value |
||
283 | * @return array |
||
284 | */ |
||
285 | protected function serializeObject($value) |
||
306 | |||
307 | /** |
||
308 | * Return the list of properties to be serialized |
||
309 | * |
||
310 | * @param ReflectionClass $ref |
||
311 | * @param object $value |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getObjectProperties($ref, $value) |
||
326 | |||
327 | /** |
||
328 | * Extract the object data |
||
329 | * |
||
330 | * @param object $value |
||
331 | * @param ReflectionClass $ref |
||
332 | * @param array $properties |
||
333 | * @return array |
||
334 | */ |
||
335 | protected function extractObjectData($value, $ref, $properties) |
||
349 | |||
350 | /** |
||
351 | * Unserialize the value from JSON |
||
352 | * |
||
353 | * @param string $value |
||
354 | * @return mixed |
||
355 | */ |
||
356 | public function unserialize($value) |
||
370 | |||
371 | /** |
||
372 | * Parse the json decode to convert to objects again |
||
373 | * |
||
374 | * @param mixed $value |
||
375 | * @return mixed |
||
376 | */ |
||
377 | protected function unserializeData($value) |
||
389 | |||
390 | /** |
||
391 | * @param mixed $serializedData |
||
392 | * |
||
393 | * @return mixed |
||
394 | */ |
||
395 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
431 | |||
432 | /** |
||
433 | * Convert the serialized array into an object |
||
434 | * |
||
435 | * @param array $value |
||
436 | * @return object |
||
437 | * @throws JsonSerializerException |
||
438 | */ |
||
439 | protected function unserializeObject($value) |
||
472 | |||
473 | /** |
||
474 | * @param $className |
||
475 | * @return object |
||
476 | */ |
||
477 | protected function getObjectInstance($className) |
||
482 | |||
483 | /** |
||
484 | * @param $value |
||
485 | * @return array |
||
486 | */ |
||
487 | protected function serializeClosure($value) |
||
495 | |||
496 | /** |
||
497 | * @param $value |
||
498 | * @return mixed |
||
499 | */ |
||
500 | protected function unserializeClosure($value) |
||
508 | |||
509 | /** |
||
510 | * Reset variables |
||
511 | * |
||
512 | * @return void |
||
513 | */ |
||
514 | protected function reset() |
||
520 | } |
||
521 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.