Complex classes like Serializer 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 Serializer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Serializer { |
||
6 | const TYPE_INT = 0; |
||
7 | const TYPE_DOUBLE = 1; |
||
8 | const TYPE_STRING = 2; |
||
9 | const TYPE_ARRAY_INDEX = 3; |
||
10 | const TYPE_ARRAY_KEY_VALUE = 4; |
||
11 | const TYPE_OBJECT_KEY_VALUE = 5; |
||
12 | const TYPE_SERIALIZABLE = ISerializable::TYPE; |
||
13 | const TYPE_RESOURCE = 7; |
||
14 | const TYPE_BOOLEAN = 7; |
||
15 | const TYPE_NULL = 8; |
||
16 | |||
17 | const DEFAULT_DEPTH = 255; |
||
18 | |||
19 | /** |
||
20 | * @param mixed $data |
||
21 | * @param integer $depth |
||
22 | * |
||
23 | * @return string |
||
24 | */ |
||
25 | 23 | public static function serialize($data, $depth = self::DEFAULT_DEPTH) { |
|
28 | |||
29 | /** |
||
30 | * @param mixed $text |
||
31 | * @param boolean $is_valid |
||
32 | * @param boolean $safe |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 28 | public static function unserialize($text, &$is_valid, $safe = true) { |
|
58 | |||
59 | /** |
||
60 | * @param mixed $data |
||
61 | * @param integer $depth |
||
62 | * |
||
63 | * @return Variant|object|boolean|null |
||
64 | */ |
||
65 | 23 | public static function serialize_to_variant($data, $depth = self::DEFAULT_DEPTH) { |
|
97 | |||
98 | protected static $_binary_codes = [ |
||
99 | 0, 1, 2, 3, 4, 5, 6, 7, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 129, 130, |
||
100 | 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, |
||
101 | 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, |
||
102 | 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, |
||
103 | 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, |
||
104 | 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, |
||
105 | 246, 247, 248, 249, 250, 251, 252, 253, 254, |
||
106 | ]; |
||
107 | |||
108 | protected static $_binary_codes_flip = null; |
||
109 | |||
110 | /** |
||
111 | * @param string $value |
||
112 | * |
||
113 | * @return boolean |
||
114 | */ |
||
115 | 19 | public static function is_binary_string($value) { |
|
130 | |||
131 | /** |
||
132 | * @param double $value |
||
133 | * |
||
134 | * @return Variant|object |
||
135 | */ |
||
136 | 8 | public static function serialize_double($value) { |
|
142 | |||
143 | /** |
||
144 | * @param integer $value |
||
145 | * |
||
146 | * @return Variant|object |
||
147 | */ |
||
148 | 4 | public static function serialize_int($value) { |
|
154 | |||
155 | /** |
||
156 | * @param array $value |
||
157 | * @param integer $depth |
||
158 | * |
||
159 | * @return Variant|object |
||
160 | */ |
||
161 | 4 | public static function serialize_indexed_array(array $value, $depth) { |
|
172 | |||
173 | /** |
||
174 | * @param array $value |
||
175 | * @param integer $depth |
||
176 | * |
||
177 | * @return Variant|object |
||
178 | */ |
||
179 | 2 | public static function serialize_array_key_value(array $value, $depth) { |
|
193 | |||
194 | /** |
||
195 | * @param \stdClass|object $value |
||
196 | * |
||
197 | * @return Variant|object |
||
198 | */ |
||
199 | 4 | public static function serialize_object_key_value($value) { |
|
214 | |||
215 | /** |
||
216 | * @param string $value |
||
217 | * |
||
218 | * @return Variant|object |
||
219 | */ |
||
220 | 5 | public static function serialize_string($value) { |
|
226 | |||
227 | /** |
||
228 | * @param array $input |
||
229 | * |
||
230 | * @return boolean |
||
231 | */ |
||
232 | 6 | public static function is_indexed_array(array $input) { |
|
246 | |||
247 | /** |
||
248 | * @param \stdClass|Variant $variant |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | 5 | public static function unserialize_string($variant) { |
|
255 | |||
256 | /** |
||
257 | * @param \stdClass|Variant $variant |
||
258 | * @param boolean $safe |
||
259 | * |
||
260 | * @return array |
||
261 | * @throws UnserializeException |
||
262 | */ |
||
263 | 4 | public static function unserialize_array_index($variant, $safe) { |
|
271 | |||
272 | /** |
||
273 | * @param \stdClass|KeyValueVariant $variant |
||
274 | * @param boolean $safe |
||
275 | * |
||
276 | * @return array |
||
277 | * @throws UnserializeException |
||
278 | */ |
||
279 | 6 | public static function unserialize_array_key_value($variant, $safe) { |
|
296 | |||
297 | /** |
||
298 | * @param SerializableVariant|boolean|null|object $variant |
||
299 | * @param boolean $safe |
||
300 | * |
||
301 | * @return mixed |
||
302 | * @throws UnserializeException |
||
303 | */ |
||
304 | 6 | public static function unserialize_object($variant, $safe) { |
|
326 | |||
327 | /** |
||
328 | * @param Variant|boolean|null|object $variant |
||
329 | * @param boolean $safe |
||
330 | * |
||
331 | * @return mixed |
||
332 | * @throws UnserializeException |
||
333 | */ |
||
334 | 26 | public static function unserialize_variant_to_value($variant, $safe) { |
|
360 | |||
361 | } |
||
362 | |||
363 | ?> |
||
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: