Complex classes like Methods 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 Methods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class Methods |
||
| 27 | { |
||
| 28 | private const MAGIC_METHODS = [ |
||
| 29 | '__construct', |
||
| 30 | '__destruct', |
||
| 31 | '__call', |
||
| 32 | '__callStatic', |
||
| 33 | '__get', |
||
| 34 | '__set', |
||
| 35 | '__isset', |
||
| 36 | '__unset', |
||
| 37 | '__sleep', |
||
| 38 | '__wakeup', |
||
| 39 | '__toString', |
||
| 40 | '__invoke', |
||
| 41 | '__set_state', |
||
| 42 | '__debugInfo', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | private const RESERVED_UNQUALIFIED_RETURN_TYPES = ['self', 'static', 'object']; |
||
| 46 | |||
| 47 | /** @varTraverser */ |
||
| 48 | private $traverser; |
||
| 49 | |||
| 50 | /** @var Parser */ |
||
| 51 | private $parser; |
||
| 52 | |||
| 53 | /** @var array */ |
||
| 54 | private $extendedMethodsCache = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Traverser $traverser |
||
| 58 | * @param Parser|null $parser |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param ReflectionClass $reflection |
||
| 70 | * @return array |
||
| 71 | * @throws ReflectionException |
||
| 72 | */ |
||
| 73 | public function getMethods(ReflectionClass $reflection): array |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param ReflectionMethod $method |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | private function isIgnoredMethod(ReflectionMethod $method): bool |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $name |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | private function isMagicMethod(string $name): bool |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param ReflectionMethod $method |
||
| 125 | * @return int |
||
| 126 | */ |
||
| 127 | private function packFlags(ReflectionMethod $method): int |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param ReflectionMethod $method |
||
| 143 | * @return Node|null |
||
| 144 | */ |
||
| 145 | private function defineReturnType(ReflectionMethod $method): ?Node |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param ReflectionMethod $method |
||
| 158 | * @return array |
||
| 159 | * @throws ReflectionException |
||
| 160 | */ |
||
| 161 | private function packParams(ReflectionMethod $method): array |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param ReflectionParameter $parameter |
||
| 192 | * @param ReflectionMethod $method |
||
| 193 | * @return string|null |
||
| 194 | */ |
||
| 195 | private function defineParamType(ReflectionParameter $parameter, ReflectionMethod $method): ?string |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param ReflectionMethod $method |
||
| 206 | * @param ReflectionType|null $type |
||
| 207 | * @return string|null |
||
| 208 | */ |
||
| 209 | private function defineType(ReflectionMethod $method, ?ReflectionType $type): ?string |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param ReflectionMethod $method |
||
| 231 | * @param string $name |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | private function replacedSelfTypeName(ReflectionMethod $method, string $name): string |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param ReflectionType $returnType |
||
| 241 | * @param string $name |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | private function typeShouldBeQualified(ReflectionType $returnType, string $name): bool |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param ReflectionClass[] $reflections |
||
| 255 | * @return Node\Stmt\ClassMethod[] |
||
| 256 | */ |
||
| 257 | private function getExtendedMethodNodes(array $reflections): array |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $fileName |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | private function fetchReflectionMethods(string $fileName): array |
||
| 298 | } |
||
| 299 |