Complex classes like Enum 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 Enum, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Enum implements \Serializable |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * base enum values |
||
| 16 | * |
||
| 17 | * @var array $values |
||
| 18 | */ |
||
| 19 | protected static $values = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * allow keys to be unset/deleted? |
||
| 23 | * cannot be over-ridden once defined in a class |
||
| 24 | * |
||
| 25 | * @var boolean |
||
| 26 | */ |
||
| 27 | protected static $delete = false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * allow keys to be over-written when adding? |
||
| 31 | * cannot be over-ridden once defined in a class |
||
| 32 | * |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | protected static $overwrite = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * always capitalize enum keys? |
||
| 39 | * can be over-ridden once defined in a class |
||
| 40 | * with capitalize(boolean) method |
||
| 41 | * |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected static $capitalize = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * case-sensitive check when searching by key for a value? |
||
| 48 | * can be over-ridden once defined in a class |
||
| 49 | * with caseSensitive(boolean) method |
||
| 50 | * |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | protected static $caseSensitive = false; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * initialize with array of additional values not existing already |
||
| 58 | * |
||
| 59 | * @param array static::$values |
||
| 60 | */ |
||
| 61 | public function __construct(array $newValues = []) |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Set case-sensitivity when searching |
||
| 70 | * |
||
| 71 | * @param boolean $bool |
||
| 72 | */ |
||
| 73 | public static function caseSensitive($bool = false) |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Set capitalization for enum keys |
||
| 81 | * |
||
| 82 | * @param boolean $bool |
||
| 83 | */ |
||
| 84 | public static function capitalize($bool = false) |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Clear all data - use with care! |
||
| 95 | * |
||
| 96 | * @param boolean $caseSensitive |
||
| 97 | * @param boolean $capitalize |
||
| 98 | */ |
||
| 99 | public static function reset($caseSensitive = false, $capitalize = false) |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Remove an enum value - use with care! |
||
| 109 | * |
||
| 110 | */ |
||
| 111 | public static function delete($key) |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Make sure that the enum keys have no SPACEs |
||
| 129 | * Make sure the keys are strings |
||
| 130 | * Set the case according to the settings |
||
| 131 | * |
||
| 132 | * @param array $values |
||
| 133 | * @param return $values |
||
| 134 | */ |
||
| 135 | public static function fixKeys(array $values = []) |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * add array of extra values not existing already |
||
| 167 | * |
||
| 168 | * @param array|string $newValues |
||
| 169 | * @param null|boolean $overwrite allow over-write of values? |
||
| 170 | * @return array static::$values |
||
| 171 | */ |
||
| 172 | public static function add($newValues, $overwrite = null): array |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * get array of keys |
||
| 200 | * |
||
| 201 | * @return array keys of static::$values |
||
| 202 | */ |
||
| 203 | public static function keys(): array |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * get key for the given value |
||
| 211 | * |
||
| 212 | * @param mixed $value |
||
| 213 | * @param null|bool $caseSensitive search is case sensitive? |
||
| 214 | * @return string|array key(s) |
||
| 215 | */ |
||
| 216 | public static function key($value, $caseSensitive = null) |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * count values |
||
| 250 | * |
||
| 251 | * @return int number of values |
||
| 252 | */ |
||
| 253 | public static function count(): int |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * count values |
||
| 261 | * |
||
| 262 | * @return int number of values |
||
| 263 | */ |
||
| 264 | public static function sizeof(): int |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * get existing values |
||
| 272 | * |
||
| 273 | * @return array static::$values |
||
| 274 | */ |
||
| 275 | public static function values(): array |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * get value for the given key |
||
| 283 | * |
||
| 284 | * @param string $key |
||
| 285 | * @param null|bool $caseSensitive search is case sensitive? |
||
| 286 | * @return mixed static::$values[$key] |
||
| 287 | */ |
||
| 288 | public static function value($key, $caseSensitive = null) |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * get value for the given key |
||
| 308 | * method allows getting value from an object with $object->key |
||
| 309 | * |
||
| 310 | * @return mixed static::$values[$key] |
||
| 311 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.get |
||
| 312 | */ |
||
| 313 | public function __get(string $key) { |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * get value named the same as the method called |
||
| 320 | * method allows getting value from an object with $object->key() |
||
| 321 | * |
||
| 322 | * @param string $key the method called is used as the key |
||
| 323 | * @param array $args |
||
| 324 | * @return mixed static::$values[$key] |
||
| 325 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.call |
||
| 326 | */ |
||
| 327 | public function __call(string $key, array $args = []) { |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * get value named the same as the method called statically |
||
| 334 | * method allows getting value from an object with $object::key() |
||
| 335 | * |
||
| 336 | * @param string $key the method called is used as the key |
||
| 337 | * @param array $args |
||
| 338 | * @return mixed static::$values[$key] |
||
| 339 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.callstatic |
||
| 340 | */ |
||
| 341 | public static function __callStatic(string $key, array $args = []) { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * check if the given key exists via call to $object of isset($object->key) |
||
| 348 | * |
||
| 349 | * @param string $key |
||
| 350 | * @return boolean |
||
| 351 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.isset |
||
| 352 | */ |
||
| 353 | public function __isset($key) |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * when called as a function this class will add new values and return the result |
||
| 363 | * |
||
| 364 | * @param array $newValues |
||
| 365 | * @return boolean |
||
| 366 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.invoke |
||
| 367 | */ |
||
| 368 | public function __invoke($newValues) |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * return the values as a string |
||
| 376 | * method allows outputting values as a string |
||
| 377 | * |
||
| 378 | * @return string json_encode(static::$values) |
||
| 379 | * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
| 380 | */ |
||
| 381 | public function __toString(): string |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * return the values as a string |
||
| 389 | * method allows outputting values as a string when called statically |
||
| 390 | * |
||
| 391 | * @return string json_encode(static::$values) |
||
| 392 | */ |
||
| 393 | public static function toString(): string |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * serialize enum values |
||
| 401 | * |
||
| 402 | * @return string enum values serialized |
||
| 403 | * @link http://php.net/manual/en/class.serializable.php |
||
| 404 | */ |
||
| 405 | public function serialize(): string |
||
| 409 | |||
| 410 | /** |
||
| 411 | * unserialize serialized values to object |
||
| 412 | * |
||
| 413 | * @return string enum values serialized |
||
| 414 | * @link http://php.net/manual/en/class.serializable.php |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public function unserialize($data) { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * returned dump values as array |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public static function var_dump(): array |
||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * returned values when called with var_dump() |
||
| 447 | * |
||
| 448 | * @return array debug info |
||
| 449 | * @link http://php.net/manual/en/language.oop5.magic.php#object.debuginfo |
||
| 450 | */ |
||
| 451 | public function __debugInfo(): array |
||
| 455 | } |
||
| 456 |