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 return $values |
||
|
|
|||
| 133 | */ |
||
| 134 | public static function fixKeys() |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * add array of extra values not existing already |
||
| 160 | * |
||
| 161 | * @param array|string $newValues |
||
| 162 | * @param null|boolean $overwrite allow over-write of values? |
||
| 163 | * @return array static::$values |
||
| 164 | */ |
||
| 165 | public static function add($newValues, $overwrite = null): array |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * get array of keys |
||
| 192 | * |
||
| 193 | * @return array keys of static::$values |
||
| 194 | */ |
||
| 195 | public static function keys(): array |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * get key for the given value |
||
| 203 | * |
||
| 204 | * @param mixed $value |
||
| 205 | * @param null|bool $caseSensitive search is case sensitive? |
||
| 206 | * @return string|array key(s) |
||
| 207 | */ |
||
| 208 | public static function key($value, $caseSensitive = null) |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * count values |
||
| 242 | * |
||
| 243 | * @return int number of values |
||
| 244 | */ |
||
| 245 | public static function count(): int |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * count values |
||
| 253 | * |
||
| 254 | * @return int number of values |
||
| 255 | */ |
||
| 256 | public static function sizeof(): int |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * get existing values |
||
| 264 | * |
||
| 265 | * @return array static::$values |
||
| 266 | */ |
||
| 267 | public static function values(): array |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * get value for the given key |
||
| 275 | * |
||
| 276 | * @param string $key |
||
| 277 | * @param null|bool $caseSensitive search is case sensitive? |
||
| 278 | * @return mixed static::$values[$key] |
||
| 279 | */ |
||
| 280 | public static function value($key, $caseSensitive = null) |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * get value for the given key |
||
| 300 | * method allows getting value from an object with $object->key |
||
| 301 | * |
||
| 302 | * @return mixed static::$values[$key] |
||
| 303 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.get |
||
| 304 | */ |
||
| 305 | public function __get(string $key) { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * get value named the same as the method called |
||
| 312 | * method allows getting value from an object with $object->key() |
||
| 313 | * |
||
| 314 | * @param string $key the method called is used as the key |
||
| 315 | * @param array $args |
||
| 316 | * @return mixed static::$values[$key] |
||
| 317 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.call |
||
| 318 | */ |
||
| 319 | public function __call(string $key, array $args = []) { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * get value named the same as the method called statically |
||
| 326 | * method allows getting value from an object with $object::key() |
||
| 327 | * |
||
| 328 | * @param string $key the method called is used as the key |
||
| 329 | * @param array $args |
||
| 330 | * @return mixed static::$values[$key] |
||
| 331 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.callstatic |
||
| 332 | */ |
||
| 333 | public static function __callStatic(string $key, array $args = []) { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * check if the given key exists via call to $object of isset($object->key) |
||
| 340 | * |
||
| 341 | * @param string $key |
||
| 342 | * @return boolean |
||
| 343 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.isset |
||
| 344 | */ |
||
| 345 | public function __isset($key) |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * when called as a function this class will add new values and return the result |
||
| 354 | * |
||
| 355 | * @param array $newValues |
||
| 356 | * @return boolean |
||
| 357 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.invoke |
||
| 358 | */ |
||
| 359 | public function __invoke($newValues) |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * return the values as a string |
||
| 367 | * method allows outputting values as a string |
||
| 368 | * |
||
| 369 | * @return string json_encode(static::$values) |
||
| 370 | * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
| 371 | */ |
||
| 372 | public function __toString(): string |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * return the values as a string |
||
| 380 | * method allows outputting values as a string when called statically |
||
| 381 | * |
||
| 382 | * @return string json_encode(static::$values) |
||
| 383 | */ |
||
| 384 | public static function toString(): string |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * serialize enum values |
||
| 392 | * |
||
| 393 | * @return string enum values serialized |
||
| 394 | * @link http://php.net/manual/en/class.serializable.php |
||
| 395 | */ |
||
| 396 | public function serialize(): string |
||
| 400 | |||
| 401 | /** |
||
| 402 | * unserialize serialized values to object |
||
| 403 | * |
||
| 404 | * @return string enum values serialized |
||
| 405 | * @link http://php.net/manual/en/class.serializable.php |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | public function unserialize($data) { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * returned dump values as array |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public static function var_dump(): array |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * returned values when called with var_dump() |
||
| 438 | * |
||
| 439 | * @return array debug info |
||
| 440 | * @link http://php.net/manual/en/language.oop5.magic.php#object.debuginfo |
||
| 441 | */ |
||
| 442 | public function __debugInfo(): array |
||
| 446 | } |
||
| 447 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.