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 | /** @var string */ |
||
29 | protected $name; |
||
30 | |||
31 | /** |
||
32 | * This construct is not part of the public API and COULD change in a minor release. |
||
33 | * You SHOULD NOT use it by your own - instead you SHOULD use the make() method. |
||
34 | * |
||
35 | * @internal |
||
36 | * @see \Spatie\Enum\Enum::make() |
||
37 | * |
||
38 | * @param string|null $name |
||
39 | * @param string|null $value |
||
40 | * @param int|null $index |
||
41 | */ |
||
42 | public function __construct(?string $name = null, ?string $value = null, ?int $index = null) |
||
43 | { |
||
44 | if (is_null($name) && is_null($value) && is_null($index)) { |
||
45 | ['name' => $name, 'value' => $value, 'index' => $index] = $this->resolveByStaticCall(); |
||
46 | } |
||
47 | |||
48 | if (is_null($name) || ! static::isValidName($name)) { |
||
49 | throw new InvalidNameException($name, static::class); |
||
50 | } |
||
51 | |||
52 | if (is_null($value) || ! static::isValidValue($value)) { |
||
53 | throw new InvalidValueException($value, static::class); |
||
54 | } |
||
55 | |||
56 | if (is_null($index) || ! static::isValidIndex($index)) { |
||
57 | throw new InvalidIndexException($index, static::class); |
||
58 | } |
||
59 | |||
60 | $this->name = $name; |
||
61 | $this->value = $value; |
||
62 | $this->index = $index; |
||
63 | } |
||
64 | |||
65 | public function __call(string $name, array $arguments) |
||
66 | { |
||
67 | if (static::startsWith($name, 'is')) { |
||
68 | return $this->isEqual(substr($name, 2)); |
||
69 | } |
||
70 | |||
71 | if (static::isValidName($name)) { |
||
72 | return static::make($name); |
||
73 | } |
||
74 | |||
75 | throw new BadMethodCallException('Call to undefined method '.static::class.'->'.$name.'()'); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $name |
||
80 | * @param mixed[] $arguments |
||
81 | * |
||
82 | * @return \Spatie\Enum\Enumerable|bool |
||
83 | */ |
||
84 | public static function __callStatic(string $name, array $arguments) |
||
85 | { |
||
86 | if (static::startsWith($name, 'is')) { |
||
87 | if (! isset($arguments[0])) { |
||
88 | throw new ArgumentCountError('Calling '.static::class.'::'.$name.'() in static context requires one argument'); |
||
89 | } |
||
90 | |||
91 | return static::make($arguments[0])->$name(); |
||
92 | } |
||
93 | |||
94 | if (static::isValidName($name) || static::isValidValue($name)) { |
||
95 | return static::make($name); |
||
96 | } |
||
97 | |||
98 | throw new BadMethodCallException('Call to undefined method '.static::class.'::'.$name.'()'); |
||
99 | } |
||
100 | |||
101 | public function __toString(): string |
||
105 | |||
106 | public function getIndex(): int |
||
107 | { |
||
108 | return $this->index; |
||
109 | } |
||
110 | |||
111 | public static function getIndices(): array |
||
112 | { |
||
113 | return array_column(static::resolve(), 'index'); |
||
114 | } |
||
115 | |||
116 | public function getValue(): string |
||
117 | { |
||
118 | return $this->value; |
||
119 | } |
||
120 | |||
121 | public static function getValues(): array |
||
125 | |||
126 | public function getName(): string |
||
127 | { |
||
128 | return $this->name; |
||
129 | } |
||
130 | |||
131 | public static function getNames(): array |
||
132 | { |
||
135 | |||
136 | public function isAny(array $values): bool |
||
146 | |||
147 | public function isEqual($value): bool |
||
165 | |||
166 | public function jsonSerialize(): string |
||
170 | |||
171 | /** |
||
172 | * @param int|string $value |
||
173 | * |
||
174 | * @return static |
||
175 | */ |
||
176 | public static function make($value): Enumerable |
||
201 | |||
202 | /** |
||
203 | * @param int $index |
||
204 | * |
||
205 | * @return static |
||
206 | */ |
||
207 | public static function makeByIndex(int $index): Enumerable |
||
217 | |||
218 | /** |
||
219 | * @param string $value |
||
220 | * |
||
221 | * @return static |
||
222 | */ |
||
223 | public static function makeByValue(string $value): Enumerable |
||
233 | |||
234 | public static function toArray(): array |
||
238 | |||
239 | /** |
||
240 | * @return \Spatie\Enum\Enumerable[] |
||
241 | */ |
||
242 | public static function getAll(): array |
||
248 | |||
249 | public static function isValidIndex(int $index): bool |
||
253 | |||
254 | public static function isValidName(string $value): bool |
||
258 | |||
259 | public static function isValidValue(string $value): bool |
||
263 | |||
264 | protected static function resolve(): array |
||
321 | |||
322 | protected static function resolveFromDocBlocks(ReflectionClass $reflection): array |
||
340 | |||
341 | protected static function resolveFromStaticMethods(ReflectionClass $reflection): array |
||
363 | |||
364 | protected function resolveByStaticCall(): array |
||
380 | |||
381 | protected static function resolveByIndex(int $index): array |
||
388 | |||
389 | protected static function resolveByString(string $string): array |
||
401 | |||
402 | protected static function resolveByValue(string $value): array |
||
409 | |||
410 | protected static function resolveByName(string $name): array |
||
418 | |||
419 | protected static function startsWith(string $haystack, string $needle): bool |
||
423 | |||
424 | protected static function clearCache(): void |
||
428 | |||
429 | protected static function getMappedIndex(string $name): ?int |
||
443 | |||
444 | protected static function getMappedValue(string $name): ?string |
||
458 | } |
||
459 |
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.