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 | * Map of custom object serializers |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $customObjectSerializerMap; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Constructor. |
||
| 69 | * |
||
| 70 | * @param ClosureSerializerInterface $closureSerializer |
||
| 71 | * @param array $customObjectSerializerMap |
||
| 72 | */ |
||
| 73 | public function __construct(ClosureSerializerInterface $closureSerializer = null, $customObjectSerializerMap = array()) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Serialize the value in JSON |
||
| 82 | * |
||
| 83 | * @param mixed $value |
||
| 84 | * @return string JSON encoded |
||
| 85 | * @throws JsonSerializerException |
||
| 86 | */ |
||
| 87 | public function serialize($value) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Calculate encoding options |
||
| 109 | * |
||
| 110 | * @return integer |
||
| 111 | */ |
||
| 112 | protected function calculateEncodeOptions() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param mixed $serializedData |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Execute post-encoding actions |
||
| 170 | * |
||
| 171 | * @param string $encoded |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function processEncodedValue($encoded) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Unserialize the value from JSON |
||
| 184 | * |
||
| 185 | * @param string $value |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function unserialize($value) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Parse the data to be json encoded |
||
| 205 | * |
||
| 206 | * @param mixed $value |
||
| 207 | * @return mixed |
||
| 208 | * @throws JsonSerializerException |
||
| 209 | */ |
||
| 210 | protected function serializeData($value) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Extract the data from an object |
||
| 240 | * |
||
| 241 | * @param object $value |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | protected function serializeObject($value) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the list of properties to be serialized |
||
| 272 | * |
||
| 273 | * @param ReflectionClass $ref |
||
| 274 | * @param object $value |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | protected function getObjectProperties($ref, $value) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Extract the object data |
||
| 292 | * |
||
| 293 | * @param object $value |
||
| 294 | * @param ReflectionClass $ref |
||
| 295 | * @param array $properties |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | protected function extractObjectData($value, $ref, $properties) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Parse the json decode to convert to objects again |
||
| 315 | * |
||
| 316 | * @param mixed $value |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | protected function unserializeData($value) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param mixed $serializedData |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Convert the serialized array into an object |
||
| 383 | * |
||
| 384 | * @param array $value |
||
| 385 | * @return object |
||
| 386 | * @throws JsonSerializerException |
||
| 387 | */ |
||
| 388 | protected function unserializeObject($value) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return boolean |
||
| 445 | */ |
||
| 446 | protected function isSplList($className) |
||
| 450 | |||
| 451 | protected function restoreUsingUnserialize($className, $attributes) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Reset variables |
||
| 460 | * |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | protected function reset() |
||
| 469 | } |
||
| 470 |
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.