Complex classes like Serializer 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 Serializer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Serializer |
||
| 12 | { |
||
| 13 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
| 14 | const SCALAR_TYPE = '@scalar'; |
||
| 15 | const SCALAR_VALUE = '@value'; |
||
| 16 | const NULL_VAR = null; |
||
| 17 | const MAP_TYPE = '@map'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Storage for object. |
||
| 21 | * |
||
| 22 | * Used for recursion |
||
| 23 | * |
||
| 24 | * @var SplObjectStorage |
||
| 25 | */ |
||
| 26 | protected static $objectStorage; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Object mapping for recursion. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected static $objectMapping = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Object mapping index. |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected static $objectMappingIndex = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \NilPortugues\Serializer\Strategy\StrategyInterface|\NilPortugues\Serializer\Strategy\JsonStrategy |
||
| 44 | */ |
||
| 45 | protected $serializationStrategy; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $dateTimeClassType = ['DateTime', 'DateTimeImmutable', 'DateTimeZone', 'DateInterval', 'DatePeriod']; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $serializationMap = [ |
||
| 56 | 'array' => 'serializeArray', |
||
| 57 | 'integer' => 'serializeScalar', |
||
| 58 | 'double' => 'serializeScalar', |
||
| 59 | 'boolean' => 'serializeScalar', |
||
| 60 | 'string' => 'serializeScalar', |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | protected $isHHVM; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Hack specific serialization classes. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $unserializationMapHHVM = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param StrategyInterface $strategy |
||
| 77 | */ |
||
| 78 | public function __construct(StrategyInterface $strategy) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * This is handly specially in order to add additional data before the |
||
| 95 | * serialization process takes place using the transformer public methods, if any. |
||
| 96 | * |
||
| 97 | * @return StrategyInterface |
||
| 98 | */ |
||
| 99 | public function getTransformer() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Serialize the value in JSON. |
||
| 106 | * |
||
| 107 | * @param mixed $value |
||
| 108 | * |
||
| 109 | * @return string JSON encoded |
||
| 110 | * |
||
| 111 | * @throws SerializerException |
||
| 112 | */ |
||
| 113 | public function serialize($value) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Reset variables. |
||
| 122 | */ |
||
| 123 | protected function reset() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Parse the data to be json encoded. |
||
| 132 | * |
||
| 133 | * @param mixed $value |
||
| 134 | * |
||
| 135 | * @return mixed |
||
| 136 | * |
||
| 137 | * @throws SerializerException |
||
| 138 | */ |
||
| 139 | protected function serializeData($value) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param mixed $value |
||
| 161 | * |
||
| 162 | * @throws SerializerException |
||
| 163 | */ |
||
| 164 | protected function guardForUnsupportedValues($value) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Unserialize the value from string. |
||
| 183 | * |
||
| 184 | * @param mixed $value |
||
| 185 | * |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function unserialize($value) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Parse the json decode to convert to objects again. |
||
| 201 | * |
||
| 202 | * @param mixed $value |
||
| 203 | * |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | protected function unserializeData($value) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param $value |
||
| 231 | * |
||
| 232 | * @return float|int|null|bool |
||
| 233 | */ |
||
| 234 | protected function getScalarValue($value) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Convert the serialized array into an object. |
||
| 252 | * |
||
| 253 | * @param array $value |
||
| 254 | * |
||
| 255 | * @return object |
||
| 256 | * |
||
| 257 | * @throws SerializerException |
||
| 258 | */ |
||
| 259 | protected function unserializeObject(array $value) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param array $value |
||
| 283 | * @param string $className |
||
| 284 | * |
||
| 285 | * @return mixed |
||
| 286 | */ |
||
| 287 | protected function unserializeDateTimeFamilyObject(array $value, $className) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $className |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | protected function isDateTimeFamilyObject($className) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $className |
||
| 326 | * @param array $attributes |
||
| 327 | * |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | protected function restoreUsingUnserialize($className, array $attributes) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param array $value |
||
| 348 | * @param string $className |
||
| 349 | * |
||
| 350 | * @return object |
||
| 351 | */ |
||
| 352 | protected function unserializeUserDefinedObject(array $value, $className) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param array $value |
||
| 369 | * @param ReflectionClass $ref |
||
| 370 | * @param mixed $obj |
||
| 371 | * |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | protected function setUnserializedObjectProperties(array $value, ReflectionClass $ref, $obj) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param $value |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function serializeScalar($value) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $value |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | protected function serializeArray(array $value) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Extract the data from an object. |
||
| 428 | * |
||
| 429 | * @param mixed $value |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | protected function serializeObject($value) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param mixed $value |
||
| 449 | * @param string $className |
||
| 450 | * @param ReflectionClass $ref |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | protected function serializeInternalClass($value, $className, ReflectionClass $ref) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Return the list of properties to be serialized. |
||
| 465 | * |
||
| 466 | * @param ReflectionClass $ref |
||
| 467 | * @param $value |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | protected function getObjectProperties(ReflectionClass $ref, $value) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Extract the object data. |
||
| 483 | * |
||
| 484 | * @param mixed $value |
||
| 485 | * @param \ReflectionClass $rc |
||
| 486 | * @param array $properties |
||
| 487 | * |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | protected function extractObjectData($value, ReflectionClass $rc, array $properties) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param mixed $value |
||
| 502 | * @param ReflectionClass $rc |
||
| 503 | * @param array $properties |
||
| 504 | * @param array $data |
||
| 505 | */ |
||
| 506 | protected function extractCurrentObjectProperties($value, ReflectionClass $rc, array $properties, array &$data) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param mixed $value |
||
| 521 | * @param ReflectionClass $rc |
||
| 522 | * @param array $data |
||
| 523 | */ |
||
| 524 | protected function extractAllInhertitedProperties($value, ReflectionClass $rc, array &$data) |
||
| 536 | } |
||
| 537 |