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 $entity |
||
| 91 | * @return EntitySerializer|null |
||
| 92 | */ |
||
| 93 | protected function resolveEntitySerializer($entity) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $className |
||
| 106 | * @return EntitySerializer|null |
||
| 107 | */ |
||
| 108 | protected function resolveEntitySerializerByDirectClassName($className) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param $object |
||
| 125 | * @return EntitySerializer|null |
||
| 126 | */ |
||
| 127 | protected function resolveEntitySerializerByInheritance($object) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Serialize the value in JSON |
||
| 142 | * |
||
| 143 | * @param mixed $value |
||
| 144 | * @return string JSON encoded |
||
| 145 | * @throws JsonSerializerException |
||
| 146 | */ |
||
| 147 | public function serialize($value) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Calculate encoding options |
||
| 169 | * |
||
| 170 | * @return integer |
||
| 171 | */ |
||
| 172 | protected function calculateEncodeOptions() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param mixed $serializedData |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Execute post-encoding actions |
||
| 230 | * |
||
| 231 | * @param string $encoded |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | protected function processEncodedValue($encoded) |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Parse the data to be json encoded |
||
| 245 | * |
||
| 246 | * @param mixed $value |
||
| 247 | * @return mixed |
||
| 248 | * @throws JsonSerializerException |
||
| 249 | */ |
||
| 250 | protected function serializeData($value) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Extract the data from an object |
||
| 272 | * |
||
| 273 | * @param object $value |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | protected function serializeObject($value) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return the list of properties to be serialized |
||
| 300 | * |
||
| 301 | * @param ReflectionClass $ref |
||
| 302 | * @param object $value |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | protected function getObjectProperties($ref, $value) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Extract the object data |
||
| 320 | * |
||
| 321 | * @param object $value |
||
| 322 | * @param ReflectionClass $ref |
||
| 323 | * @param array $properties |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | protected function extractObjectData($value, $ref, $properties) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Unserialize the value from JSON |
||
| 343 | * |
||
| 344 | * @param string $value |
||
| 345 | * @return mixed |
||
| 346 | */ |
||
| 347 | public function unserialize($value) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Parse the json decode to convert to objects again |
||
| 364 | * |
||
| 365 | * @param mixed $value |
||
| 366 | * @return mixed |
||
| 367 | */ |
||
| 368 | protected function unserializeData($value) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param mixed $serializedData |
||
| 383 | * |
||
| 384 | * @return mixed |
||
| 385 | */ |
||
| 386 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Convert the serialized array into an object |
||
| 425 | * |
||
| 426 | * @param array $value |
||
| 427 | * @return object |
||
| 428 | * @throws JsonSerializerException |
||
| 429 | */ |
||
| 430 | protected function unserializeObject($value) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param $className |
||
| 466 | * @return object |
||
| 467 | */ |
||
| 468 | protected function getObjectInstance($className) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param $value |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | protected function serializeClosure($value) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param $value |
||
| 488 | * @return mixed |
||
| 489 | */ |
||
| 490 | protected function unserializeClosure($value) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Reset variables |
||
| 500 | * |
||
| 501 | * @return void |
||
| 502 | */ |
||
| 503 | protected function reset() |
||
| 509 | } |
||
| 510 |
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
$aand$care used. There was no need to assign$b.Instead, the list call could have been.