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 | |||
| 14 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
| 15 | const CLOSURE_IDENTIFIER_KEY = '@closure'; |
||
| 16 | const UTF8ENCODED_IDENTIFIER_KEY = '@utf8encoded'; |
||
| 17 | const SCALAR_IDENTIFIER_KEY = '@scalar'; |
||
| 18 | const FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
| 19 | |||
| 20 | const KEY_UTF8ENCODED = 1; |
||
| 21 | const VALUE_UTF8ENCODED = 2; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Storage for object |
||
| 25 | * |
||
| 26 | * Used for recursion |
||
| 27 | * |
||
| 28 | * @var SplObjectStorage |
||
| 29 | */ |
||
| 30 | protected $objectStorage; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Object mapping for recursion |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $objectMapping = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Object mapping index |
||
| 41 | * |
||
| 42 | * @var integer |
||
| 43 | */ |
||
| 44 | protected $objectMappingIndex = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Support PRESERVE_ZERO_FRACTION json option |
||
| 48 | * |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $preserveZeroFractionSupport; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Closure serializer instance |
||
| 55 | * |
||
| 56 | * @var ClosureSerializerInterface |
||
| 57 | */ |
||
| 58 | protected $closureSerializer; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor. |
||
| 62 | * |
||
| 63 | * @param ClosureSerializerInterface $closureSerializer |
||
| 64 | */ |
||
| 65 | public function __construct(ClosureSerializerInterface $closureSerializer = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Serialize the value in JSON |
||
| 73 | * |
||
| 74 | * @param mixed $value |
||
| 75 | * @return string JSON encoded |
||
| 76 | * @throws JsonSerializerException |
||
| 77 | */ |
||
| 78 | public function serialize($value) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Calculate encoding options |
||
| 100 | * |
||
| 101 | * @return integer |
||
| 102 | */ |
||
| 103 | protected function calculateEncodeOptions() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param mixed $serializedData |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Execute post-encoding actions |
||
| 161 | * |
||
| 162 | * @param string $encoded |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | protected function processEncodedValue($encoded) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Unserialize the value from JSON |
||
| 175 | * |
||
| 176 | * @param string $value |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function unserialize($value) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Parse the data to be json encoded |
||
| 196 | * |
||
| 197 | * @param mixed $value |
||
| 198 | * @return mixed |
||
| 199 | * @throws JsonSerializerException |
||
| 200 | */ |
||
| 201 | protected function serializeData($value) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Extract the data from an object |
||
| 231 | * |
||
| 232 | * @param object $value |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | protected function serializeObject($value) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Return the list of properties to be serialized |
||
| 252 | * |
||
| 253 | * @param ReflectionClass $ref |
||
| 254 | * @param object $value |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | protected function getObjectProperties($ref, $value) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Extract the object data |
||
| 272 | * |
||
| 273 | * @param object $value |
||
| 274 | * @param ReflectionClass $ref |
||
| 275 | * @param array $properties |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | protected function extractObjectData($value, $ref, $properties) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Parse the json decode to convert to objects again |
||
| 295 | * |
||
| 296 | * @param mixed $value |
||
| 297 | * @return mixed |
||
| 298 | */ |
||
| 299 | protected function unserializeData($value) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param mixed $serializedData |
||
| 321 | * |
||
| 322 | * @return mixed |
||
| 323 | */ |
||
| 324 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Convert the serialized array into an object |
||
| 363 | * |
||
| 364 | * @param array $value |
||
| 365 | * @return object |
||
| 366 | * @throws JsonSerializerException |
||
| 367 | */ |
||
| 368 | protected function unserializeObject($value) |
||
| 405 | |||
| 406 | protected function restoreUsingUnserialize($className, $attributes) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Reset variables |
||
| 415 | * |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | protected function reset() |
||
| 424 | } |
||
| 425 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.