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 |
||
| 14 | class Enum implements \Serializable, \ArrayAccess |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * base enum values |
||
| 18 | * |
||
| 19 | * @var array $values |
||
| 20 | */ |
||
| 21 | protected static $values = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * allow keys to be unset/deleted? |
||
| 25 | * cannot be over-ridden once defined in a class |
||
| 26 | * |
||
| 27 | * @var boolean |
||
| 28 | */ |
||
| 29 | protected static $delete = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * allow keys to be over-written when adding? |
||
| 33 | * cannot be over-ridden once defined in a class |
||
| 34 | * |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | protected static $overwrite = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * always capitalize enum keys? |
||
| 41 | * can be over-ridden once defined in a class |
||
| 42 | * with capitalize(boolean) method |
||
| 43 | * |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | protected static $capitalize = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * case-sensitive check when searching by key for a value? |
||
| 50 | * can be over-ridden once defined in a class |
||
| 51 | * with caseSensitive(boolean) method |
||
| 52 | * |
||
| 53 | * @var boolean |
||
| 54 | */ |
||
| 55 | protected static $caseSensitive = false; |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * initialize with array of additional values not existing already |
||
| 60 | * |
||
| 61 | * @param array static::$values |
||
| 62 | */ |
||
| 63 | public function __construct(array $newValues = []) |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Set case-sensitivity when searching |
||
| 72 | * |
||
| 73 | * @param boolean $bool |
||
| 74 | */ |
||
| 75 | 2 | public static function caseSensitive($bool = false) |
|
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Set capitalization for enum keys |
||
| 83 | * |
||
| 84 | * @param boolean $bool |
||
| 85 | */ |
||
| 86 | 2 | public static function capitalize($bool = false) |
|
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Clear all data - use with care! |
||
| 97 | * |
||
| 98 | * @param boolean $caseSensitive |
||
| 99 | * @param boolean $capitalize |
||
| 100 | */ |
||
| 101 | 1 | public static function reset($caseSensitive = false, $capitalize = false) |
|
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Remove an enum value - use with care! |
||
| 111 | * |
||
| 112 | */ |
||
| 113 | 1 | public static function delete($key) |
|
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * Make sure that the enum keys have no SPACEs |
||
| 131 | * Make sure the keys are strings |
||
| 132 | * Set the case according to the settings |
||
| 133 | * |
||
| 134 | * @param return array static::$values |
||
| 135 | */ |
||
| 136 | 2 | public static function fixKeys(): array |
|
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * add array of extra values not existing already |
||
| 162 | * |
||
| 163 | * @param array|string $newValues |
||
| 164 | * @param null|boolean $overwrite allow over-write of values? |
||
| 165 | * @return array static::$values |
||
| 166 | */ |
||
| 167 | 1 | public static function add($newValues, $overwrite = null): array |
|
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * get array of keys |
||
| 194 | * |
||
| 195 | * @return array keys of static::$values |
||
| 196 | */ |
||
| 197 | 1 | public static function keys(): array |
|
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * get key for the given value |
||
| 205 | * |
||
| 206 | * @param mixed $value |
||
| 207 | * @param null|bool $caseSensitive search is case sensitive? |
||
| 208 | * @return string|array key(s) |
||
|
|
|||
| 209 | */ |
||
| 210 | 1 | public static function key($value, $caseSensitive = null) |
|
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * count values |
||
| 244 | * |
||
| 245 | * @return int number of values |
||
| 246 | */ |
||
| 247 | 1 | public static function count(): int |
|
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * count values |
||
| 255 | * |
||
| 256 | * @return int number of values |
||
| 257 | */ |
||
| 258 | 1 | public static function sizeof(): int |
|
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * get existing values |
||
| 266 | * |
||
| 267 | * @return array static::$values |
||
| 268 | */ |
||
| 269 | 1 | public static function values(): array |
|
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * get value for the given key |
||
| 277 | * |
||
| 278 | * @param string $key |
||
| 279 | * @param null|bool $caseSensitive search is case sensitive? |
||
| 280 | * @return mixed static::$values[$key] |
||
| 281 | */ |
||
| 282 | 3 | public static function value($key, $caseSensitive = null) |
|
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * get value for the given key |
||
| 302 | * method allows getting value from an object with $object->key |
||
| 303 | * |
||
| 304 | * @return mixed static::$values[$key] |
||
| 305 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.get |
||
| 306 | */ |
||
| 307 | public function __get(string $key) { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * get value named the same as the method called |
||
| 314 | * method allows getting value from an object with $object->key() |
||
| 315 | * |
||
| 316 | * @param string $key the method called is used as the key |
||
| 317 | * @param array $args |
||
| 318 | * @return mixed static::$values[$key] |
||
| 319 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.call |
||
| 320 | */ |
||
| 321 | 1 | public function __call(string $key, array $args = []) { |
|
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * get value named the same as the method called statically |
||
| 328 | * method allows getting value from an object with $object::key() |
||
| 329 | * |
||
| 330 | * @param string $key the method called is used as the key |
||
| 331 | * @param array $args |
||
| 332 | * @return mixed static::$values[$key] |
||
| 333 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.callstatic |
||
| 334 | */ |
||
| 335 | public static function __callStatic(string $key, array $args = []) { |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * check if the given key exists via call to $object of isset($object->key) |
||
| 342 | * |
||
| 343 | * @param string $key |
||
| 344 | * @return boolean |
||
| 345 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.isset |
||
| 346 | */ |
||
| 347 | 1 | public function __isset($key) |
|
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * when called as a function this class will add new values and return the result |
||
| 356 | * |
||
| 357 | * @param array $newValues |
||
| 358 | * @return boolean |
||
| 359 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.invoke |
||
| 360 | */ |
||
| 361 | 1 | public function __invoke($newValues) |
|
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * return the values as a string |
||
| 369 | * method allows outputting values as a string |
||
| 370 | * |
||
| 371 | * @return string json_encode(static::$values) |
||
| 372 | * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
| 373 | */ |
||
| 374 | 1 | public function __toString(): string |
|
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * return the values as a string |
||
| 382 | * method allows outputting values as a string when called statically |
||
| 383 | * |
||
| 384 | * @return string json_encode(static::$values) |
||
| 385 | */ |
||
| 386 | public static function toString(): string |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * serialize enum values |
||
| 394 | * |
||
| 395 | * @return string enum values serialized |
||
| 396 | * @link http://php.net/manual/en/class.serializable.php |
||
| 397 | */ |
||
| 398 | 1 | public function serialize(): string |
|
| 402 | |||
| 403 | /** |
||
| 404 | * unserialize serialized values to object |
||
| 405 | * |
||
| 406 | * @return string enum values serialized |
||
| 407 | * @link http://php.net/manual/en/class.serializable.php |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | 1 | public function unserialize($data) { |
|
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * returned dump values as array |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public static function var_dump(): array |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * returned values when called with var_dump() |
||
| 440 | * |
||
| 441 | * @return array debug info |
||
| 442 | * @link http://php.net/manual/en/language.oop5.magic.php#object.debuginfo |
||
| 443 | */ |
||
| 444 | public function __debugInfo(): array |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Implement Array offsetSet |
||
| 451 | * |
||
| 452 | * @param string $key the key to set |
||
| 453 | * @param mixed $value the value to set |
||
| 454 | * @return array debug info |
||
| 455 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
| 456 | */ |
||
| 457 | public function offsetSet($key, $value) { |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Implement Array offsetExists |
||
| 468 | * |
||
| 469 | * @param string $key the key to set |
||
| 470 | * @param mixed $value the value to set |
||
| 471 | * @return array debug info |
||
| 472 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
| 473 | */ |
||
| 474 | 1 | public function offsetExists($key) { |
|
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * Implement Array offsetUnset |
||
| 481 | * |
||
| 482 | * @param string $key the key to set |
||
| 483 | * @param mixed $value the value to set |
||
| 484 | * @return array debug info |
||
| 485 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
| 486 | */ |
||
| 487 | 1 | public function offsetUnset($key) { |
|
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * Implement Array offsetGet |
||
| 497 | * |
||
| 498 | * @param string $key the key to set |
||
| 499 | * @param mixed $value the value to set |
||
| 500 | * @return array debug info |
||
| 501 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
| 502 | */ |
||
| 503 | 1 | public function offsetGet($key) { |
|
| 506 | } |
||
| 507 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.