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 |
||
| 12 | abstract class Enum implements JsonSerializable |
||
| 13 | { |
||
| 14 | /** @var array */ |
||
| 15 | protected static $cache = []; |
||
| 16 | |||
| 17 | /** @var array */ |
||
| 18 | protected static $map = []; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $value; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $value |
||
| 25 | * |
||
| 26 | * @return static |
||
| 27 | */ |
||
| 28 | public static function from(string $value): Enum |
||
| 36 | |||
| 37 | public function __construct(string $value = null) |
||
| 59 | |||
| 60 | public static function __callStatic($name, $arguments) |
||
| 76 | |||
| 77 | public function __call($name, $arguments) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string|\Spatie\Enum\Enum $enum |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function equals($enum): bool |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string[]|\Spatie\Enum\Enum[] $enums |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function isOneOf(array $enums): bool |
||
| 127 | |||
| 128 | public function __toString(): string |
||
| 132 | |||
| 133 | public function jsonSerialize() |
||
| 137 | |||
| 138 | public static function toArray(): array |
||
| 142 | |||
| 143 | public static function getKeys(): array |
||
| 147 | |||
| 148 | public static function getValues(): array |
||
| 152 | |||
| 153 | protected static function resolve(): array |
||
| 175 | |||
| 176 | protected static function resolveValuesFromStaticMethods(ReflectionClass $staticReflection): array |
||
| 190 | |||
| 191 | protected static function resolveFromDocblocks(ReflectionClass $staticReflection): array |
||
| 209 | |||
| 210 | protected function resolveValueFromStaticCall(): ?string |
||
| 220 | } |
||
| 221 |
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.