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 | * An array of available constants by class |
||
| 34 | * |
||
| 35 | * @var array ["$class" => ["$name" => $value, ...], ...] |
||
| 36 | */ |
||
| 37 | private static $constants = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Already instantiated enumerators |
||
| 41 | * |
||
| 42 | * @var array ["$class" => ["$name" => $instance, ...], ...] |
||
| 43 | */ |
||
| 44 | private static $instances = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * |
||
| 49 | * @param null|bool|int|float|string $value The value of the enumerator |
||
| 50 | * @param int|null $ordinal The ordinal number of the enumerator |
||
| 51 | */ |
||
| 52 | 16 | final private function __construct($value, $ordinal = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Get the name of the enumerator |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | * @see getName() |
||
| 63 | */ |
||
| 64 | 1 | public function __toString() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @throws LogicException Enums are not cloneable |
||
| 71 | * because instances are implemented as singletons |
||
| 72 | */ |
||
| 73 | 1 | final private function __clone() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @throws LogicException Enums are not serializable |
||
| 80 | * because instances are implemented as singletons |
||
| 81 | */ |
||
| 82 | 1 | final public function __sleep() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @throws LogicException Enums are not serializable |
||
| 89 | * because instances are implemented as singletons |
||
| 90 | */ |
||
| 91 | 1 | final public function __wakeup() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Get the value of the enumerator |
||
| 98 | * |
||
| 99 | * @return null|bool|int|float|string |
||
| 100 | */ |
||
| 101 | 22 | final public function getValue() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get the name of the enumerator |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | 8 | final public function getName() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get the ordinal number of the enumerator |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | 44 | final public function getOrdinal() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Compare this enumerator against another and check if it's the same. |
||
| 143 | * |
||
| 144 | * @param mixed $enumerator |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 2 | final public function is($enumerator) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get an enumerator instance of the given value or instance |
||
| 160 | * |
||
| 161 | * @param static|null|bool|int|float|string $value |
||
| 162 | * @return static |
||
| 163 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
| 164 | * @throws LogicException On ambiguous constant values |
||
| 165 | */ |
||
| 166 | 67 | final public static function get($value) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Get an enumerator instance by the given value |
||
| 177 | * |
||
| 178 | * @param mixed $value |
||
| 179 | * @return static |
||
| 180 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
| 181 | * @throws LogicException On ambiguous constant values |
||
| 182 | */ |
||
| 183 | 48 | final public static function byValue($value) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get an enumerator instance by the given name |
||
| 204 | * |
||
| 205 | * @param string $name The name of the enumerator |
||
| 206 | * @return static |
||
| 207 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 208 | * @throws LogicException On ambiguous values |
||
| 209 | */ |
||
| 210 | 37 | final public static function byName($name) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get an enumeration instance by the given ordinal number |
||
| 228 | * |
||
| 229 | * @param int $ordinal The ordinal number or the enumerator |
||
| 230 | * @return static |
||
| 231 | * @throws InvalidArgumentException On an invalid ordinal number |
||
| 232 | * @throws LogicException On ambiguous values |
||
| 233 | */ |
||
| 234 | 19 | final public static function byOrdinal($ordinal) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get an enumerator instance by the given name |
||
| 257 | * |
||
| 258 | * @param string $name The name of the enumerator |
||
| 259 | * @return static |
||
| 260 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 261 | * @throws LogicException On ambiguous values |
||
| 262 | * @deprecated |
||
| 263 | */ |
||
| 264 | final public static function getByName($name) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get an enumeration instance by the given ordinal number |
||
| 271 | * |
||
| 272 | * @param int $ordinal The ordinal number or the enumerator |
||
| 273 | * @return static |
||
| 274 | * @throws InvalidArgumentException On an invalid ordinal number |
||
| 275 | * @throws LogicException On ambiguous values |
||
| 276 | * @deprecated |
||
| 277 | */ |
||
| 278 | final public static function getByOrdinal($ordinal) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Clear all instantiated enumerators of the called class |
||
| 285 | * |
||
| 286 | * NOTE: This can break singleton behavior ... use it with caution! |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | * @deprecated |
||
| 290 | */ |
||
| 291 | final public static function clear() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get a list of enumerator instances ordered by ordinal number |
||
| 299 | * |
||
| 300 | * @return static[] |
||
| 301 | */ |
||
| 302 | 13 | final public static function getEnumerators() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Get a list of enumerator values ordered by ordinal number |
||
| 309 | * |
||
| 310 | * @return mixed[] |
||
| 311 | */ |
||
| 312 | 1 | final public static function getValues() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get a list of enumerator names ordered by ordinal number |
||
| 319 | * |
||
| 320 | * @return string[] |
||
| 321 | */ |
||
| 322 | 1 | final public static function getNames() |
|
| 326 | /* |
||
| 327 | * Get a list of enumerator ordinal numbers |
||
| 328 | * |
||
| 329 | * @return int[] |
||
| 330 | */ |
||
| 331 | 1 | final public static function getOrdinals() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Get all available constants of the called class |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | * @throws LogicException On ambiguous constant values |
||
| 342 | */ |
||
| 343 | 64 | final public static function getConstants() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Is the given enumerator part of this enumeration |
||
| 350 | * |
||
| 351 | * @param static|null|bool|int|float|string $value |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | 1 | final public static function has($value) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Detect all available constants by the given class |
||
| 368 | * |
||
| 369 | * @param string $class |
||
| 370 | * @return array |
||
| 371 | * @throws LogicException On ambiguous constant values |
||
| 372 | */ |
||
| 373 | 84 | private static function detectConstants($class) |
|
| 374 | { |
||
| 375 | 84 | if (!isset(self::$constants[$class])) { |
|
| 376 | 15 | $reflection = new ReflectionClass($class); |
|
| 377 | 15 | $constants = array(); |
|
| 378 | |||
| 379 | do { |
||
| 380 | 15 | $scopeConstants = array(); |
|
| 381 | 15 | if (PHP_VERSION_ID >= 70100) { |
|
| 382 | // Since PHP-7.1 visibility modifiers are allowed for class constants |
||
| 383 | // for enumerations we are only interested in public once. |
||
| 384 | 15 | foreach ($reflection->getReflectionConstants() as $reflConstant) { |
|
| 385 | 14 | if ($reflConstant->isPublic()) { |
|
| 386 | 15 | $scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
|
| 387 | } |
||
| 388 | } |
||
| 389 | } else { |
||
| 390 | // In PHP < 7.1 all class constants were public by definition |
||
| 391 | $scopeConstants = $reflection->getConstants(); |
||
| 392 | } |
||
| 393 | |||
| 394 | 15 | $constants = $scopeConstants + $constants; |
|
| 395 | 15 | } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
| 396 | |||
| 397 | // Detect ambiguous values and report names |
||
| 398 | 15 | $ambiguous = array(); |
|
| 399 | 15 | foreach ($constants as $value) { |
|
| 400 | 14 | $names = array_keys($constants, $value, true); |
|
| 401 | 14 | if (count($names) > 1) { |
|
| 402 | 2 | $ambiguous[var_export($value, true)] = $names; |
|
| 403 | } |
||
| 404 | } |
||
| 405 | 15 | if (!empty($ambiguous)) { |
|
| 406 | 2 | throw new LogicException( |
|
| 407 | 'All possible values needs to be unique. The following are ambiguous: ' |
||
| 408 | 2 | . implode(', ', array_map(function ($names) use ($constants) { |
|
| 409 | 2 | return implode('/', $names) . '=' . var_export($constants[$names[0]], true); |
|
| 410 | 2 | }, $ambiguous)) |
|
| 411 | ); |
||
| 412 | } |
||
| 413 | |||
| 414 | 13 | self::$constants[$class] = $constants; |
|
| 415 | } |
||
| 416 | |||
| 417 | 82 | return self::$constants[$class]; |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get an enumerator instance by the given name. |
||
| 422 | * |
||
| 423 | * This will be called automatically on calling a method |
||
| 424 | * with the same name of a defined enumerator. |
||
| 425 | * |
||
| 426 | * @param string $method The name of the enumeraotr (called as method) |
||
| 427 | * @param array $args There should be no arguments |
||
| 428 | * @return static |
||
| 429 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 430 | * @throws LogicException On ambiguous constant values |
||
| 431 | */ |
||
| 432 | 22 | final public static function __callStatic($method, array $args) |
|
| 436 | } |
||
| 437 |