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 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 | 72 | 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 | 46 | final public function getValue() |
|
112 | |||
113 | /** |
||
114 | * Get the name of the enumerator |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 16 | final public function getName() |
|
123 | |||
124 | /** |
||
125 | * Get the ordinal number of the enumerator |
||
126 | * |
||
127 | * @return int |
||
128 | */ |
||
129 | 124 | final public function getOrdinal() |
|
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 | 4 | final public function is($enumerator) |
|
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 | 144 | final public static function get($value) |
|
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 | 102 | final public static function byValue($value) |
|
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 | 88 | final public static function byName($name) |
|
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 | 60 | final public static function byOrdinal($ordinal) |
|
264 | |||
265 | /** |
||
266 | * Get a list of enumerator instances ordered by ordinal number |
||
267 | * |
||
268 | * @return static[] |
||
269 | */ |
||
270 | 32 | final public static function getEnumerators() |
|
277 | |||
278 | /** |
||
279 | * Get a list of enumerator values ordered by ordinal number |
||
280 | * |
||
281 | * @return mixed[] |
||
282 | */ |
||
283 | 2 | final public static function getValues() |
|
287 | |||
288 | /** |
||
289 | * Get a list of enumerator names ordered by ordinal number |
||
290 | * |
||
291 | * @return string[] |
||
292 | */ |
||
293 | 4 | final public static function getNames() |
|
300 | /* |
||
301 | * Get a list of enumerator ordinal numbers |
||
302 | * |
||
303 | * @return int[] |
||
304 | */ |
||
305 | 2 | final public static function getOrdinals() |
|
310 | |||
311 | /** |
||
312 | * Get all available constants of the called class |
||
313 | * |
||
314 | * @return array |
||
315 | * @throws LogicException On ambiguous constant values |
||
316 | */ |
||
317 | 156 | final public static function getConstants() |
|
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 | 2 | final public static function has($value) |
|
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 | 202 | private static function detectConstants($class) |
|
346 | { |
||
347 | 202 | if (!isset(self::$constants[$class])) { |
|
348 | 70 | $reflection = new ReflectionClass($class); |
|
349 | 70 | $constants = array(); |
|
350 | |||
351 | do { |
||
352 | 70 | $scopeConstants = array(); |
|
353 | 70 | if (PHP_VERSION_ID >= 70100) { |
|
354 | // Since PHP-7.1 visibility modifiers are allowed for class constants |
||
355 | // for enumerations we are only interested in public once. |
||
356 | foreach ($reflection->getReflectionConstants() as $reflConstant) { |
||
357 | if ($reflConstant->isPublic()) { |
||
358 | $scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
||
359 | } |
||
360 | } |
||
361 | } else { |
||
362 | // In PHP < 7.1 all class constants were public by definition |
||
363 | 70 | $scopeConstants = $reflection->getConstants(); |
|
364 | } |
||
365 | |||
366 | 70 | $constants = $scopeConstants + $constants; |
|
367 | 70 | } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
368 | |||
369 | // Detect ambiguous values and report names |
||
370 | 70 | $ambiguous = array(); |
|
371 | 70 | foreach ($constants as $value) { |
|
372 | 68 | $names = \array_keys($constants, $value, true); |
|
373 | 68 | if (\count($names) > 1) { |
|
374 | 36 | $ambiguous[\var_export($value, true)] = $names; |
|
375 | 2 | } |
|
376 | 35 | } |
|
377 | 70 | if (!empty($ambiguous)) { |
|
378 | 4 | throw new LogicException( |
|
379 | 'All possible values needs to be unique. The following are ambiguous: ' |
||
380 | 4 | . \implode(', ', \array_map(function ($names) use ($constants) { |
|
381 | 4 | return \implode('/', $names) . '=' . \var_export($constants[$names[0]], true); |
|
382 | 4 | }, $ambiguous)) |
|
383 | 2 | ); |
|
384 | } |
||
385 | |||
386 | 66 | self::$constants[$class] = $constants; |
|
387 | 66 | self::$names[$class] = \array_keys($constants); |
|
388 | 33 | } |
|
389 | |||
390 | 198 | return self::$constants[$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 | 52 | final public static function __callStatic($method, array $args) |
|
409 | } |
||
410 |