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 |
||
12 | class JsonSerializer |
||
13 | { |
||
14 | |||
15 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
16 | const CLOSURE_IDENTIFIER_KEY = '@closure'; |
||
17 | const UTF8ENCODED_IDENTIFIER_KEY = '@utf8encoded'; |
||
18 | const SCALAR_IDENTIFIER_KEY = '@scalar'; |
||
19 | const FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
20 | |||
21 | const KEY_UTF8ENCODED = 1; |
||
22 | const VALUE_UTF8ENCODED = 2; |
||
23 | |||
24 | const UNDECLARED_PROPERTY_MODE_SET = 1; |
||
25 | const UNDECLARED_PROPERTY_MODE_IGNORE = 2; |
||
26 | const UNDECLARED_PROPERTY_MODE_EXCEPTION = 3; |
||
27 | |||
28 | /** |
||
29 | * Storage for object |
||
30 | * |
||
31 | * Used for recursion |
||
32 | * |
||
33 | * @var SplObjectStorage |
||
34 | */ |
||
35 | protected $objectStorage; |
||
36 | |||
37 | /** |
||
38 | * Object mapping for recursion |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $objectMapping = array(); |
||
43 | |||
44 | /** |
||
45 | * Object mapping index |
||
46 | * |
||
47 | * @var integer |
||
48 | */ |
||
49 | protected $objectMappingIndex = 0; |
||
50 | |||
51 | /** |
||
52 | * Support PRESERVE_ZERO_FRACTION json option |
||
53 | * |
||
54 | * @var boolean |
||
55 | */ |
||
56 | protected $preserveZeroFractionSupport; |
||
57 | |||
58 | /** |
||
59 | * Closure serializer instance |
||
60 | * |
||
61 | * @var ClosureSerializerInterface |
||
62 | */ |
||
63 | protected $closureSerializer; |
||
64 | |||
65 | /** |
||
66 | * Map of custom object serializers |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $customObjectSerializerMap; |
||
71 | |||
72 | /** |
||
73 | * Undefined Attribute Mode |
||
74 | * |
||
75 | * @var integer |
||
76 | */ |
||
77 | protected $undefinedAttributeMode = self::UNDECLARED_PROPERTY_MODE_SET; |
||
78 | |||
79 | /** |
||
80 | * Undefined Attribute Mode |
||
81 | * |
||
82 | * @var false|array |
||
83 | */ |
||
84 | protected $supportedClasses = false; |
||
85 | |||
86 | /** |
||
87 | * Constructor. |
||
88 | * |
||
89 | * @param ClosureSerializerInterface $closureSerializer |
||
90 | * @param array $customObjectSerializerMap |
||
91 | */ |
||
92 | public function __construct(ClosureSerializerInterface $closureSerializer = null, $customObjectSerializerMap = array()) |
||
98 | |||
99 | public function withSupportedClasses($supportedClasses) |
||
104 | |||
105 | public function isClassSupported($className) |
||
113 | |||
114 | /** |
||
115 | * Serialize the value in JSON |
||
116 | * |
||
117 | * @param mixed $value |
||
118 | * @return string JSON encoded |
||
119 | * @throws JsonSerializerException |
||
120 | */ |
||
121 | public function serialize($value) |
||
140 | |||
141 | /** |
||
142 | * Calculate encoding options |
||
143 | * |
||
144 | * @return integer |
||
145 | */ |
||
146 | protected function calculateEncodeOptions() |
||
154 | |||
155 | /** |
||
156 | * @param mixed $serializedData |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
201 | |||
202 | /** |
||
203 | * Execute post-encoding actions |
||
204 | * |
||
205 | * @param string $encoded |
||
206 | * @return string |
||
207 | */ |
||
208 | protected function processEncodedValue($encoded) |
||
215 | |||
216 | /** |
||
217 | * Unserialize the value from JSON |
||
218 | * |
||
219 | * @param string $value |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function unserialize($value) |
||
236 | |||
237 | /** |
||
238 | * Set unserialization mode for undeclared class properties |
||
239 | * |
||
240 | * @param integer $value One of the JsonSerializer::UNDECLARED_PROPERTY_MODE_* |
||
241 | * @return self |
||
242 | * @throws InvalidArgumentException When the value is not one of the UNDECLARED_PROPERTY_MODE_* options |
||
243 | */ |
||
244 | public function setUnserializeUndeclaredPropertyMode($value) |
||
257 | |||
258 | /** |
||
259 | * Parse the data to be json encoded |
||
260 | * |
||
261 | * @param mixed $value |
||
262 | * @return mixed |
||
263 | * @throws JsonSerializerException |
||
264 | */ |
||
265 | protected function serializeData($value) |
||
292 | |||
293 | /** |
||
294 | * Extract the data from an object |
||
295 | * |
||
296 | * @param object $value |
||
297 | * @return array |
||
298 | */ |
||
299 | protected function serializeObject($value) |
||
329 | |||
330 | /** |
||
331 | * Return the list of properties to be serialized |
||
332 | * |
||
333 | * @param ReflectionClass $ref |
||
334 | * @param object $value |
||
335 | * @return array |
||
336 | */ |
||
337 | protected function getObjectProperties($ref, $value) |
||
349 | |||
350 | /** |
||
351 | * Extract the object data |
||
352 | * |
||
353 | * @param object $value |
||
354 | * @param ReflectionClass $ref |
||
355 | * @param array $properties |
||
356 | * @return array |
||
357 | */ |
||
358 | protected function extractObjectData($value, $ref, $properties) |
||
372 | |||
373 | /** |
||
374 | * Parse the json decode to convert to objects again |
||
375 | * |
||
376 | * @param mixed $value |
||
377 | * @return mixed |
||
378 | */ |
||
379 | protected function unserializeData($value) |
||
398 | |||
399 | /** |
||
400 | * @param mixed $serializedData |
||
401 | * |
||
402 | * @return mixed |
||
403 | */ |
||
404 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
440 | |||
441 | /** |
||
442 | * Convert the serialized array into an object |
||
443 | * |
||
444 | * @param array $value |
||
445 | * @return object |
||
446 | * @throws JsonSerializerException |
||
447 | */ |
||
448 | protected function unserializeObject($value) |
||
515 | |||
516 | /** |
||
517 | * @return boolean |
||
518 | */ |
||
519 | protected function isSplList($className) |
||
523 | |||
524 | protected function restoreUsingUnserialize($className, $attributes) |
||
530 | |||
531 | /** |
||
532 | * Reset variables |
||
533 | * |
||
534 | * @return void |
||
535 | */ |
||
536 | protected function reset() |
||
542 | } |
||
543 |
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.