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 |
||
15 | abstract class Enum implements Enumerable, JsonSerializable |
||
16 | { |
||
17 | /** @var array[] */ |
||
18 | protected static $cache = []; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $value; |
||
22 | |||
23 | /** @var int */ |
||
24 | protected $index; |
||
25 | |||
26 | public function __construct(?string $value = null, ?int $index = null) |
||
27 | { |
||
28 | if (is_null($value) && is_null($index)) { |
||
29 | $value = $this->resolveValueFromStaticCall(); |
||
30 | $index = static::toArray()[$value]; |
||
31 | } |
||
32 | |||
33 | if (! static::isValidValue($value)) { |
||
34 | throw new InvalidValueException($value, static::class); |
||
35 | } |
||
36 | |||
37 | if (! static::isValidIndex($index)) { |
||
38 | throw new InvalidIndexException($value, static::class); |
||
39 | } |
||
40 | |||
41 | $this->value = $value; |
||
42 | $this->index = $index; |
||
43 | } |
||
44 | |||
45 | public function __call($name, $arguments) |
||
46 | { |
||
47 | $name = strtolower($name); |
||
48 | |||
49 | if (strlen($name) > 2 && strpos($name, 'is') === 0) { |
||
50 | return $this->isEqual(substr($name, 2)); |
||
51 | } |
||
52 | |||
53 | if (static::isValidValue($name)) { |
||
54 | return static::__callStatic($name, $arguments); |
||
55 | } |
||
56 | |||
57 | throw new BadMethodCallException(sprintf('Call to undefined method %s->%s()', static::class, $name)); |
||
58 | } |
||
59 | |||
60 | public static function __callStatic($name, $arguments) |
||
61 | { |
||
62 | $name = strtolower($name); |
||
63 | |||
64 | if (strlen($name) > 2 && strpos($name, 'is') === 0) { |
||
65 | if (! isset($arguments[0])) { |
||
66 | throw new \ArgumentCountError(sprintf('Calling %s::%s() in static context requires one argument', static::class, $name)); |
||
67 | } |
||
68 | |||
69 | return static::make($arguments[0])->$name(); |
||
70 | } |
||
71 | |||
72 | if (static::isValidValue($name)) { |
||
73 | return static::make($name); |
||
74 | } |
||
75 | |||
76 | throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', static::class, $name)); |
||
77 | } |
||
78 | |||
79 | public static function make($value): Enumerable |
||
80 | { |
||
81 | if (is_int($value)) { |
||
82 | $index = $value; |
||
83 | |||
84 | if (! static::isValidIndex($index)) { |
||
85 | throw new InvalidIndexException($value, static::class); |
||
86 | } |
||
87 | |||
88 | return new static(array_search($index, static::toArray()), $index); |
||
89 | } elseif (is_string($value)) { |
||
90 | $value = strtolower($value); |
||
91 | |||
92 | if (! static::isValidValue($value)) { |
||
93 | throw new InvalidValueException($value, static::class); |
||
94 | } |
||
95 | |||
96 | if (method_exists(static::class, $value)) { |
||
97 | return forward_static_call(static::class.'::'.$value); |
||
98 | } |
||
99 | |||
100 | return new static($value, static::toArray()[$value]); |
||
101 | } |
||
102 | |||
103 | throw new TypeError(sprintf('%s::make() expects string|int as argument but %s given', static::class, gettype($value))); |
||
|
|||
104 | } |
||
105 | |||
106 | public static function isValidIndex(int $index): bool |
||
107 | { |
||
108 | return in_array($index, static::getIndices()); |
||
109 | } |
||
110 | |||
111 | public static function isValidValue(string $value): bool |
||
112 | { |
||
113 | return in_array($value, static::getValues()); |
||
114 | } |
||
115 | |||
116 | public static function getIndices(): array |
||
117 | { |
||
118 | return array_values(static::toArray()); |
||
119 | } |
||
120 | |||
121 | public static function getValues(): array |
||
125 | |||
126 | public static function toArray(): array |
||
127 | { |
||
128 | return static::resolve(); |
||
129 | } |
||
130 | |||
131 | public function getValue(): string |
||
132 | { |
||
133 | return $this->value; |
||
135 | |||
136 | public function getIndex(): int |
||
140 | |||
141 | public function isEqual($value): bool |
||
159 | |||
160 | public function isAny(array $values): bool |
||
170 | |||
171 | public function __toString(): string |
||
175 | |||
176 | public function jsonSerialize() |
||
180 | |||
181 | protected static function resolve(): array |
||
203 | |||
204 | protected static function resolveFromStaticMethods(ReflectionClass $reflection): array |
||
217 | |||
218 | protected static function resolveFromDocBlocks(ReflectionClass $reflection): array |
||
236 | |||
237 | protected function resolveValueFromStaticCall(): string |
||
253 | } |
||
254 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.