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( |
||
93 | |||
94 | /** |
||
95 | * Serialize the value in JSON |
||
96 | * |
||
97 | * @param mixed $value |
||
98 | * @return string JSON encoded |
||
99 | * @throws JsonSerializerException |
||
100 | */ |
||
101 | public function serialize($value) |
||
120 | |||
121 | /** |
||
122 | * Calculate encoding options |
||
123 | * |
||
124 | * @return integer |
||
125 | */ |
||
126 | protected function calculateEncodeOptions() |
||
134 | |||
135 | /** |
||
136 | * |
||
137 | * @param mixed $serializedData |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | protected function encodeNonUtf8ToUtf8($serializedData) |
||
184 | |||
185 | /** |
||
186 | * Execute post-encoding actions |
||
187 | * |
||
188 | * @param string $encoded |
||
189 | * @return string |
||
190 | */ |
||
191 | protected function processEncodedValue($encoded) |
||
198 | |||
199 | /** |
||
200 | * Unserialize the value from JSON |
||
201 | * |
||
202 | * @param string $value |
||
203 | * @return mixed |
||
204 | */ |
||
205 | public function unserialize($value) |
||
219 | |||
220 | /** |
||
221 | * Set unserialization mode for undeclared class properties |
||
222 | * |
||
223 | * @param integer $value One of the JsonSerializer::UNDECLARED_PROPERTY_MODE_* |
||
224 | * @return self |
||
225 | * @throws InvalidArgumentException When the value is not one of the UNDECLARED_PROPERTY_MODE_* options |
||
226 | */ |
||
227 | public function setUnserializeUndeclaredPropertyMode($value) |
||
240 | |||
241 | /** |
||
242 | * Parse the data to be json encoded |
||
243 | * |
||
244 | * @param mixed $value |
||
245 | * @return mixed |
||
246 | * @throws JsonSerializerException |
||
247 | */ |
||
248 | protected function serializeData($value) |
||
275 | |||
276 | /** |
||
277 | * Extract the data from an object |
||
278 | * |
||
279 | * @param object $value |
||
280 | * @return array |
||
281 | */ |
||
282 | protected function serializeObject($value) |
||
307 | |||
308 | /** |
||
309 | * Return the list of properties to be serialized |
||
310 | * |
||
311 | * @param ReflectionClass $ref |
||
312 | * @param object $value |
||
313 | * @return array |
||
314 | */ |
||
315 | protected function getObjectProperties($ref, $value) |
||
327 | |||
328 | /** |
||
329 | * Extract the object data |
||
330 | * |
||
331 | * @param object $value |
||
332 | * @param ReflectionClass $ref |
||
333 | * @param array $properties |
||
334 | * @return array |
||
335 | */ |
||
336 | protected function extractObjectData($value, $ref, $properties) |
||
350 | |||
351 | /** |
||
352 | * Parse the json decode to convert to objects again |
||
353 | * |
||
354 | * @param mixed $value |
||
355 | * @return mixed |
||
356 | */ |
||
357 | protected function unserializeData($value) |
||
376 | |||
377 | /** |
||
378 | * |
||
379 | * @param mixed $serializedData |
||
380 | * |
||
381 | * @return mixed |
||
382 | */ |
||
383 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
419 | |||
420 | /** |
||
421 | * Convert the serialized array into an object |
||
422 | * |
||
423 | * @param array $value |
||
424 | * @return object |
||
425 | * @throws JsonSerializerException |
||
426 | */ |
||
427 | protected function unserializeObject($value) |
||
490 | |||
491 | /** |
||
492 | * |
||
493 | * @return boolean |
||
494 | */ |
||
495 | protected function isSplList($className) |
||
499 | |||
500 | protected function restoreUsingUnserialize($className, $attributes) |
||
510 | |||
511 | /** |
||
512 | * Reset variables |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | protected function reset() |
||
522 | } |
||
523 |
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.