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 | private static $nodesByFileNameCache = []; |
||
| 48 | |||
| 49 | /** @varTraverser */ |
||
| 50 | private $traverser; |
||
| 51 | |||
| 52 | /** @var Parser */ |
||
| 53 | private $parser; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param Traverser $traverser |
||
| 57 | * @param Parser|null $parser |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param ReflectionClass $reflection |
||
| 69 | * @return array |
||
| 70 | * @throws ReflectionException |
||
| 71 | */ |
||
| 72 | public function getMethods(ReflectionClass $reflection): array |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param ReflectionMethod $method |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | private function isIgnoredMethod(ReflectionMethod $method): bool |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $name |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | private function isMagicMethod(string $name): bool |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param ReflectionMethod $method |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | private function packFlags(ReflectionMethod $method): int |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param ReflectionMethod $method |
||
| 142 | * @return Node|null |
||
| 143 | */ |
||
| 144 | private function defineReturnType(ReflectionMethod $method): ?Node |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param ReflectionMethod $method |
||
| 157 | * @return array |
||
| 158 | * @throws ReflectionException |
||
| 159 | */ |
||
| 160 | private function packParams(ReflectionMethod $method): array |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param ReflectionParameter $parameter |
||
| 191 | * @param ReflectionMethod $method |
||
| 192 | * @return string|null |
||
| 193 | */ |
||
| 194 | private function defineParamType(ReflectionParameter $parameter, ReflectionMethod $method): ?string |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param ReflectionMethod $method |
||
| 205 | * @param ReflectionType|null $type |
||
| 206 | * @return string|null |
||
| 207 | */ |
||
| 208 | private function defineType(ReflectionMethod $method, ?ReflectionType $type): ?string |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param ReflectionMethod $method |
||
| 230 | * @param string $name |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | private function replacedSelfTypeName(ReflectionMethod $method, string $name): string |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param ReflectionType $returnType |
||
| 240 | * @param string $name |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | private function typeShouldBeQualified(ReflectionType $returnType, string $name): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param ReflectionClass[] $reflections |
||
| 254 | * @return Node\Stmt\ClassMethod[] |
||
| 255 | */ |
||
| 256 | private function getExtendedMethodNodes(array $reflections): array |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $fileName |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | private function getMethodsByFile(string $fileName): array |
||
| 296 | |||
| 297 | public static function resetNodesCache(): void |
||
| 301 | } |
||
| 302 |