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 | public function __construct(?string $name = null, ?string $value = null, ?int $index = null) |
||
| 53 | |||
| 54 | public function __call($name, $arguments) |
||
| 62 | |||
| 63 | public static function __callStatic($name, $arguments) |
||
| 79 | |||
| 80 | public function __toString(): string |
||
| 84 | |||
| 85 | public function getIndex(): int |
||
| 89 | |||
| 90 | public static function getIndices(): array |
||
| 94 | |||
| 95 | public function getValue(): string |
||
| 99 | |||
| 100 | public static function getValues(): array |
||
| 104 | |||
| 105 | public function getName(): string |
||
| 109 | |||
| 110 | public static function getNames(): array |
||
| 114 | |||
| 115 | public function isAny(array $values): bool |
||
| 125 | |||
| 126 | public function isEqual($value): bool |
||
| 138 | |||
| 139 | public function jsonSerialize() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param int|string $value |
||
| 146 | * |
||
| 147 | * @return static |
||
| 148 | */ |
||
| 149 | public static function make($value): Enumerable |
||
| 174 | |||
| 175 | public static function toArray(): array |
||
| 181 | |||
| 182 | protected static function isValidIndex(int $index): bool |
||
| 186 | |||
| 187 | protected static function isValidName(string $value): bool |
||
| 191 | |||
| 192 | protected static function isValidValue(string $value): bool |
||
| 196 | |||
| 197 | protected static function resolve(): array |
||
| 198 | { |
||
| 199 | $values = []; |
||
| 200 | |||
| 201 | $class = static::class; |
||
| 202 | |||
| 203 | if (isset(self::$cache[$class])) { |
||
| 204 | return self::$cache[$class]; |
||
| 205 | } |
||
| 206 | |||
| 207 | self::$cache[$class] = []; |
||
| 208 | |||
| 209 | $reflection = new ReflectionClass(static::class); |
||
| 210 | |||
| 211 | foreach (self::resolveFromDocBlocks($reflection) as $value) { |
||
| 212 | $values[] = $value; |
||
| 213 | } |
||
| 214 | |||
| 215 | foreach (self::resolveFromStaticMethods($reflection) as $value) { |
||
| 216 | $values[] = $value; |
||
| 217 | } |
||
| 218 | |||
| 219 | foreach ($values as $index => $value) { |
||
| 220 | $name = strtoupper($value); |
||
| 221 | |||
| 222 | self::$cache[$class][$name] = [ |
||
| 223 | 'name' => $name, |
||
| 224 | 'index' => static::getMappedIndex($name) ?? $index, |
||
| 225 | 'value' => static::getMappedValue($name) ?? $value, |
||
| 226 | ]; |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach (self::$cache[$class] as $name => $enum) { |
||
| 230 | self::$cache[$class][$name]['value'] = static::make($name)->getValue(); |
||
| 231 | self::$cache[$class][$name]['index'] = static::make($name)->getIndex(); |
||
| 232 | } |
||
| 233 | |||
| 234 | $duplicatedValues = array_filter(array_count_values(static::getValues()), function (int $count) { |
||
| 235 | return $count > 1; |
||
| 236 | }); |
||
| 237 | |||
| 238 | if (! empty($duplicatedValues)) { |
||
| 239 | self::clearCache(); |
||
| 240 | throw new DuplicatedValueException(array_keys($duplicatedValues), static::class); |
||
| 241 | } |
||
| 242 | |||
| 243 | $duplicatedIndices = array_filter(array_count_values(static::getIndices()), function (int $count) { |
||
| 244 | return $count > 1; |
||
| 245 | }); |
||
| 246 | |||
| 247 | if (! empty($duplicatedIndices)) { |
||
| 248 | self::clearCache(); |
||
| 249 | throw new DuplicatedIndexException(array_keys($duplicatedIndices), static::class); |
||
| 250 | } |
||
| 251 | |||
| 252 | return self::$cache[$class]; |
||
| 253 | } |
||
| 254 | |||
| 255 | protected static function resolveFromDocBlocks(ReflectionClass $reflection): array |
||
| 256 | { |
||
| 257 | $values = []; |
||
| 258 | |||
| 259 | $docComment = $reflection->getDocComment(); |
||
| 260 | |||
| 261 | if (! $docComment) { |
||
| 262 | return $values; |
||
| 263 | } |
||
| 264 | |||
| 265 | preg_match_all('/\@method static self ([\w]+)\(\)/', $docComment, $matches); |
||
| 266 | |||
| 267 | foreach ($matches[1] ?? [] as $value) { |
||
| 268 | $values[] = $value; |
||
| 269 | } |
||
| 270 | |||
| 271 | return $values; |
||
| 272 | } |
||
| 273 | |||
| 274 | protected static function resolveFromStaticMethods(ReflectionClass $reflection): array |
||
| 275 | { |
||
| 276 | $values = []; |
||
| 277 | foreach ($reflection->getMethods(ReflectionMethod::IS_STATIC) as $method) { |
||
| 278 | if ($method->getDeclaringClass()->getName() === self::class) { |
||
| 279 | continue; |
||
| 280 | } |
||
| 281 | |||
| 282 | $values[] = $method->getName(); |
||
| 283 | } |
||
| 284 | |||
| 285 | return $values; |
||
| 286 | } |
||
| 287 | |||
| 288 | protected function resolveByStaticCall(): array |
||
| 289 | { |
||
| 290 | if (strpos(get_class($this), 'class@anonymous') !== 0) { |
||
| 291 | throw new InvalidValueException(null, static::class); |
||
| 292 | } |
||
| 293 | |||
| 294 | $backtrace = debug_backtrace(); |
||
| 295 | |||
| 296 | $name = $backtrace[2]['function']; |
||
| 297 | |||
| 298 | if (! static::isValidName($name)) { |
||
| 299 | throw new InvalidValueException($name, static::class); |
||
| 300 | } |
||
| 301 | |||
| 302 | return static::resolve()[strtoupper($name)]; |
||
| 303 | } |
||
| 304 | |||
| 305 | protected static function resolveByIndex(int $index): array |
||
| 312 | |||
| 313 | protected static function resolveByString(string $string): array |
||
| 325 | |||
| 326 | protected static function resolveByValue(string $value): array |
||
| 333 | |||
| 334 | protected static function resolveByName(string $name): array |
||
| 340 | |||
| 341 | protected static function startsWith(string $haystack, string $needle) |
||
| 345 | |||
| 346 | protected static function clearCache() |
||
| 350 | |||
| 351 | protected static function getMappedIndex(string $name): ?int |
||
| 359 | |||
| 360 | protected static function getMappedValue(string $name): ?string |
||
| 368 | } |
||
| 369 |
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.