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 | 22 | 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 | 22 | public static function unserialize($text, &$is_valid, $safe = true) { |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param mixed $data |
||
| 55 | * @param integer $depth |
||
| 56 | * |
||
| 57 | * @return Variant|object|boolean|null |
||
| 58 | */ |
||
| 59 | 22 | public static function serialize_to_variant($data, $depth = self::DEFAULT_DEPTH) { |
|
| 93 | |||
| 94 | protected static $_binary_codes = [ |
||
| 95 | 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, |
||
| 96 | 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, |
||
| 97 | 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, |
||
| 98 | 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, |
||
| 99 | 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, |
||
| 100 | 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, |
||
| 101 | 246, 247, 248, 249, 250, 251, 252, 253, 254, |
||
| 102 | ]; |
||
| 103 | |||
| 104 | protected static $_binary_codes_flip = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $value |
||
| 108 | * |
||
| 109 | * @return boolean |
||
| 110 | */ |
||
| 111 | 18 | public static function is_binary_string($value) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param double $value |
||
| 129 | * |
||
| 130 | * @return Variant|object |
||
| 131 | */ |
||
| 132 | 8 | public static function serialize_double($value) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param integer $value |
||
| 141 | * |
||
| 142 | * @return Variant|object |
||
| 143 | */ |
||
| 144 | 4 | public static function serialize_int($value) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param array $value |
||
| 153 | * @param integer $depth |
||
| 154 | * |
||
| 155 | * @return Variant|object |
||
| 156 | */ |
||
| 157 | 3 | public static function serialize_indexed_array(array $value, $depth) { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $value |
||
| 171 | * @param integer $depth |
||
| 172 | * |
||
| 173 | * @return Variant|object |
||
| 174 | */ |
||
| 175 | 2 | public static function serialize_array_key_value(array $value, $depth) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param \stdClass|object $value |
||
| 192 | * |
||
| 193 | * @return Variant|object |
||
| 194 | */ |
||
| 195 | 4 | public static function serialize_object_key_value($value) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $value |
||
| 213 | * |
||
| 214 | * @return Variant|object |
||
| 215 | */ |
||
| 216 | 5 | public static function serialize_string($value) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param array $input |
||
| 225 | * |
||
| 226 | * @return boolean |
||
| 227 | */ |
||
| 228 | 5 | public static function is_indexed_array(array $input) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param \stdClass|Variant $variant |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | 5 | public static function unserialize_string($variant) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param \stdClass|Variant $variant |
||
| 254 | * @param boolean $safe |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | * @throws UnserializeException |
||
| 258 | */ |
||
| 259 | 3 | public static function unserialize_array_index($variant, $safe) { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param \stdClass|KeyValueVariant $variant |
||
| 270 | * @param boolean $safe |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | * @throws UnserializeException |
||
| 274 | */ |
||
| 275 | 6 | public static function unserialize_array_key_value($variant, $safe) { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param SerializableVariant|boolean|null|object $variant |
||
| 295 | * @param boolean $safe |
||
| 296 | * |
||
| 297 | * @return mixed |
||
| 298 | * @throws UnserializeException |
||
| 299 | */ |
||
| 300 | 3 | public static function unserialize_object($variant, $safe) { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param Variant|boolean|null|object $variant |
||
| 322 | * @param boolean $safe |
||
| 323 | * |
||
| 324 | * @return mixed |
||
| 325 | * @throws UnserializeException |
||
| 326 | */ |
||
| 327 | 22 | public static function unserialize_variant_to_value($variant, $safe) { |
|
| 353 | |||
| 354 | } |
||
| 355 | |||
| 356 | ?> |
||
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: