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 FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
17 | |||
18 | /** |
||
19 | * Storage for object |
||
20 | * |
||
21 | * Used for recursion |
||
22 | * |
||
23 | * @var SplObjectStorage |
||
24 | */ |
||
25 | protected $objectStorage; |
||
26 | |||
27 | /** |
||
28 | * Object mapping for recursion |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $objectMapping = array(); |
||
33 | |||
34 | /** |
||
35 | * Object mapping index |
||
36 | * |
||
37 | * @var integer |
||
38 | */ |
||
39 | protected $objectMappingIndex = 0; |
||
40 | |||
41 | /** |
||
42 | * Support PRESERVE_ZERO_FRACTION json option |
||
43 | * |
||
44 | * @var boolean |
||
45 | */ |
||
46 | protected $preserveZeroFractionSupport; |
||
47 | |||
48 | /** |
||
49 | * Closure serializer instance |
||
50 | * |
||
51 | * @var SuperClosure\SerializerInterface |
||
52 | */ |
||
53 | protected $closureSerializer; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param SuperClosure\SerializerInterface $closureSerializer |
||
59 | */ |
||
60 | public function __construct(ClosureSerializerInterface $closureSerializer = null) |
||
65 | |||
66 | /** |
||
67 | * Serialize the value in JSON |
||
68 | * |
||
69 | * @param mixed $value |
||
70 | * @return string JSON encoded |
||
71 | * @throws Zumba\JsonSerializer\Exception\JsonSerializerException |
||
72 | */ |
||
73 | public function serialize($value) |
||
79 | |||
80 | /** |
||
81 | * Calculate encoding options |
||
82 | * |
||
83 | * @return integer |
||
84 | */ |
||
85 | protected function calculateEncodeOptions() |
||
93 | |||
94 | /** |
||
95 | * Execute post-encoding actions |
||
96 | * |
||
97 | * @param string $encoded |
||
98 | * @return string |
||
99 | */ |
||
100 | protected function processEncodedValue($encoded) |
||
107 | |||
108 | /** |
||
109 | * Unserialize the value from JSON |
||
110 | * |
||
111 | * @param string $value |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function unserialize($value) |
||
123 | |||
124 | /** |
||
125 | * Parse the data to be json encoded |
||
126 | * |
||
127 | * @param mixed $value |
||
128 | * @return mixed |
||
129 | * @throws Zumba\JsonSerializer\Exception\JsonSerializerException |
||
130 | */ |
||
131 | protected function serializeData($value) |
||
158 | |||
159 | /** |
||
160 | * Extract the data from an object |
||
161 | * |
||
162 | * @param object $value |
||
163 | * @return array |
||
164 | */ |
||
165 | protected function serializeObject($value) |
||
179 | |||
180 | /** |
||
181 | * Return the list of properties to be serialized |
||
182 | * |
||
183 | * @param ReflectionClass $ref |
||
184 | * @param object $value |
||
185 | * @return array |
||
186 | */ |
||
187 | protected function getObjectProperties($ref, $value) |
||
199 | |||
200 | /** |
||
201 | * Extract the object data |
||
202 | * |
||
203 | * @param object $value |
||
204 | * @param ReflectionClass $ref |
||
205 | * @param array $properties |
||
206 | * @return array |
||
207 | */ |
||
208 | protected function extractObjectData($value, $ref, $properties) |
||
222 | |||
223 | /** |
||
224 | * Parse the json decode to convert to objects again |
||
225 | * |
||
226 | * @param mixed $value |
||
227 | * @return mixed |
||
228 | */ |
||
229 | protected function unserializeData($value) |
||
248 | |||
249 | /** |
||
250 | * Convert the serialized array into an object |
||
251 | * |
||
252 | * @param aray $value |
||
253 | * @return object |
||
254 | * @throws Zumba\JsonSerializer\Exception\JsonSerializerException |
||
255 | */ |
||
256 | protected function unserializeObject($value) |
||
293 | |||
294 | protected function restoreUsingUnserialize($className, $attributes) |
||
300 | |||
301 | /** |
||
302 | * Reset variables |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | protected function reset() |
||
312 | } |
||
313 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.