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 | 21 | 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() |
|
122 | { |
||
123 | 44 | if ($this->ordinal !== null) { |
|
124 | 44 | return $this->ordinal; |
|
125 | } |
||
126 | |||
127 | // detect ordinal |
||
128 | 9 | $ordinal = 0; |
|
129 | 9 | $value = $this->value; |
|
130 | 9 | foreach (self::detectConstants(get_called_class()) as $constValue) { |
|
131 | 9 | if ($value === $constValue) { |
|
132 | 9 | break; |
|
133 | } |
||
134 | 9 | ++$ordinal; |
|
135 | } |
||
136 | |||
137 | 9 | $this->ordinal = $ordinal; |
|
138 | 9 | return $ordinal; |
|
139 | } |
||
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 | 1 | final public function is($enumerator) |
|
151 | |||
152 | /** |
||
153 | * Get an enumerator instance of the given value or instance |
||
154 | * |
||
155 | * @param static|null|bool|int|float|string $value |
||
156 | * @return static |
||
157 | * @throws InvalidArgumentException On an unknwon or invalid value |
||
158 | * @throws LogicException On ambiguous constant values |
||
159 | */ |
||
160 | 67 | final public static function get($value) |
|
161 | { |
||
162 | 67 | if ($value instanceof static && get_class($value) === get_called_class()) { |
|
163 | 26 | return $value; |
|
164 | } |
||
165 | |||
166 | 48 | $class = get_called_class(); |
|
167 | 48 | $constants = self::detectConstants($class); |
|
168 | 46 | $name = array_search($value, $constants, true); |
|
169 | 46 | View Code Duplication | if ($name === false) { |
170 | 7 | $message = is_scalar($value) |
|
171 | 3 | ? 'Unknown value ' . var_export($value, true) |
|
172 | 7 | : 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value)); |
|
173 | 7 | throw new InvalidArgumentException($message); |
|
174 | } |
||
175 | |||
176 | 40 | if (!isset(self::$instances[$class][$name])) { |
|
177 | 7 | self::$instances[$class][$name] = new $class($constants[$name]); |
|
178 | } |
||
179 | |||
180 | 40 | return self::$instances[$class][$name]; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get an enumerator instance by the given name |
||
185 | * |
||
186 | * @param string $name The name of the enumerator |
||
187 | * @return static |
||
188 | * @throws InvalidArgumentException On an invalid or unknown name |
||
189 | * @throws LogicException On ambiguous values |
||
190 | */ |
||
191 | 36 | final public static function getByName($name) |
|
206 | |||
207 | /** |
||
208 | * Get an enumeration instance by the given ordinal number |
||
209 | * |
||
210 | * @param int $ordinal The ordinal number or the enumerator |
||
211 | * @return static |
||
212 | * @throws InvalidArgumentException On an invalid ordinal number |
||
213 | * @throws LogicException On ambiguous values |
||
214 | */ |
||
215 | 19 | final public static function getByOrdinal($ordinal) |
|
235 | |||
236 | /** |
||
237 | * Clear all instantiated enumerators of the called class |
||
238 | * |
||
239 | * NOTE: This can break singleton behavior ... use it with caution! |
||
240 | * |
||
241 | * @return void |
||
242 | * @deprecated |
||
243 | */ |
||
244 | final public static function clear() |
||
249 | |||
250 | /** |
||
251 | * Get a list of enumerator instances ordered by ordinal number |
||
252 | * |
||
253 | * @return static[] |
||
254 | */ |
||
255 | 13 | final public static function getEnumerators() |
|
259 | |||
260 | /** |
||
261 | * Get all available constants of the called class |
||
262 | * |
||
263 | * @return array |
||
264 | * @throws LogicException On ambiguous constant values |
||
265 | */ |
||
266 | 58 | final public static function getConstants() |
|
270 | |||
271 | /** |
||
272 | * Is the given enumerator part of this enumeration |
||
273 | * |
||
274 | * @param static|null|bool|int|float|string $value |
||
275 | * @return bool |
||
276 | */ |
||
277 | 1 | final public static function has($value) |
|
288 | |||
289 | /** |
||
290 | * Detect all available constants by the given class |
||
291 | * |
||
292 | * @param string $class |
||
293 | * @return array |
||
294 | * @throws LogicException On ambiguous constant values |
||
295 | */ |
||
296 | 78 | private static function detectConstants($class) |
|
297 | { |
||
298 | 78 | if (!isset(self::$constants[$class])) { |
|
299 | 12 | $reflection = new ReflectionClass($class); |
|
300 | 12 | $constants = array(); |
|
301 | |||
302 | do { |
||
303 | 12 | $scopeConstants = array(); |
|
304 | 12 | if (PHP_VERSION_ID >= 70100) { |
|
305 | // Since PHP-7.1 visibility modifiers are allowed for class constants |
||
306 | // for enumerations we are only interested in public once. |
||
307 | foreach ($reflection->getReflectionConstants() as $reflConstant) { |
||
308 | if ($reflConstant->isPublic()) { |
||
309 | $scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
||
310 | } |
||
311 | } |
||
312 | } else { |
||
313 | // In PHP < 7.1 all class constants were public by definition |
||
314 | 12 | $scopeConstants = $reflection->getConstants(); |
|
315 | } |
||
316 | |||
317 | 12 | $constants = $scopeConstants + $constants; |
|
318 | 12 | } while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
319 | |||
320 | // Detect ambiguous values and report names |
||
321 | 12 | $ambiguous = array(); |
|
322 | 12 | foreach ($constants as $value) { |
|
323 | 11 | $names = array_keys($constants, $value, true); |
|
324 | 11 | if (count($names) > 1) { |
|
325 | 11 | $ambiguous[var_export($value, true)] = $names; |
|
326 | } |
||
327 | } |
||
328 | 12 | if (!empty($ambiguous)) { |
|
329 | 2 | throw new LogicException( |
|
330 | 'All possible values needs to be unique. The following are ambiguous: ' |
||
331 | 2 | . implode(', ', array_map(function ($names) use ($constants) { |
|
332 | 2 | return implode('/', $names) . '=' . var_export($constants[$names[0]], true); |
|
333 | 2 | }, $ambiguous)) |
|
334 | ); |
||
335 | } |
||
336 | |||
337 | 10 | self::$constants[$class] = $constants; |
|
338 | } |
||
339 | |||
340 | 76 | return self::$constants[$class]; |
|
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get an enumerator instance by the given name. |
||
345 | * |
||
346 | * This will be called automatically on calling a method |
||
347 | * with the same name of a defined enumerator. |
||
348 | * |
||
349 | * @param string $method The name of the enumeraotr (called as method) |
||
350 | * @param array $args There should be no arguments |
||
351 | * @return static |
||
352 | * @throws InvalidArgumentException On an invalid or unknown name |
||
353 | * @throws LogicException On ambiguous constant values |
||
354 | */ |
||
355 | 21 | final public static function __callStatic($method, array $args) |
|
359 | } |
||
360 |