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 |
||
| 11 | class JsonSerializer { |
||
| 12 | |||
| 13 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
| 14 | const CLOSURE_IDENTIFIER_KEY = '@closure'; |
||
| 15 | const FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Storage for object |
||
| 19 | * |
||
| 20 | * Used for recursion |
||
| 21 | * |
||
| 22 | * @var SplObjectStorage |
||
| 23 | */ |
||
| 24 | protected $objectStorage; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Object mapping for recursion |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $objectMapping = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Object mapping index |
||
| 35 | * |
||
| 36 | * @var integer |
||
| 37 | */ |
||
| 38 | protected $objectMappingIndex = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Support PRESERVE_ZERO_FRACTION json option |
||
| 42 | * |
||
| 43 | * @var boolean |
||
| 44 | */ |
||
| 45 | protected $preserveZeroFractionSupport; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Closure serializer instance |
||
| 49 | * |
||
| 50 | * @var SuperClosure\SerializerInterface |
||
| 51 | */ |
||
| 52 | protected $closureSerializer; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor. |
||
| 56 | * |
||
| 57 | * @param SuperClosure\SerializerInterface $closureSerializer |
||
| 58 | */ |
||
| 59 | public function __construct(ClosureSerializerInterface $closureSerializer = null) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Serialize the value in JSON |
||
| 69 | * |
||
| 70 | * @param mixed $value |
||
| 71 | * @return string JSON encoded |
||
| 72 | * @throws Zumba\Exception\JsonSerializerException |
||
| 73 | */ |
||
| 74 | public function serialize($value) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Calculate encoding options |
||
| 82 | * |
||
| 83 | * @return integer |
||
| 84 | */ |
||
| 85 | protected function calculateEncodeOptions() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Execute post-encoding actions |
||
| 95 | * |
||
| 96 | * @param string $encoded |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | protected function processEncodedValue($encoded) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Unserialize the value from JSON |
||
| 108 | * |
||
| 109 | * @param string $value |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public function unserialize($value) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Parse the data to be json encoded |
||
| 119 | * |
||
| 120 | * @param mixed $value |
||
| 121 | * @return mixed |
||
| 122 | * @throws Zumba\Exception\JsonSerializerException |
||
| 123 | */ |
||
| 124 | protected function serializeData($value) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Extract the data from an object |
||
| 153 | * |
||
| 154 | * @param object $value |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | protected function serializeObject($value) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Return the list of properties to be serialized |
||
| 173 | * |
||
| 174 | * @param ReflectionClass $ref |
||
| 175 | * @param object $value |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | protected function getObjectProperties($ref, $value) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Extract the object data |
||
| 192 | * |
||
| 193 | * @param object $value |
||
| 194 | * @param ReflectionClass $ref |
||
| 195 | * @param array $properties |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | protected function extractObjectData($value, $ref, $properties) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Parse the json decode to convert to objects again |
||
| 214 | * |
||
| 215 | * @param mixed $value |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | protected function unserializeData($value) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Convert the serialized array into an object |
||
| 239 | * |
||
| 240 | * @param aray $value |
||
| 241 | * @return object |
||
| 242 | * @throws Zumba\Exception\JsonSerializerException |
||
| 243 | */ |
||
| 244 | protected function unserializeObject($value) { |
||
| 280 | |||
| 281 | protected function restoreUsingUnserialize($className, $attributes) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Reset variables |
||
| 289 | * |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | protected function reset() { |
||
| 297 | |||
| 298 | } |
||
| 299 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..