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 |
||
| 27 | final class Methods |
||
| 28 | { |
||
| 29 | private const MAGIC_METHODS = [ |
||
| 30 | '__construct', |
||
| 31 | '__destruct', |
||
| 32 | '__call', |
||
| 33 | '__callStatic', |
||
| 34 | '__get', |
||
| 35 | '__set', |
||
| 36 | '__isset', |
||
| 37 | '__unset', |
||
| 38 | '__sleep', |
||
| 39 | '__wakeup', |
||
| 40 | '__toString', |
||
| 41 | '__invoke', |
||
| 42 | '__set_state', |
||
| 43 | '__debugInfo', |
||
| 44 | ]; |
||
| 45 | |||
| 46 | private const RESERVED_UNQUALIFIED_RETURN_TYPES = ['self', 'static', 'object']; |
||
| 47 | |||
| 48 | /** @varTraverser */ |
||
| 49 | private $traverser; |
||
| 50 | |||
| 51 | /** @var Parser */ |
||
| 52 | private $parser; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | private $extendedMethodsCache = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Traverser $traverser |
||
| 59 | * @param Parser|null $parser |
||
| 60 | */ |
||
| 61 | public function __construct( |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param ReflectionClass $reflection |
||
| 71 | * @return array |
||
| 72 | * @throws ReflectionException |
||
| 73 | */ |
||
| 74 | public function getMethods(ReflectionClass $reflection): array |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param ReflectionMethod $method |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | private function isIgnoredMethod(ReflectionMethod $method): bool |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $name |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | private function isMagicMethod(string $name): bool |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param ReflectionMethod $method |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | private function packFlags(ReflectionMethod $method): int |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param ReflectionMethod $method |
||
| 144 | * @return Node|null |
||
| 145 | */ |
||
| 146 | private function defineReturnType(ReflectionMethod $method): ?Node |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param ReflectionMethod $method |
||
| 159 | * @return array |
||
| 160 | * @throws ReflectionException |
||
| 161 | */ |
||
| 162 | private function packParams(ReflectionMethod $method): array |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param ReflectionParameter $parameter |
||
| 193 | * @param ReflectionMethod $method |
||
| 194 | * @return string|null |
||
| 195 | */ |
||
| 196 | private function defineParamType(ReflectionParameter $parameter, ReflectionMethod $method): ?string |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param ReflectionMethod $method |
||
| 207 | * @param ReflectionType|null $type |
||
| 208 | * @return string|null |
||
| 209 | */ |
||
| 210 | private function defineType(ReflectionMethod $method, ?ReflectionType $type): ?string |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param ReflectionMethod $method |
||
| 234 | * @param ReflectionNamedType $type |
||
| 235 | * @return string|null |
||
| 236 | */ |
||
| 237 | private function defineSingularType(ReflectionMethod $method, ReflectionNamedType $type): ?string |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param ReflectionMethod $method |
||
| 255 | * @param string $name |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | private function replacedSelfTypeName(ReflectionMethod $method, string $name): string |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param ReflectionType $returnType |
||
| 265 | * @param string $name |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | private function typeShouldBeQualified(ReflectionType $returnType, string $name): bool |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param ReflectionClass[] $reflections |
||
| 279 | * @return Node\Stmt\ClassMethod[] |
||
| 280 | */ |
||
| 281 | private function getExtendedMethodNodes(array $reflections): array |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $fileName |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | private function fetchReflectionMethods(string $fileName): array |
||
| 322 | } |
||
| 323 |