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 string */ |
||
| 23 | protected $value; |
||
| 24 | |||
| 25 | /** @var int */ |
||
| 26 | protected $index; |
||
| 27 | |||
| 28 | public function __construct(?string $value = null, ?int $index = null) |
||
| 29 | { |
||
| 30 | if (is_null($value) && is_null($index)) { |
||
| 31 | $value = $this->resolveValueFromStaticCall(); |
||
| 32 | $index = static::toArray()[$value]; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (is_null($value) || ! static::isValidValue($value)) { |
||
| 36 | throw new InvalidValueException($value, static::class); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (is_null($index) || ! static::isValidIndex($index)) { |
||
| 40 | throw new InvalidIndexException($index, static::class); |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->value = $value; |
||
| 44 | $this->index = $index; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function __call($name, $arguments) |
||
| 48 | { |
||
| 49 | if (strlen($name) > 2 && strpos($name, 'is') === 0) { |
||
| 50 | return $this->isEqual(substr($name, 2)); |
||
| 51 | } |
||
| 52 | |||
| 53 | throw new BadMethodCallException(sprintf('Call to undefined method %s->%s()', static::class, $name)); |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function __callStatic($name, $arguments) |
||
| 57 | { |
||
| 58 | if (strlen($name) > 2 && strpos($name, 'is') === 0) { |
||
| 59 | if (! isset($arguments[0])) { |
||
| 60 | throw new \ArgumentCountError(sprintf('Calling %s::%s() in static context requires one argument', static::class, $name)); |
||
| 61 | } |
||
| 62 | |||
| 63 | return static::make($arguments[0])->$name(); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (static::isValidName($name) || static::isValidValue($name)) { |
||
| 67 | return static::make($name); |
||
| 68 | } |
||
| 69 | |||
| 70 | throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', static::class, $name)); |
||
| 71 | } |
||
| 72 | |||
| 73 | public static function make($value): Enumerable |
||
| 74 | { |
||
| 75 | if (! (is_int($value) || is_string($value))) { |
||
| 76 | throw new TypeError(sprintf('%s::make() expects string|int as argument but %s given', static::class, gettype($value))); |
||
|
|
|||
| 77 | } |
||
| 78 | |||
| 79 | $name = null; |
||
| 80 | $index = null; |
||
| 81 | |||
| 82 | if (is_int($value)) { |
||
| 83 | if (! static::isValidIndex($value)) { |
||
| 84 | throw new InvalidIndexException($value, static::class); |
||
| 85 | } |
||
| 86 | |||
| 87 | $name = array_combine(static::getIndices(), array_keys(static::resolve()))[$value]; |
||
| 88 | $index = $value; |
||
| 89 | $value = array_search($index, static::toArray()); |
||
| 90 | } elseif (is_string($value)) { |
||
| 91 | if (method_exists(static::class, $value)) { |
||
| 92 | return forward_static_call(static::class.'::'.$value); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (static::isValidValue($value)) { |
||
| 96 | $index = static::toArray()[$value]; |
||
| 97 | $name = array_combine(static::getValues(), array_keys(static::resolve()))[$value]; |
||
| 98 | } elseif (static::isValidName($value)) { |
||
| 99 | $name = $value; |
||
| 100 | list('value' => $value, 'index' => $index) = static::resolve()[strtoupper($name)]; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (is_string($name) && method_exists(static::class, $name)) { |
||
| 105 | return forward_static_call(static::class.'::'.$name); |
||
| 106 | } elseif (is_int($index) && is_string($value)) { |
||
| 107 | return new static($value, $index); |
||
| 108 | } |
||
| 109 | |||
| 110 | throw new InvalidValueException($value, static::class); |
||
| 111 | } |
||
| 112 | |||
| 113 | public static function isValidIndex(int $index): bool |
||
| 117 | |||
| 118 | public static function isValidName(string $value): bool |
||
| 122 | |||
| 123 | public static function isValidValue(string $value): bool |
||
| 127 | |||
| 128 | public static function getIndices(): array |
||
| 132 | |||
| 133 | public static function getValues(): array |
||
| 137 | |||
| 138 | public static function toArray(): array |
||
| 144 | |||
| 145 | public function getValue(): string |
||
| 149 | |||
| 150 | public function getIndex(): int |
||
| 154 | |||
| 155 | public function isEqual($value): bool |
||
| 156 | { |
||
| 157 | if (is_int($value) || is_string($value)) { |
||
| 158 | $enum = static::make($value); |
||
| 159 | } elseif ($value instanceof $this) { |
||
| 160 | $enum = $value; |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( |
||
| 164 | isset($enum) |
||
| 165 | && $enum instanceof $this |
||
| 166 | && $enum->getValue() === $this->getValue() |
||
| 167 | ) { |
||
| 168 | return true; |
||
| 169 | } |
||
| 170 | |||
| 171 | return false; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function isAny(array $values): bool |
||
| 184 | |||
| 185 | public function __toString(): string |
||
| 189 | |||
| 190 | public function jsonSerialize() |
||
| 194 | |||
| 195 | protected static function resolve(): array |
||
| 196 | { |
||
| 197 | $values = []; |
||
| 198 | |||
| 199 | $class = static::class; |
||
| 246 | |||
| 247 | protected static function resolveFromStaticMethods(ReflectionClass $reflection): array |
||
| 260 | |||
| 261 | protected static function resolveFromDocBlocks(ReflectionClass $reflection): array |
||
| 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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.