Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 16 | abstract class Enum |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The selected enumerator value |
||
| 20 | * |
||
| 21 | * @var null|bool|int|float|string |
||
| 22 | */ |
||
| 23 | private $value; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The ordinal number of the enumerator |
||
| 27 | * |
||
| 28 | * @var null|int |
||
| 29 | */ |
||
| 30 | private $ordinal; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * A map of enumerator names and values by enumeration class |
||
| 34 | * |
||
| 35 | * @var array ["$class" => ["$name" => $value, ...], ...] |
||
| 36 | */ |
||
| 37 | private static $constants = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * A List of of available enumerator names by enumeration class |
||
| 41 | * |
||
| 42 | * @var array ["$class" => ["$name0", ...], ...] |
||
| 43 | */ |
||
| 44 | private static $names = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Already instantiated enumerators |
||
| 48 | * |
||
| 49 | * @var array ["$class" => ["$name" => $instance, ...], ...] |
||
| 50 | */ |
||
| 51 | private static $instances = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor |
||
| 55 | * |
||
| 56 | * @param null|bool|int|float|string $value The value of the enumerator |
||
| 57 | * @param int|null $ordinal The ordinal number of the enumerator |
||
| 58 | */ |
||
| 59 | 14 | final private function __construct($value, $ordinal = null) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Get the name of the enumerator |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | * @see getName() |
||
| 70 | */ |
||
| 71 | 1 | public function __toString() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @throws LogicException Enums are not cloneable |
||
| 78 | * because instances are implemented as singletons |
||
| 79 | */ |
||
| 80 | 1 | final private function __clone() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @throws LogicException Enums are not serializable |
||
| 87 | * because instances are implemented as singletons |
||
| 88 | */ |
||
| 89 | 1 | final public function __sleep() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @throws LogicException Enums are not serializable |
||
| 96 | * because instances are implemented as singletons |
||
| 97 | */ |
||
| 98 | 1 | final public function __wakeup() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Get the value of the enumerator |
||
| 105 | * |
||
| 106 | * @return null|bool|int|float|string |
||
| 107 | */ |
||
| 108 | 21 | final public function getValue() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Get the name of the enumerator |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 7 | final public function getName() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Get the ordinal number of the enumerator |
||
| 129 | * |
||
| 130 | * @return int |
||
| 131 | */ |
||
| 132 | 40 | final public function getOrdinal() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Compare this enumerator against another and check if it's the same. |
||
| 154 | * |
||
| 155 | * @param mixed $enumerator |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 2 | final public function is($enumerator) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get an enumerator instance of the given value or instance |
||
| 171 | * |
||
| 172 | * @param static|null|bool|int|float|string $value |
||
| 173 | * @return static |
||
| 174 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
| 175 | * @throws LogicException On ambiguous constant values |
||
| 176 | */ |
||
| 177 | 61 | final public static function get($value) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get an enumerator instance by the given value |
||
| 188 | * |
||
| 189 | * @param mixed $value |
||
| 190 | * @return static |
||
| 191 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
| 192 | * @throws LogicException On ambiguous constant values |
||
| 193 | */ |
||
| 194 | 46 | final public static function byValue($value) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get an enumerator instance by the given name |
||
| 215 | * |
||
| 216 | * @param string $name The name of the enumerator |
||
| 217 | * @return static |
||
| 218 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 219 | * @throws LogicException On ambiguous values |
||
| 220 | */ |
||
| 221 | 32 | final public static function byName($name) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get an enumeration instance by the given ordinal number |
||
| 239 | * |
||
| 240 | * @param int $ordinal The ordinal number or the enumerator |
||
| 241 | * @return static |
||
| 242 | * @throws InvalidArgumentException On an invalid ordinal number |
||
| 243 | * @throws LogicException On ambiguous values |
||
| 244 | */ |
||
| 245 | 19 | final public static function byOrdinal($ordinal) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get a list of enumerator instances ordered by ordinal number |
||
| 272 | * |
||
| 273 | * @return static[] |
||
| 274 | */ |
||
| 275 | 12 | final public static function getEnumerators() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Get a list of enumerator values ordered by ordinal number |
||
| 285 | * |
||
| 286 | * @return mixed[] |
||
| 287 | */ |
||
| 288 | 1 | final public static function getValues() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get a list of enumerator names ordered by ordinal number |
||
| 295 | * |
||
| 296 | * @return string[] |
||
| 297 | */ |
||
| 298 | 1 | final public static function getNames() |
|
| 305 | /* |
||
| 306 | * Get a list of enumerator ordinal numbers |
||
| 307 | * |
||
| 308 | * @return int[] |
||
| 309 | */ |
||
| 310 | 1 | final public static function getOrdinals() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Get all available constants of the called class |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | * @throws LogicException On ambiguous constant values |
||
| 321 | */ |
||
| 322 | 63 | final public static function getConstants() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Is the given enumerator part of this enumeration |
||
| 329 | * |
||
| 330 | * @param static|null|bool|int|float|string $value |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | 1 | final public static function has($value) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Detect all public available constants of given enumeration class |
||
| 345 | * |
||
| 346 | * @param string $class |
||
| 347 | * @return array |
||
| 348 | * @throws LogicException On ambiguous constant values |
||
| 349 | */ |
||
| 350 | 80 | private static function detectConstants($class) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get an enumerator instance by the given name. |
||
| 400 | * |
||
| 401 | * This will be called automatically on calling a method |
||
| 402 | * with the same name of a defined enumerator. |
||
| 403 | * |
||
| 404 | * @param string $method The name of the enumeraotr (called as method) |
||
| 405 | * @param array $args There should be no arguments |
||
| 406 | * @return static |
||
| 407 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 408 | * @throws LogicException On ambiguous constant values |
||
| 409 | */ |
||
| 410 | 18 | final public static function __callStatic($method, array $args) |
|
| 414 | } |
||
| 415 |