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 | * Constructor. |
||
81 | * |
||
82 | * @param ClosureSerializerInterface $closureSerializer |
||
83 | * @param array $customObjectSerializerMap |
||
84 | */ |
||
85 | public function __construct(ClosureSerializerInterface $closureSerializer = null, $customObjectSerializerMap = array()) |
||
91 | |||
92 | /** |
||
93 | * Serialize the value in JSON |
||
94 | * |
||
95 | * @param mixed $value |
||
96 | * @return string JSON encoded |
||
97 | * @throws JsonSerializerException |
||
98 | */ |
||
99 | public function serialize($value) |
||
118 | |||
119 | /** |
||
120 | * Calculate encoding options |
||
121 | * |
||
122 | * @return integer |
||
123 | */ |
||
124 | protected function calculateEncodeOptions() |
||
132 | |||
133 | /** |
||
134 | * @param mixed $serializedData |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
179 | |||
180 | /** |
||
181 | * Execute post-encoding actions |
||
182 | * |
||
183 | * @param string $encoded |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function processEncodedValue($encoded) |
||
193 | |||
194 | /** |
||
195 | * Unserialize the value from JSON |
||
196 | * |
||
197 | * @param string $value |
||
198 | * @return mixed |
||
199 | */ |
||
200 | public function unserialize($value) |
||
214 | |||
215 | /** |
||
216 | * Set unserialization mode for undeclared class properties |
||
217 | * |
||
218 | * @param integer $value One of the JsonSerializer::UNDECLARED_PROPERTY_MODE_* |
||
219 | * @return self |
||
220 | * @throws InvalidArgumentException When the value is not one of the UNDECLARED_PROPERTY_MODE_* options |
||
221 | */ |
||
222 | public function setUnserializeUndeclaredPropertyMode($value) |
||
235 | |||
236 | /** |
||
237 | * Parse the data to be json encoded |
||
238 | * |
||
239 | * @param mixed $value |
||
240 | * @return mixed |
||
241 | * @throws JsonSerializerException |
||
242 | */ |
||
243 | protected function serializeData($value) |
||
270 | |||
271 | /** |
||
272 | * Extract the data from an object |
||
273 | * |
||
274 | * @param object $value |
||
275 | * @return array |
||
276 | */ |
||
277 | protected function serializeObject($value) |
||
302 | |||
303 | /** |
||
304 | * Return the list of properties to be serialized |
||
305 | * |
||
306 | * @param ReflectionClass $ref |
||
307 | * @param object $value |
||
308 | * @return array |
||
309 | */ |
||
310 | protected function getObjectProperties($ref, $value) |
||
322 | |||
323 | /** |
||
324 | * Extract the object data |
||
325 | * |
||
326 | * @param object $value |
||
327 | * @param ReflectionClass $ref |
||
328 | * @param array $properties |
||
329 | * @return array |
||
330 | */ |
||
331 | protected function extractObjectData($value, $ref, $properties) |
||
345 | |||
346 | /** |
||
347 | * Parse the json decode to convert to objects again |
||
348 | * |
||
349 | * @param mixed $value |
||
350 | * @return mixed |
||
351 | */ |
||
352 | protected function unserializeData($value) |
||
371 | |||
372 | /** |
||
373 | * @param mixed $serializedData |
||
374 | * |
||
375 | * @return mixed |
||
376 | */ |
||
377 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
413 | |||
414 | /** |
||
415 | * Convert the serialized array into an object |
||
416 | * |
||
417 | * @param array $value |
||
418 | * @return object |
||
419 | * @throws JsonSerializerException |
||
420 | */ |
||
421 | protected function unserializeObject($value) |
||
484 | |||
485 | /** |
||
486 | * @return boolean |
||
487 | */ |
||
488 | protected function isSplList($className) |
||
492 | |||
493 | protected function restoreUsingUnserialize($className, $attributes) |
||
499 | |||
500 | /** |
||
501 | * Reset variables |
||
502 | * |
||
503 | * @return void |
||
504 | */ |
||
505 | protected function reset() |
||
511 | } |
||
512 |
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.