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 |
||
14 | class JsonSerializer |
||
15 | { |
||
16 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
17 | const CLOSURE_IDENTIFIER_KEY = '@closure'; |
||
18 | const UTF8ENCODED_IDENTIFIER_KEY = '@utf8encoded'; |
||
19 | const SCALAR_IDENTIFIER_KEY = '@scalar'; |
||
20 | const FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
21 | |||
22 | const KEY_UTF8ENCODED = 1; |
||
23 | const VALUE_UTF8ENCODED = 2; |
||
24 | |||
25 | /** |
||
26 | * Storage for object |
||
27 | * |
||
28 | * Used for recursion |
||
29 | * |
||
30 | * @var SplObjectStorage |
||
31 | */ |
||
32 | protected $objectStorage; |
||
33 | |||
34 | /** |
||
35 | * Object mapping for recursion |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $objectMapping = []; |
||
40 | |||
41 | /** |
||
42 | * Object mapping index |
||
43 | * |
||
44 | * @var integer |
||
45 | */ |
||
46 | protected $objectMappingIndex = 0; |
||
47 | |||
48 | /** |
||
49 | * Support PRESERVE_ZERO_FRACTION json option |
||
50 | * |
||
51 | * @var boolean |
||
52 | */ |
||
53 | protected $preserveZeroFractionSupport; |
||
54 | |||
55 | /** |
||
56 | * Map of custom object serializers |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $customObjectSerializerMap; |
||
61 | |||
62 | /** |
||
63 | * JsonSerializer constructor. |
||
64 | * @param ClosureSerializerInterface|null $closureSerializer |
||
65 | */ |
||
66 | public function __construct(ClosureSerializerInterface $closureSerializer = null) |
||
76 | |||
77 | /** |
||
78 | * @param $serializer |
||
79 | * @return JsonSerializer |
||
80 | */ |
||
81 | public function registerEntitySerializer(EntitySerializer $serializer) |
||
87 | |||
88 | /** |
||
89 | * @param $entity |
||
90 | * @return EntitySerializer|null |
||
91 | */ |
||
92 | protected function resolveEntitySerializer($entity) |
||
102 | |||
103 | /** |
||
104 | * @param $className |
||
105 | * @return EntitySerializer|null |
||
106 | */ |
||
107 | protected function resolveEntitySerializerByDirectClassName($className) |
||
121 | |||
122 | /** |
||
123 | * @param $object |
||
124 | * @return EntitySerializer|null |
||
125 | */ |
||
126 | protected function resolveEntitySerializerByInheritance($object) |
||
138 | |||
139 | /** |
||
140 | * Serialize the value in JSON |
||
141 | * |
||
142 | * @param mixed $value |
||
143 | * @return string JSON encoded |
||
144 | * @throws JsonSerializerException |
||
145 | */ |
||
146 | public function serialize($value) |
||
165 | |||
166 | /** |
||
167 | * Calculate encoding options |
||
168 | * |
||
169 | * @return integer |
||
170 | */ |
||
171 | protected function calculateEncodeOptions() |
||
179 | |||
180 | /** |
||
181 | * @param mixed $serializedData |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
226 | |||
227 | /** |
||
228 | * Execute post-encoding actions |
||
229 | * |
||
230 | * @param string $encoded |
||
231 | * @return string |
||
232 | */ |
||
233 | protected function processEncodedValue($encoded) |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Parse the data to be json encoded |
||
244 | * |
||
245 | * @param mixed $value |
||
246 | * @return mixed |
||
247 | * @throws JsonSerializerException |
||
248 | */ |
||
249 | protected function serializeData($value) |
||
268 | |||
269 | /** |
||
270 | * Extract the data from an object |
||
271 | * |
||
272 | * @param object $value |
||
273 | * @return array |
||
274 | */ |
||
275 | protected function serializeObject($value) |
||
296 | |||
297 | /** |
||
298 | * Return the list of properties to be serialized |
||
299 | * |
||
300 | * @param ReflectionClass $ref |
||
301 | * @param object $value |
||
302 | * @return array |
||
303 | */ |
||
304 | protected function getObjectProperties($ref, $value) |
||
316 | |||
317 | /** |
||
318 | * Extract the object data |
||
319 | * |
||
320 | * @param object $value |
||
321 | * @param ReflectionClass $ref |
||
322 | * @param array $properties |
||
323 | * @return array |
||
324 | */ |
||
325 | protected function extractObjectData($value, $ref, $properties) |
||
339 | |||
340 | /** |
||
341 | * Unserialize the value from JSON |
||
342 | * |
||
343 | * @param string $value |
||
344 | * @return mixed |
||
345 | */ |
||
346 | public function unserialize($value) |
||
360 | |||
361 | /** |
||
362 | * Parse the json decode to convert to objects again |
||
363 | * |
||
364 | * @param mixed $value |
||
365 | * @return mixed |
||
366 | */ |
||
367 | protected function unserializeData($value) |
||
379 | |||
380 | /** |
||
381 | * @param mixed $serializedData |
||
382 | * |
||
383 | * @return mixed |
||
384 | */ |
||
385 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
421 | |||
422 | /** |
||
423 | * Convert the serialized array into an object |
||
424 | * |
||
425 | * @param array $value |
||
426 | * @return object |
||
427 | * @throws JsonSerializerException |
||
428 | */ |
||
429 | protected function unserializeObject($value) |
||
462 | |||
463 | /** |
||
464 | * @param $className |
||
465 | * @return object |
||
466 | */ |
||
467 | protected function getObjectInstance($className) |
||
472 | |||
473 | /** |
||
474 | * @param $value |
||
475 | * @return array |
||
476 | */ |
||
477 | protected function serializeClosure($value) |
||
484 | |||
485 | /** |
||
486 | * @param $value |
||
487 | * @return mixed |
||
488 | */ |
||
489 | protected function unserializeClosure($value) |
||
496 | |||
497 | /** |
||
498 | * Reset variables |
||
499 | * |
||
500 | * @return void |
||
501 | */ |
||
502 | protected function reset() |
||
508 | } |
||
509 |
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.