Complex classes like RouteHandler 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 RouteHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 13 | class RouteHandler | ||
| 14 | { | ||
| 15 | /** | ||
| 16 | * @var ContainerInterface $di the dependency/service container. | ||
| 17 | */ | ||
| 18 | protected $di; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | /** | ||
| 23 | * Handle the action for a route and return the results. | ||
| 24 | * | ||
| 25 | * @param string $method the request method. | ||
| 26 | * @param string $path that was matched. | ||
| 27 | * @param string|array $action base for the callable. | ||
| 28 | * @param array $arguments optional arguments. | ||
| 29 | * @param ContainerInjectableInterface $di container with services. | ||
| 30 | * | ||
| 31 | * @return mixed as the result from the route handler. | ||
| 32 | */ | ||
| 33 | 141 | public function handle( | |
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | /** | ||
| 73 | * Get an informative string representing the handler type. | ||
| 74 | * | ||
| 75 | * @param string|array $action base for the callable. | ||
| 76 | * @param ContainerInjectableInterface $di container with services. | ||
| 77 | * | ||
| 78 | * @return string as the type of handler. | ||
| 79 | */ | ||
| 80 | public function getHandlerType( | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | /** | ||
| 116 | * Check if items can be used to call a controller action, verify | ||
| 117 | * that the controller exists, the action has a class-method to call. | ||
| 118 | * | ||
| 119 | * @param string $method the request method. | ||
| 120 | * @param string $path the matched path, base for the controller action | ||
| 121 | * and the arguments. | ||
| 122 | * @param string $class the controller class | ||
| 123 | * | ||
| 124 | * @return array with callable details. | ||
| 125 | */ | ||
| 126 | 20 | protected function isControllerAction( | |
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | /** | ||
| 158 | * Call the controller action with optional arguments and call | ||
| 159 | * initialisation methods if available. | ||
| 160 | * | ||
| 161 | * @param string $callable with details on what controller action to call. | ||
| 162 | * | ||
| 163 | * @return mixed result from the handler. | ||
| 164 | */ | ||
| 165 | 18 | protected function handleAsControllerAction(array $callable) | |
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | /** | ||
| 200 | * Handle as callable support callables where the method is not static. | ||
| 201 | * | ||
| 202 | * @param string|array $action base for the callable | ||
| 203 | * @param array $arguments optional arguments | ||
| 204 | * @param ContainerInjectableInterface $di container with services | ||
| 205 | * | ||
| 206 | * @return mixed as the result from the route handler. | ||
| 207 | */ | ||
| 208 | 116 | protected function handleAsCallable( | |
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | /** | ||
| 242 | * Load callable as a service from the $di container. | ||
| 243 | * | ||
| 244 | * @param string|array $action base for the callable | ||
| 245 | * @param array $arguments optional arguments | ||
| 246 | * @param ContainerInjectableInterface $di container with services | ||
| 247 | * | ||
| 248 | * @return mixed as the result from the route handler. | ||
| 249 | */ | ||
| 250 | 3 | protected function handleUsingDi( | |
| 271 | } | ||
| 272 |