| Total Complexity | 53 |
| Total Lines | 392 |
| Duplicated Lines | 1.53 % |
| Coverage | 97.74% |
| Changes | 0 | ||
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.
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 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 | 108 | final private function __construct($value, $ordinal = null) |
|
| 60 | { |
||
| 61 | 108 | $this->value = $value; |
|
| 62 | 108 | $this->ordinal = $ordinal; |
|
| 63 | 108 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Get the name of the enumerator |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | * @see getName() |
||
| 70 | */ |
||
| 71 | 3 | public function __toString() |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @throws LogicException Enums are not cloneable |
||
| 78 | * because instances are implemented as singletons |
||
| 79 | */ |
||
| 80 | 3 | final private function __clone() |
|
| 81 | { |
||
| 82 | 3 | throw new LogicException('Enums are not cloneable'); |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @throws LogicException Enums are not serializable |
||
| 87 | * because instances are implemented as singletons |
||
| 88 | */ |
||
| 89 | 3 | final public function __sleep() |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @throws LogicException Enums are not serializable |
||
| 96 | * because instances are implemented as singletons |
||
| 97 | */ |
||
| 98 | 3 | final public function __wakeup() |
|
| 99 | { |
||
| 100 | 3 | throw new LogicException('Enums are not serializable'); |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the value of the enumerator |
||
| 105 | * |
||
| 106 | * @return null|bool|int|float|string |
||
| 107 | */ |
||
| 108 | 69 | final public function getValue() |
|
| 109 | { |
||
| 110 | 69 | return $this->value; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get the name of the enumerator |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 24 | final public function getName() |
|
| 119 | { |
||
| 120 | 24 | $ordinal = $this->ordinal !== null ? $this->ordinal : $this->getOrdinal(); |
|
| 121 | 24 | return self::$names[static::class][$ordinal]; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the ordinal number of the enumerator |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | 189 | final public function getOrdinal() |
|
| 130 | { |
||
| 131 | 189 | if ($this->ordinal === null) { |
|
| 132 | 57 | $ordinal = 0; |
|
| 133 | 57 | $value = $this->value; |
|
| 134 | 57 | foreach (self::detectConstants(static::class) as $constValue) { |
|
| 135 | 57 | if ($value === $constValue) { |
|
| 136 | 57 | break; |
|
| 137 | } |
||
| 138 | 48 | ++$ordinal; |
|
| 139 | 19 | } |
|
| 140 | |||
| 141 | 57 | $this->ordinal = $ordinal; |
|
| 142 | 19 | } |
|
| 143 | |||
| 144 | 189 | return $this->ordinal; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Compare this enumerator against another and check if it's the same. |
||
| 149 | * |
||
| 150 | * @param mixed $enumerator |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 6 | final public function is($enumerator) |
|
| 154 | { |
||
| 155 | 6 | return $this === $enumerator || $this->value === $enumerator |
|
| 156 | |||
| 157 | // The following additional conditions are required only because of the issue of serializable singletons |
||
| 158 | 6 | || ($enumerator instanceof static |
|
| 159 | 6 | && \get_class($enumerator) === static::class |
|
| 160 | 6 | && $enumerator->value === $this->value |
|
| 161 | 2 | ); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get an enumerator instance of the given value or instance |
||
| 166 | * |
||
| 167 | * @param static|null|bool|int|float|string $value |
||
| 168 | * @return static |
||
| 169 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
| 170 | * @throws LogicException On ambiguous constant values |
||
| 171 | */ |
||
| 172 | 219 | final public static function get($value) |
|
| 173 | { |
||
| 174 | 219 | if ($value instanceof static && \get_class($value) === static::class) { |
|
| 175 | 84 | return $value; |
|
| 176 | } |
||
| 177 | |||
| 178 | 156 | return static::byValue($value); |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get an enumerator instance by the given value |
||
| 183 | * |
||
| 184 | * @param mixed $value |
||
| 185 | * @return static |
||
| 186 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
| 187 | * @throws LogicException On ambiguous constant values |
||
| 188 | */ |
||
| 189 | 156 | final public static function byValue($value) |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get an enumerator instance by the given name |
||
| 210 | * |
||
| 211 | * @param string $name The name of the enumerator |
||
| 212 | * @return static |
||
| 213 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 214 | * @throws LogicException On ambiguous values |
||
| 215 | */ |
||
| 216 | 132 | final public static function byName($name) |
|
| 217 | { |
||
| 218 | 132 | $name = (string) $name; |
|
| 219 | 132 | $class = static::class; |
|
| 220 | 132 | if (isset(self::$instances[$class][$name])) { |
|
| 221 | 96 | return self::$instances[$class][$name]; |
|
| 222 | } |
||
| 223 | |||
| 224 | 51 | $const = $class . '::' . $name; |
|
| 225 | 51 | if (!\defined($const)) { |
|
| 226 | 3 | throw new InvalidArgumentException($const . ' not defined'); |
|
| 227 | } |
||
| 228 | |||
| 229 | 48 | return self::$instances[$class][$name] = new $class(\constant($const)); |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get an enumeration instance by the given ordinal number |
||
| 234 | * |
||
| 235 | * @param int $ordinal The ordinal number or the enumerator |
||
| 236 | * @return static |
||
| 237 | * @throws InvalidArgumentException On an invalid ordinal number |
||
| 238 | * @throws LogicException On ambiguous values |
||
| 239 | */ |
||
| 240 | 90 | final public static function byOrdinal($ordinal) |
|
| 241 | { |
||
| 242 | 90 | $ordinal = (int) $ordinal; |
|
| 243 | 90 | $class = static::class; |
|
| 244 | |||
| 245 | 90 | if (!isset(self::$names[$class])) { |
|
| 246 | 6 | self::detectConstants($class); |
|
| 247 | 2 | } |
|
| 248 | |||
| 249 | 90 | if (!isset(self::$names[$class][$ordinal])) { |
|
| 250 | 3 | throw new InvalidArgumentException(\sprintf( |
|
| 251 | 3 | 'Invalid ordinal number, must between 0 and %s', |
|
| 252 | 3 | \count(self::$names[$class]) - 1 |
|
| 253 | 1 | )); |
|
| 254 | } |
||
| 255 | |||
| 256 | 87 | $name = self::$names[$class][$ordinal]; |
|
| 257 | 87 | if (isset(self::$instances[$class][$name])) { |
|
| 258 | 81 | return self::$instances[$class][$name]; |
|
| 259 | } |
||
| 260 | |||
| 261 | 9 | $const = $class . '::' . $name; |
|
| 262 | 9 | return self::$instances[$class][$name] = new $class(\constant($const), $ordinal); |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get a list of enumerator instances ordered by ordinal number |
||
| 267 | * |
||
| 268 | * @return static[] |
||
| 269 | */ |
||
| 270 | 48 | final public static function getEnumerators() |
|
| 271 | { |
||
| 272 | 48 | if (!isset(self::$names[static::class])) { |
|
| 273 | 3 | self::detectConstants(static::class); |
|
| 274 | 1 | } |
|
| 275 | 48 | return \array_map([static::class, 'byName'], self::$names[static::class]); |
|
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get a list of enumerator values ordered by ordinal number |
||
| 280 | * |
||
| 281 | * @return mixed[] |
||
| 282 | */ |
||
| 283 | 3 | final public static function getValues() |
|
| 284 | { |
||
| 285 | 3 | return \array_values(self::detectConstants(static::class)); |
|
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get a list of enumerator names ordered by ordinal number |
||
| 290 | * |
||
| 291 | * @return string[] |
||
| 292 | */ |
||
| 293 | 6 | final public static function getNames() |
|
| 299 | } |
||
| 300 | /* |
||
| 301 | * Get a list of enumerator ordinal numbers |
||
| 302 | * |
||
| 303 | * @return int[] |
||
| 304 | */ |
||
| 305 | 3 | final public static function getOrdinals() |
|
| 306 | { |
||
| 307 | 3 | $count = \count(self::detectConstants(static::class)); |
|
| 308 | 3 | return $count === 0 ? array() : \range(0, $count - 1); |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get all available constants of the called class |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | * @throws LogicException On ambiguous constant values |
||
| 316 | */ |
||
| 317 | 236 | final public static function getConstants() |
|
| 318 | { |
||
| 319 | 236 | return self::detectConstants(static::class); |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Is the given enumerator part of this enumeration |
||
| 324 | * |
||
| 325 | * @param static|null|bool|int|float|string $value |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | 3 | final public static function has($value) |
|
| 329 | { |
||
| 330 | 3 | if ($value instanceof static && \get_class($value) === static::class) { |
|
| 331 | 3 | return true; |
|
| 332 | } |
||
| 333 | |||
| 334 | 3 | $constants = self::detectConstants(static::class); |
|
| 335 | 3 | return \in_array($value, $constants, true); |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Detect all public available constants of given enumeration class |
||
| 340 | * |
||
| 341 | * @param string $class |
||
| 342 | * @return array |
||
| 343 | * @throws LogicException On ambiguous constant values |
||
| 344 | */ |
||
| 345 | 308 | private static function detectConstants($class) |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get an enumerator instance by the given name. |
||
| 395 | * |
||
| 396 | * This will be called automatically on calling a method |
||
| 397 | * with the same name of a defined enumerator. |
||
| 398 | * |
||
| 399 | * @param string $method The name of the enumeraotr (called as method) |
||
| 400 | * @param array $args There should be no arguments |
||
| 401 | * @return static |
||
| 402 | * @throws InvalidArgumentException On an invalid or unknown name |
||
| 403 | * @throws LogicException On ambiguous constant values |
||
| 404 | */ |
||
| 405 | 78 | final public static function __callStatic($method, array $args) |
|
| 408 | } |
||
| 409 | } |
||
| 410 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.