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 | const CLASS_IDENTIFIER_KEY = '@type'; |
||
14 | const CLOSURE_IDENTIFIER_KEY = '@closure'; |
||
15 | const FLOAT_ADAPTER = 'JsonSerializerFloatAdapter'; |
||
16 | |||
17 | /** |
||
18 | * Storage for object |
||
19 | * |
||
20 | * Used for recursion |
||
21 | * |
||
22 | * @var SplObjectStorage |
||
23 | */ |
||
24 | protected $objectStorage; |
||
25 | |||
26 | /** |
||
27 | * Object mapping for recursion |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $objectMapping = array(); |
||
32 | |||
33 | /** |
||
34 | * Object mapping index |
||
35 | * |
||
36 | * @var integer |
||
37 | */ |
||
38 | protected $objectMappingIndex = 0; |
||
39 | |||
40 | /** |
||
41 | * Support PRESERVE_ZERO_FRACTION json option |
||
42 | * |
||
43 | * @var boolean |
||
44 | */ |
||
45 | protected $preserveZeroFractionSupport; |
||
46 | |||
47 | /** |
||
48 | * Closure serializer instance |
||
49 | * |
||
50 | * @var SuperClosure\SerializerInterface |
||
51 | */ |
||
52 | protected $closureSerializer; |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | * |
||
57 | * @param SuperClosure\SerializerInterface $closureSerializer |
||
58 | */ |
||
59 | public function __construct(ClosureSerializerInterface $closureSerializer = null) { |
||
69 | |||
70 | /** |
||
71 | * Serialize the value in JSON |
||
72 | * |
||
73 | * @param mixed $value |
||
74 | * @return string JSON encoded |
||
75 | * @throws Zumba\Exception\JsonSerializerException |
||
76 | */ |
||
77 | public function serialize($value) { |
||
82 | |||
83 | /** |
||
84 | * Calculate encoding options |
||
85 | * |
||
86 | * @return integer |
||
87 | */ |
||
88 | protected function calculateEncodeOptions() { |
||
95 | |||
96 | /** |
||
97 | * Execute post-encoding actions |
||
98 | * |
||
99 | * @param string $encoded |
||
100 | * @return string |
||
101 | */ |
||
102 | protected function processEncodedValue($encoded) { |
||
108 | |||
109 | /** |
||
110 | * Unserialize the value from JSON |
||
111 | * |
||
112 | * @param string $value |
||
113 | * @return mixed |
||
114 | */ |
||
115 | public function unserialize($value) { |
||
119 | |||
120 | /** |
||
121 | * Parse the data to be json encoded |
||
122 | * |
||
123 | * @param mixed $value |
||
124 | * @return mixed |
||
125 | * @throws Zumba\Exception\JsonSerializerException |
||
126 | */ |
||
127 | protected function serializeData($value) { |
||
153 | |||
154 | /** |
||
155 | * Extract the data from an object |
||
156 | * |
||
157 | * @param object $value |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function serializeObject($value) { |
||
173 | |||
174 | /** |
||
175 | * Return the list of properties to be serialized |
||
176 | * |
||
177 | * @param ReflectionClass $ref |
||
178 | * @param object $value |
||
179 | * @return array |
||
180 | */ |
||
181 | protected function getObjectProperties($ref, $value) { |
||
192 | |||
193 | /** |
||
194 | * Extract the object data |
||
195 | * |
||
196 | * @param object $value |
||
197 | * @param ReflectionClass $ref |
||
198 | * @param array $properties |
||
199 | * @return array |
||
200 | */ |
||
201 | protected function extractObjectData($value, $ref, $properties) { |
||
214 | |||
215 | /** |
||
216 | * Parse the json decode to convert to objects again |
||
217 | * |
||
218 | * @param mixed $value |
||
219 | * @return mixed |
||
220 | */ |
||
221 | protected function unserializeData($value) { |
||
239 | |||
240 | /** |
||
241 | * Convert the serialized array into an object |
||
242 | * |
||
243 | * @param aray $value |
||
244 | * @return object |
||
245 | * @throws Zumba\Exception\JsonSerializerException |
||
246 | */ |
||
247 | protected function unserializeObject($value) { |
||
283 | |||
284 | protected function restoreUsingUnserialize($className, $attributes) { |
||
289 | |||
290 | /** |
||
291 | * Reset variables |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function reset() { |
||
300 | |||
301 | } |
||
302 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.