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 |
||
14 | class Enum implements \Serializable, \ArrayAccess |
||
15 | { |
||
16 | /** |
||
17 | * base enum values |
||
18 | * |
||
19 | * @var array $values |
||
20 | */ |
||
21 | protected static $values = []; |
||
22 | |||
23 | /** |
||
24 | * allow keys to be unset/deleted? |
||
25 | * cannot be over-ridden once defined in a class |
||
26 | * |
||
27 | * @var boolean |
||
28 | */ |
||
29 | protected static $delete = false; |
||
30 | |||
31 | /** |
||
32 | * allow keys to be over-written when adding? |
||
33 | * cannot be over-ridden once defined in a class |
||
34 | * |
||
35 | * @var boolean |
||
36 | */ |
||
37 | protected static $overwrite = false; |
||
38 | |||
39 | /** |
||
40 | * always capitalize enum keys? |
||
41 | * can be over-ridden once defined in a class |
||
42 | * with capitalize(boolean) method |
||
43 | * |
||
44 | * @var boolean |
||
45 | */ |
||
46 | protected static $capitalize = false; |
||
47 | |||
48 | /** |
||
49 | * case-sensitive check when searching by key for a value? |
||
50 | * can be over-ridden once defined in a class |
||
51 | * with caseSensitive(boolean) method |
||
52 | * |
||
53 | * @var boolean |
||
54 | */ |
||
55 | protected static $caseSensitive = false; |
||
56 | |||
57 | |||
58 | /** |
||
59 | * initialize with array of additional values not existing already |
||
60 | * |
||
61 | * @param array static::$values |
||
62 | */ |
||
63 | public function __construct(array $newValues = []) |
||
64 | { |
||
65 | static::fixKeys(); |
||
66 | static::add($newValues); |
||
67 | } |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Set case-sensitivity when searching |
||
72 | * |
||
73 | * @param boolean $bool |
||
74 | */ |
||
75 | 2 | public static function caseSensitive($bool = false) |
|
79 | |||
80 | |||
81 | /** |
||
82 | * Set capitalization for enum keys |
||
83 | * |
||
84 | * @param boolean $bool |
||
85 | */ |
||
86 | 2 | public static function capitalize($bool = false) |
|
93 | |||
94 | |||
95 | /** |
||
96 | * Clear all data - use with care! |
||
97 | * |
||
98 | * @param boolean $caseSensitive |
||
99 | * @param boolean $capitalize |
||
100 | */ |
||
101 | 1 | public static function reset($caseSensitive = false, $capitalize = false) |
|
107 | |||
108 | |||
109 | /** |
||
110 | * Remove an enum value - use with care! |
||
111 | * |
||
112 | */ |
||
113 | 1 | public static function delete($key) |
|
114 | { |
||
115 | 1 | $allowed = !empty(static::$delete); |
|
116 | 1 | if (empty($allowed)) { |
|
117 | 1 | throw new \LogicException('Method not allowed.'); |
|
118 | } |
||
119 | $values = & static::$values; |
||
120 | if (array_key_exists($key, $values)) { |
||
121 | unset($values[$key]); |
||
122 | } else { |
||
123 | return false; |
||
124 | } |
||
125 | return true; |
||
126 | } |
||
127 | |||
128 | |||
129 | /** |
||
130 | * Make sure that the enum keys have no SPACEs |
||
131 | * Make sure the keys are strings |
||
132 | * Set the case according to the settings |
||
133 | * |
||
134 | * @param return array static::$values |
||
135 | */ |
||
136 | 2 | public static function fixKeys(): array |
|
158 | |||
159 | |||
160 | /** |
||
161 | * add array of extra values not existing already |
||
162 | * |
||
163 | * @param array|string $newValues |
||
164 | * @param null|boolean $overwrite allow over-write of values? |
||
165 | * @return array static::$values |
||
166 | */ |
||
167 | 1 | public static function add($newValues, $overwrite = null): array |
|
190 | |||
191 | |||
192 | /** |
||
193 | * get array of keys |
||
194 | * |
||
195 | * @return array keys of static::$values |
||
196 | */ |
||
197 | 1 | public static function keys(): array |
|
201 | |||
202 | |||
203 | /** |
||
204 | * get key for the given value |
||
205 | * |
||
206 | * @param mixed $value |
||
207 | * @param null|bool $caseSensitive search is case sensitive? |
||
208 | * @return integer|string|array<integer|string>|null key(s) |
||
209 | */ |
||
210 | 1 | public static function key($value, $caseSensitive = null) |
|
211 | { |
||
212 | 1 | $values = static::$values; |
|
213 | 1 | if (!is_array($value) && !is_object($value)) { |
|
214 | 1 | if (is_string($value)) { |
|
215 | 1 | $value = strtoupper($value); |
|
216 | // if case-sensitivity is not specified use the class boolean value |
||
217 | 1 | if (null === $caseSensitive) { |
|
218 | 1 | $caseSensitive = static::$caseSensitive; |
|
219 | } |
||
220 | 1 | if (empty($caseSensitive)) { |
|
221 | 1 | $values = array_map(function($value) { |
|
222 | 1 | return strtoupper($value); |
|
223 | 1 | }, $values); |
|
224 | } |
||
225 | } |
||
226 | 1 | $keys = array_keys($values, $value); |
|
227 | 1 | $count = count($keys); |
|
228 | 1 | if (0 === $count) { |
|
229 | throw new \InvalidArgumentException(sprintf("Key for value '%s' does not exist.", print_r($value, 1))); |
||
230 | } |
||
231 | 1 | return count($keys) > 1 ? $keys : $keys[0]; |
|
232 | } elseif (is_array($value)) { |
||
233 | $search = array_search($value, $values); |
||
234 | if (false === $search) { |
||
235 | throw new \InvalidArgumentException(sprintf("Key for value '%s' does not exist.", print_r($value, 1))); |
||
236 | } |
||
237 | return $search; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | |||
242 | /** |
||
243 | * count values |
||
244 | * |
||
245 | * @return int number of values |
||
246 | */ |
||
247 | 1 | public static function count(): int |
|
251 | |||
252 | |||
253 | /** |
||
254 | * count values |
||
255 | * |
||
256 | * @return int number of values |
||
257 | */ |
||
258 | 1 | public static function sizeof(): int |
|
262 | |||
263 | |||
264 | /** |
||
265 | * get existing values |
||
266 | * |
||
267 | * @return array static::$values |
||
268 | */ |
||
269 | 1 | public static function values(): array |
|
273 | |||
274 | |||
275 | /** |
||
276 | * get value for the given key |
||
277 | * |
||
278 | * @param string $key |
||
279 | * @param null|bool $caseSensitive search is case sensitive? |
||
280 | * @return mixed static::$values[$key] |
||
281 | */ |
||
282 | 3 | public static function value($key, $caseSensitive = null) |
|
298 | |||
299 | |||
300 | /** |
||
301 | * get value for the given key |
||
302 | * method allows getting value from an object with $object->key |
||
303 | * |
||
304 | * @return mixed static::$values[$key] |
||
305 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.get |
||
306 | */ |
||
307 | public function __get(string $key) { |
||
308 | return static::value($key); |
||
309 | } |
||
310 | |||
311 | |||
312 | /** |
||
313 | * get value named the same as the method called |
||
314 | * method allows getting value from an object with $object->key() |
||
315 | * |
||
316 | * @param string $key the method called is used as the key |
||
317 | * @param array $args |
||
318 | * @return mixed static::$values[$key] |
||
319 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.call |
||
320 | */ |
||
321 | 1 | public function __call(string $key, array $args = []) { |
|
324 | |||
325 | |||
326 | /** |
||
327 | * get value named the same as the method called statically |
||
328 | * method allows getting value from an object with $object::key() |
||
329 | * |
||
330 | * @param string $key the method called is used as the key |
||
331 | * @param array $args |
||
332 | * @return mixed static::$values[$key] |
||
333 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.callstatic |
||
334 | */ |
||
335 | public static function __callStatic(string $key, array $args = []) { |
||
336 | return static::value($key); |
||
337 | } |
||
338 | |||
339 | |||
340 | /** |
||
341 | * check if the given key exists via call to $object of isset($object->key) |
||
342 | * |
||
343 | * @param string $key |
||
344 | * @return boolean |
||
345 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.isset |
||
346 | */ |
||
347 | 1 | public function __isset($key) |
|
352 | |||
353 | |||
354 | /** |
||
355 | * when called as a function this class will add new values and return the result |
||
356 | * |
||
357 | * @param array $newValues |
||
358 | * @return array|null static::$values |
||
359 | * @link http://php.net/manual/en/language.oop5.overloading.php#object.invoke |
||
360 | */ |
||
361 | 1 | public function __invoke($newValues) |
|
365 | |||
366 | |||
367 | /** |
||
368 | * return the values as a string |
||
369 | * method allows outputting values as a string |
||
370 | * |
||
371 | * @return string json_encode(static::$values) |
||
372 | * @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
373 | */ |
||
374 | 1 | public function __toString(): string |
|
378 | |||
379 | |||
380 | /** |
||
381 | * return the values as a string |
||
382 | * method allows outputting values as a string when called statically |
||
383 | * |
||
384 | * @return string json_encode(static::$values) |
||
385 | */ |
||
386 | public static function toString(): string |
||
387 | { |
||
388 | return json_encode(static::$values, JSON_PRETTY_PRINT); |
||
389 | } |
||
390 | |||
391 | |||
392 | /** |
||
393 | * serialize enum values |
||
394 | * |
||
395 | * @return string enum values serialized |
||
396 | * @link http://php.net/manual/en/class.serializable.php |
||
397 | */ |
||
398 | 1 | public function serialize(): string |
|
402 | |||
403 | /** |
||
404 | * unserialize serialized values to object |
||
405 | * |
||
406 | * @return string enum values serialized |
||
407 | * @link http://php.net/manual/en/class.serializable.php |
||
408 | * @return void |
||
409 | */ |
||
410 | 1 | public function unserialize($data) { |
|
414 | |||
415 | |||
416 | /** |
||
417 | * returned dump values as array |
||
418 | * |
||
419 | * @return array |
||
420 | */ |
||
421 | public static function var_dump(): array |
||
422 | { |
||
423 | $delete = static::$delete; |
||
424 | $overwrite = static::$overwrite; |
||
425 | $capitalize = static::$capitalize; |
||
426 | $caseSensitive = static::$caseSensitive; |
||
427 | $values = static::$values; |
||
428 | return [ |
||
429 | 'overwrite' => $overwrite, |
||
430 | 'delete' => $delete, |
||
431 | 'capitalise' => $capitalize, |
||
432 | 'caseSensitive' => $caseSensitive, |
||
433 | 'values' => $values |
||
434 | ]; |
||
435 | } |
||
436 | |||
437 | |||
438 | /** |
||
439 | * returned values when called with var_dump() |
||
440 | * |
||
441 | * @return array|null debug info |
||
442 | * @link http://php.net/manual/en/language.oop5.magic.php#object.debuginfo |
||
443 | */ |
||
444 | public function __debugInfo(): array |
||
445 | { |
||
446 | return static::var_dump(); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Implement Array offsetSet |
||
451 | * |
||
452 | * @param string $key the key to set |
||
453 | * @param mixed $value the value to set |
||
454 | * @return array|null debug info |
||
455 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
456 | */ |
||
457 | public function offsetSet($key, $value) { |
||
458 | if (is_null($key)) { |
||
459 | static::$values[] = $value; |
||
460 | } else { |
||
461 | static::$values[$key] = $value; |
||
462 | } |
||
463 | } |
||
464 | |||
465 | |||
466 | /** |
||
467 | * Implement Array offsetExists |
||
468 | * |
||
469 | * @param string $key the key to set |
||
470 | * @return boolean debug info |
||
471 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
472 | */ |
||
473 | 1 | public function offsetExists($key) { |
|
476 | |||
477 | |||
478 | /** |
||
479 | * Implement Array offsetUnset |
||
480 | * |
||
481 | * @param string $key the key to set |
||
482 | * @return array|null debug info |
||
483 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
484 | */ |
||
485 | 1 | public function offsetUnset($key) { |
|
486 | 1 | if (!empty(static::$overwrite)) { |
|
487 | throw new \LogicException('Overwrite not allowed.'); |
||
488 | } |
||
489 | 1 | unset(static::$values[$key]); |
|
490 | 1 | } |
|
491 | |||
492 | |||
493 | /** |
||
494 | * Implement Array offsetGet |
||
495 | * |
||
496 | * @param string $key the key to set |
||
497 | * @return array debug info |
||
498 | * @link http://php.net/manual/en/class.arrayaccess.php |
||
499 | */ |
||
500 | 1 | public function offsetGet($key) { |
|
503 | } |
||
504 |