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 |
||
17 | abstract class Enum implements Enumerable, JsonSerializable |
||
18 | { |
||
19 | /** @var array[] */ |
||
20 | protected static $cache = []; |
||
21 | |||
22 | /** @var int */ |
||
23 | protected $index; |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $value; |
||
27 | |||
28 | public function __construct(?string $value = null, ?int $index = null) |
||
46 | |||
47 | public function __call($name, $arguments) |
||
55 | |||
56 | public static function __callStatic($name, $arguments) |
||
72 | |||
73 | public function __toString(): string |
||
74 | { |
||
75 | return $this->getValue(); |
||
76 | } |
||
77 | |||
78 | public function getIndex(): int |
||
79 | { |
||
80 | return $this->index; |
||
81 | } |
||
82 | |||
83 | public static function getIndices(): array |
||
84 | { |
||
85 | return array_column(static::resolve(), 'index'); |
||
86 | } |
||
87 | |||
88 | public function getValue(): string |
||
89 | { |
||
90 | return $this->value; |
||
91 | } |
||
92 | |||
93 | public static function getValues(): array |
||
94 | { |
||
95 | return array_column(static::resolve(), 'value'); |
||
96 | } |
||
97 | |||
98 | public function isAny(array $values): bool |
||
99 | { |
||
100 | foreach ($values as $value) { |
||
101 | if ($this->isEqual($value)) { |
||
102 | return true; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return false; |
||
107 | } |
||
108 | |||
109 | public function isEqual($value): bool |
||
110 | { |
||
111 | if (is_int($value) || is_string($value)) { |
||
112 | $enum = static::make($value); |
||
113 | } elseif ($value instanceof $this) { |
||
114 | $enum = $value; |
||
115 | } |
||
116 | |||
117 | if ( |
||
118 | isset($enum) |
||
119 | && $enum instanceof $this |
||
120 | && $enum->getValue() === $this->getValue() |
||
121 | ) { |
||
122 | return true; |
||
123 | } |
||
124 | |||
125 | return false; |
||
126 | } |
||
127 | |||
128 | public function jsonSerialize() |
||
129 | { |
||
130 | return $this->getValue(); |
||
131 | } |
||
132 | |||
133 | public static function make($value): Enumerable |
||
134 | { |
||
135 | if (! (is_int($value) || is_string($value))) { |
||
136 | throw new TypeError(sprintf('%s::make() expects string|int as argument but %s given', static::class, gettype($value))); |
||
|
|||
137 | } |
||
138 | |||
139 | $name = null; |
||
140 | $index = null; |
||
141 | |||
142 | if (is_int($value)) { |
||
143 | if (! static::isValidIndex($value)) { |
||
144 | throw new InvalidIndexException($value, static::class); |
||
145 | } |
||
146 | |||
147 | $name = array_combine(static::getIndices(), array_keys(static::resolve()))[$value]; |
||
148 | $index = $value; |
||
149 | $value = array_search($index, static::toArray()); |
||
150 | } elseif (is_string($value)) { |
||
151 | if (method_exists(static::class, $value)) { |
||
152 | return forward_static_call(static::class.'::'.$value); |
||
153 | } |
||
154 | |||
155 | if (static::isValidValue($value)) { |
||
156 | $index = static::toArray()[$value]; |
||
157 | $name = array_combine(static::getValues(), array_keys(static::resolve()))[$value]; |
||
158 | } elseif (static::isValidName($value)) { |
||
159 | $name = $value; |
||
160 | list('value' => $value, 'index' => $index) = static::resolve()[strtoupper($name)]; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if (is_string($name) && method_exists(static::class, $name)) { |
||
165 | return forward_static_call(static::class.'::'.$name); |
||
166 | } elseif (is_int($index) && is_string($value)) { |
||
167 | return new static($value, $index); |
||
168 | } |
||
169 | |||
170 | throw new InvalidValueException($value, static::class); |
||
171 | } |
||
172 | |||
173 | public static function toArray(): array |
||
174 | { |
||
175 | $resolved = static::resolve(); |
||
176 | |||
177 | return array_combine(array_column($resolved, 'value'), array_column($resolved, 'index')); |
||
178 | } |
||
179 | |||
180 | protected static function isValidIndex(int $index): bool |
||
181 | { |
||
182 | return in_array($index, static::getIndices(), true); |
||
183 | } |
||
184 | |||
185 | protected static function isValidName(string $value): bool |
||
186 | { |
||
187 | return in_array(strtoupper($value), array_keys(static::resolve()), true); |
||
188 | } |
||
189 | |||
190 | protected static function isValidValue(string $value): bool |
||
191 | { |
||
192 | return in_array($value, static::getValues(), true); |
||
193 | } |
||
194 | |||
195 | protected static function resolve(): array |
||
246 | |||
247 | protected static function resolveFromDocBlocks(ReflectionClass $reflection): array |
||
248 | { |
||
249 | $values = []; |
||
250 | |||
251 | $docComment = $reflection->getDocComment(); |
||
252 | |||
253 | if (! $docComment) { |
||
254 | return $values; |
||
255 | } |
||
256 | |||
257 | preg_match_all('/\@method static self ([\w]+)\(\)/', $docComment, $matches); |
||
258 | |||
259 | foreach ($matches[1] ?? [] as $value) { |
||
260 | $values[] = $value; |
||
261 | } |
||
262 | |||
263 | return $values; |
||
264 | } |
||
265 | |||
266 | protected static function resolveFromStaticMethods(ReflectionClass $reflection): array |
||
267 | { |
||
268 | $values = []; |
||
269 | foreach ($reflection->getMethods(ReflectionMethod::IS_STATIC) as $method) { |
||
270 | if ($method->getDeclaringClass()->getName() === self::class) { |
||
271 | continue; |
||
272 | } |
||
273 | |||
274 | $values[] = $method->getName(); |
||
275 | } |
||
276 | |||
277 | return $values; |
||
278 | } |
||
279 | |||
280 | protected function resolveValueFromStaticCall(): string |
||
296 | } |
||
297 |
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.