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) |
||
188 | |||
189 | /** |
||
190 | * Parse the data to be json encoded |
||
191 | * |
||
192 | * @param mixed $value |
||
193 | * @return mixed |
||
194 | * @throws JsonSerializerException |
||
195 | */ |
||
196 | protected function serializeData($value) |
||
223 | |||
224 | /** |
||
225 | * Extract the data from an object |
||
226 | * |
||
227 | * @param object $value |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function serializeObject($value) |
||
244 | |||
245 | /** |
||
246 | * Return the list of properties to be serialized |
||
247 | * |
||
248 | * @param ReflectionClass $ref |
||
249 | * @param object $value |
||
250 | * @return array |
||
251 | */ |
||
252 | protected function getObjectProperties($ref, $value) |
||
264 | |||
265 | /** |
||
266 | * Extract the object data |
||
267 | * |
||
268 | * @param object $value |
||
269 | * @param ReflectionClass $ref |
||
270 | * @param array $properties |
||
271 | * @return array |
||
272 | */ |
||
273 | protected function extractObjectData($value, $ref, $properties) |
||
287 | |||
288 | /** |
||
289 | * Parse the json decode to convert to objects again |
||
290 | * |
||
291 | * @param mixed $value |
||
292 | * @return mixed |
||
293 | */ |
||
294 | protected function unserializeData($value) |
||
315 | |||
316 | /** |
||
317 | * @param mixed $serializedData |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | protected function decodeNonUtf8FromUtf8($serializedData) |
||
357 | |||
358 | /** |
||
359 | * Convert the serialized array into an object |
||
360 | * |
||
361 | * @param array $value |
||
362 | * @return object |
||
363 | * @throws JsonSerializerException |
||
364 | */ |
||
365 | protected function unserializeObject($value) |
||
402 | |||
403 | protected function restoreUsingUnserialize($className, $attributes) |
||
409 | |||
410 | /** |
||
411 | * Reset variables |
||
412 | * |
||
413 | * @return void |
||
414 | */ |
||
415 | protected function reset() |
||
421 | } |
||
422 |
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.