Complex classes like Processor 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 Processor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Processor |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $handlers = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var MapperInterface[] |
||
| 28 | */ |
||
| 29 | private $mappers = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Processor constructor. |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param mixed $object |
||
| 41 | */ |
||
| 42 | public function addHandler($object) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param MapperInterface $mapper |
||
| 54 | */ |
||
| 55 | public function addMapper(MapperInterface $mapper) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param CallSpecifier $specifier |
||
| 62 | * @return ResultSpecifier |
||
| 63 | */ |
||
| 64 | public function process(CallSpecifier $specifier): ResultSpecifier |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param CallUnit $unit |
||
| 88 | * @return AbstractResult |
||
| 89 | */ |
||
| 90 | private function handleCallUnit(CallUnit $unit): AbstractResult |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param CallNotification $unit |
||
| 104 | * @return AbstractResult |
||
| 105 | */ |
||
| 106 | private function handleNotificationUnit(CallNotification $unit): AbstractResult |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param CallError $unit |
||
| 120 | * @return AbstractResult |
||
| 121 | */ |
||
| 122 | private function handleErrorUnit(CallError $unit): AbstractResult |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $requestedMethod |
||
| 129 | * @return array |
||
| 130 | * @throws MethodNotFoundException |
||
| 131 | */ |
||
| 132 | private function getClassAndMethod(string $requestedMethod) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $class |
||
| 148 | * @param string $method |
||
| 149 | * @param array $parameters |
||
| 150 | * @return mixed |
||
| 151 | * @throws InvalidParamsException |
||
| 152 | * @throws ServerErrorException |
||
| 153 | */ |
||
| 154 | private function invoke(string $class, string $method, array $parameters) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param \ReflectionParameter[] $formalParameters Formal parameters |
||
| 177 | * @param array $parameters Parameters from request (raw) |
||
| 178 | * @return array |
||
| 179 | * @throws InvalidParamsException |
||
| 180 | */ |
||
| 181 | private function prepareActualParameters(array $formalParameters, array $parameters): array |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param array $rawParameters |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | private function isNamedParameters(array $rawParameters): bool |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $formalType |
||
| 243 | * @param mixed $value |
||
| 244 | * @return mixed |
||
| 245 | * @throws InvalidParamsException |
||
| 246 | */ |
||
| 247 | private function matchType($formalType, $value) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $type |
||
| 263 | * @param $value |
||
| 264 | * @return bool |
||
| 265 | * @throws InvalidParamsException |
||
| 266 | */ |
||
| 267 | private function isType(string $type, $value) |
||
| 289 | } |
||
| 290 |