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 | 66 | 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 | 2 | public function __toString() |
|
75 | |||
76 | /** |
||
77 | * @throws LogicException Enums are not cloneable |
||
78 | * because instances are implemented as singletons |
||
79 | */ |
||
80 | 2 | final private function __clone() |
|
84 | |||
85 | /** |
||
86 | * @throws LogicException Enums are not serializable |
||
87 | * because instances are implemented as singletons |
||
88 | */ |
||
89 | 2 | final public function __sleep() |
|
93 | |||
94 | /** |
||
95 | * @throws LogicException Enums are not serializable |
||
96 | * because instances are implemented as singletons |
||
97 | */ |
||
98 | 2 | final public function __wakeup() |
|
102 | |||
103 | /** |
||
104 | * Get the value of the enumerator |
||
105 | * |
||
106 | * @return null|bool|int|float|string |
||
107 | */ |
||
108 | 44 | final public function getValue() |
|
112 | |||
113 | /** |
||
114 | * Get the name of the enumerator |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 16 | final public function getName() |
|
119 | { |
||
120 | 16 | if ($this->ordinal !== null) { |
|
121 | 6 | return self::$names[static::class][$this->ordinal]; |
|
122 | } |
||
123 | 10 | return array_search($this->value, self::detectConstants(static::class), true); |
|
|
|||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Get the ordinal number of the enumerator |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | 80 | final public function getOrdinal() |
|
150 | |||
151 | /** |
||
152 | * Compare this enumerator against another and check if it's the same. |
||
153 | * |
||
154 | * @param mixed $enumerator |
||
155 | * @return bool |
||
156 | */ |
||
157 | 4 | final public function is($enumerator) |
|
167 | |||
168 | /** |
||
169 | * Get an enumerator instance of the given value or instance |
||
170 | * |
||
171 | * @param static|null|bool|int|float|string $value |
||
172 | * @return static |
||
173 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
174 | * @throws LogicException On ambiguous constant values |
||
175 | */ |
||
176 | 122 | final public static function get($value) |
|
184 | |||
185 | /** |
||
186 | * Get an enumerator instance by the given value |
||
187 | * |
||
188 | * @param mixed $value |
||
189 | * @return static |
||
190 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
191 | * @throws LogicException On ambiguous constant values |
||
192 | */ |
||
193 | 92 | final public static function byValue($value) |
|
211 | |||
212 | /** |
||
213 | * Get an enumerator instance by the given name |
||
214 | * |
||
215 | * @param string $name The name of the enumerator |
||
216 | * @return static |
||
217 | * @throws InvalidArgumentException On an invalid or unknown name |
||
218 | * @throws LogicException On ambiguous values |
||
219 | */ |
||
220 | 66 | final public static function byName($name) |
|
235 | |||
236 | /** |
||
237 | * Get an enumeration instance by the given ordinal number |
||
238 | * |
||
239 | * @param int $ordinal The ordinal number or the enumerator |
||
240 | * @return static |
||
241 | * @throws InvalidArgumentException On an invalid ordinal number |
||
242 | * @throws LogicException On ambiguous values |
||
243 | */ |
||
244 | 38 | final public static function byOrdinal($ordinal) |
|
245 | { |
||
246 | 38 | $ordinal = (int) $ordinal; |
|
247 | 38 | $class = static::class; |
|
248 | |||
249 | 38 | if (!isset(self::$names[$class])) { |
|
250 | 4 | self::detectConstants($class); |
|
251 | 2 | } |
|
252 | |||
253 | 38 | if (!isset(self::$names[$class][$ordinal])) { |
|
254 | 2 | throw new InvalidArgumentException(sprintf( |
|
255 | 2 | 'Invalid ordinal number, must between 0 and %s', |
|
256 | 2 | count(self::$names[$class]) - 1 |
|
257 | 1 | )); |
|
258 | } |
||
259 | |||
260 | 36 | $name = self::$names[$class][$ordinal]; |
|
261 | 36 | if (isset(self::$instances[$class][$name])) { |
|
262 | 32 | return self::$instances[$class][$name]; |
|
263 | } |
||
264 | |||
265 | 6 | $const = $class . '::' . $name; |
|
266 | 6 | return self::$instances[$class][$name] = new $class(constant($const)); |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * Get a list of enumerator instances ordered by ordinal number |
||
271 | * |
||
272 | * @return static[] |
||
273 | */ |
||
274 | 26 | final public static function getEnumerators() |
|
275 | { |
||
276 | 26 | if (!isset(self::$names[static::class])) { |
|
277 | 2 | self::detectConstants(static::class); |
|
278 | 1 | } |
|
279 | 26 | return array_map([static::class, 'byName'], self::$names[static::class]); |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Get a list of enumerator values ordered by ordinal number |
||
284 | * |
||
285 | * @return mixed[] |
||
286 | */ |
||
287 | 2 | final public static function getValues() |
|
291 | |||
292 | /** |
||
293 | * Get a list of enumerator names ordered by ordinal number |
||
294 | * |
||
295 | * @return string[] |
||
296 | */ |
||
297 | 4 | final public static function getNames() |
|
298 | { |
||
299 | 4 | if (!isset(self::$names[static::class])) { |
|
300 | 2 | self::detectConstants(static::class); |
|
301 | 1 | } |
|
302 | 4 | return self::$names[static::class]; |
|
303 | } |
||
304 | /* |
||
305 | * Get a list of enumerator ordinal numbers |
||
306 | * |
||
307 | * @return int[] |
||
308 | */ |
||
309 | 2 | final public static function getOrdinals() |
|
314 | |||
315 | /** |
||
316 | * Get all available constants of the called class |
||
317 | * |
||
318 | * @return array |
||
319 | * @throws LogicException On ambiguous constant values |
||
320 | */ |
||
321 | 128 | final public static function getConstants() |
|
325 | |||
326 | /** |
||
327 | * Is the given enumerator part of this enumeration |
||
328 | * |
||
329 | * @param static|null|bool|int|float|string $value |
||
330 | * @return bool |
||
331 | */ |
||
332 | 2 | final public static function has($value) |
|
341 | |||
342 | /** |
||
343 | * Detect all public available constants of given enumeration class |
||
344 | * |
||
345 | * @param string $class |
||
346 | * @return array |
||
347 | * @throws LogicException On ambiguous constant values |
||
348 | */ |
||
349 | 166 | private static function detectConstants($class) |
|
350 | { |
||
351 | 166 | if (!isset(self::$constants[$class])) { |
|
352 | 68 | $reflection = new ReflectionClass($class); |
|
353 | 68 | $constants = array(); |
|
354 | |||
355 | do { |
||
356 | 68 | $scopeConstants = array(); |
|
357 | 68 | if (PHP_VERSION_ID >= 70100) { |
|
358 | // Since PHP-7.1 visibility modifiers are allowed for class constants |
||
359 | // for enumerations we are only interested in public once. |
||
360 | 35 | foreach ($reflection->getReflectionConstants() as $reflConstant) { |
|
361 | 34 | if ($reflConstant->isPublic()) { |
|
362 | 35 | $scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
|
363 | } |
||
364 | } |
||
365 | } else { |
||
366 | // In PHP < 7.1 all class constants were public by definition |
||
367 | 33 | $scopeConstants = $reflection->getConstants(); |
|
368 | } |
||
369 | |||
370 | 68 | $constants = $scopeConstants + $constants; |
|
371 | 68 | } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
372 | |||
373 | // Detect ambiguous values and report names |
||
374 | 68 | $ambiguous = array(); |
|
375 | 68 | foreach ($constants as $value) { |
|
376 | 66 | $names = array_keys($constants, $value, true); |
|
377 | 66 | if (count($names) > 1) { |
|
378 | 36 | $ambiguous[var_export($value, true)] = $names; |
|
379 | 2 | } |
|
380 | 33 | } |
|
381 | 68 | if (!empty($ambiguous)) { |
|
382 | 4 | throw new LogicException( |
|
383 | 'All possible values needs to be unique. The following are ambiguous: ' |
||
384 | 4 | . implode(', ', array_map(function ($names) use ($constants) { |
|
385 | 4 | return implode('/', $names) . '=' . var_export($constants[$names[0]], true); |
|
386 | 4 | }, $ambiguous)) |
|
387 | 2 | ); |
|
388 | } |
||
389 | |||
390 | 64 | self::$constants[$class] = $constants; |
|
391 | 64 | self::$names[$class] = array_keys($constants); |
|
392 | 31 | } |
|
393 | |||
394 | 162 | return self::$constants[$class]; |
|
395 | } |
||
396 | |||
397 | /** |
||
398 | * Get an enumerator instance by the given name. |
||
399 | * |
||
400 | * This will be called automatically on calling a method |
||
401 | * with the same name of a defined enumerator. |
||
402 | * |
||
403 | * @param string $method The name of the enumeraotr (called as method) |
||
404 | * @param array $args There should be no arguments |
||
405 | * @return static |
||
406 | * @throws InvalidArgumentException On an invalid or unknown name |
||
407 | * @throws LogicException On ambiguous constant values |
||
408 | */ |
||
409 | 36 | final public static function __callStatic($method, array $args) |
|
413 | } |
||
414 |