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 | 32 | 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) |
|
| 148 | { |
||
| 149 | 2 | return $this === $enumerator || $this->value === $enumerator |
|
| 150 | |||
| 151 | // The following additional conditions are required only because of the issue of serializable singletons |
||
| 152 | 2 | || ($enumerator instanceof static |
|
| 153 | 2 | && get_class($enumerator) === get_called_class() |
|
| 154 | 2 | && $enumerator->value === $this->value |
|
| 155 | ); |
||
| 156 | } |
||
| 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) |
|
| 167 | { |
||
| 168 | 67 | if ($value instanceof static && get_class($value) === get_called_class()) { |
|
| 169 | 26 | return $value; |
|
| 170 | } |
||
| 171 | |||
| 172 | 48 | $class = get_called_class(); |
|
| 173 | 48 | $constants = self::detectConstants($class); |
|
| 174 | 46 | $name = array_search($value, $constants, true); |
|
| 175 | 46 | View Code Duplication | if ($name === false) { |
| 176 | 7 | $message = is_scalar($value) |
|
| 177 | 3 | ? 'Unknown value ' . var_export($value, true) |
|
| 178 | 7 | : 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value)); |
|
| 179 | 7 | throw new InvalidArgumentException($message); |
|
| 180 | } |
||
| 181 | |||
| 182 | 40 | if (!isset(self::$instances[$class][$name])) { |
|
| 183 | 16 | self::$instances[$class][$name] = new $class($constants[$name]); |
|
| 184 | } |
||
| 185 | |||
| 186 | 40 | return self::$instances[$class][$name]; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get an enumerator instance by the given name |
||
| 191 | * |
||
| 192 | * @param string $name The name of the enumerator |
||
| 193 | * @return static |
||
| 194 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 195 | * @throws LogicException On ambiguous values |
||
| 196 | */ |
||
| 197 | 37 | final public static function getByName($name) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get an enumeration instance by the given ordinal number |
||
| 215 | * |
||
| 216 | * @param int $ordinal The ordinal number or the enumerator |
||
| 217 | * @return static |
||
| 218 | * @throws InvalidArgumentException On an invalid ordinal number |
||
| 219 | * @throws LogicException On ambiguous values |
||
| 220 | */ |
||
| 221 | 19 | final public static function getByOrdinal($ordinal) |
|
| 222 | { |
||
| 223 | 19 | $ordinal = (int) $ordinal; |
|
| 224 | 19 | $class = get_called_class(); |
|
| 225 | 19 | $constants = self::detectConstants($class); |
|
| 226 | 19 | $item = array_slice($constants, $ordinal, 1, true); |
|
| 227 | 19 | if (empty($item)) { |
|
| 228 | 1 | throw new InvalidArgumentException(sprintf( |
|
| 229 | 1 | 'Invalid ordinal number, must between 0 and %s', |
|
| 230 | 1 | count($constants) - 1 |
|
| 231 | )); |
||
| 232 | } |
||
| 233 | |||
| 234 | 18 | $name = key($item); |
|
| 235 | 18 | if (isset(self::$instances[$class][$name])) { |
|
| 236 | 16 | return self::$instances[$class][$name]; |
|
| 237 | } |
||
| 238 | |||
| 239 | 3 | return self::$instances[$class][$name] = new $class(current($item), $ordinal); |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Clear all instantiated enumerators of the called class |
||
| 244 | * |
||
| 245 | * NOTE: This can break singleton behavior ... use it with caution! |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | 32 | final public static function clear() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get a list of enumerator instances ordered by ordinal number |
||
| 257 | * |
||
| 258 | * @return static[] |
||
| 259 | */ |
||
| 260 | 13 | final public static function getEnumerators() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get all available constants of the called class |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | * @throws LogicException On ambiguous constant values |
||
| 270 | */ |
||
| 271 | 59 | final public static function getConstants() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Is the given enumerator part of this enumeration |
||
| 278 | * |
||
| 279 | * @param static|null|bool|int|float|string $value |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 1 | final public static function has($value) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Detect all available constants by the given class |
||
| 296 | * |
||
| 297 | * @param string $class |
||
| 298 | * @return array |
||
| 299 | * @throws LogicException On ambiguous constant values |
||
| 300 | */ |
||
| 301 | 79 | private static function detectConstants($class) |
|
| 302 | { |
||
| 303 | 79 | if (!isset(self::$constants[$class])) { |
|
| 304 | 29 | $reflection = new ReflectionClass($class); |
|
| 305 | 29 | $constants = array(); |
|
| 306 | |||
| 307 | do { |
||
| 308 | 29 | $scopeConstants = array(); |
|
| 309 | 29 | if (PHP_VERSION_ID >= 70100) { |
|
| 310 | // Since PHP-7.1 visibility modifiers are allowed for class constants |
||
| 311 | // for enumerations we are only interested in public once. |
||
| 312 | foreach ($reflection->getReflectionConstants() as $reflConstant) { |
||
| 313 | if ($reflConstant->isPublic()) { |
||
| 314 | $scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } else { |
||
| 318 | // In PHP < 7.1 all class constants were public by definition |
||
| 319 | 29 | $scopeConstants = $reflection->getConstants(); |
|
| 320 | } |
||
| 321 | |||
| 322 | 29 | $constants = $scopeConstants + $constants; |
|
| 323 | 29 | } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
| 324 | |||
| 325 | // Detect ambiguous values and report names |
||
| 326 | 29 | $ambiguous = array(); |
|
| 327 | 29 | foreach ($constants as $value) { |
|
| 328 | 28 | $names = array_keys($constants, $value, true); |
|
| 329 | 28 | if (count($names) > 1) { |
|
| 330 | 28 | $ambiguous[var_export($value, true)] = $names; |
|
| 331 | } |
||
| 332 | } |
||
| 333 | 29 | if (!empty($ambiguous)) { |
|
| 334 | 2 | throw new LogicException( |
|
| 335 | 'All possible values needs to be unique. The following are ambiguous: ' |
||
| 336 | 2 | . implode(', ', array_map(function ($names) use ($constants) { |
|
| 337 | 2 | return implode('/', $names) . '=' . var_export($constants[$names[0]], true); |
|
| 338 | 2 | }, $ambiguous)) |
|
| 339 | ); |
||
| 340 | } |
||
| 341 | |||
| 342 | 27 | self::$constants[$class] = $constants; |
|
| 343 | } |
||
| 344 | |||
| 345 | 77 | return self::$constants[$class]; |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get an enumerator instance by the given name. |
||
| 350 | * |
||
| 351 | * This will be called automatically on calling a method |
||
| 352 | * with the same name of a defined enumerator. |
||
| 353 | * |
||
| 354 | * @param string $method The name of the enumeraotr (called as method) |
||
| 355 | * @param array $args There should be no arguments |
||
| 356 | * @return static |
||
| 357 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 358 | * @throws LogicException On ambiguous constant values |
||
| 359 | */ |
||
| 360 | 22 | final public static function __callStatic($method, array $args) |
|
| 364 | } |
||
| 365 |