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 | 2 | 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( |
|
155 | |||
156 | |||
157 | |||
158 | /** |
||
159 | * Call the controller action with optional arguments and call |
||
160 | * initialisation methods if available. |
||
161 | * |
||
162 | * @param string $callable with details on what controller action to call. |
||
163 | * |
||
164 | * @return mixed result from the handler. |
||
165 | */ |
||
166 | 17 | protected function handleAsControllerAction(array $callable) |
|
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Handle as callable support callables where the method is not static. |
||
232 | * |
||
233 | * @param string|array $action base for the callable |
||
234 | * @param array $arguments optional arguments |
||
235 | * @param ContainerInjectableInterface $di container with services |
||
236 | * |
||
237 | * @return mixed as the result from the route handler. |
||
238 | */ |
||
239 | 117 | protected function handleAsCallable( |
|
269 | |||
270 | |||
271 | |||
272 | /** |
||
273 | * Load callable as a service from the $di container. |
||
274 | * |
||
275 | * @param string|array $action base for the callable |
||
276 | * @param array $arguments optional arguments |
||
277 | * @param ContainerInjectableInterface $di container with services |
||
278 | * |
||
279 | * @return mixed as the result from the route handler. |
||
280 | */ |
||
281 | 3 | protected function handleUsingDi( |
|
302 | } |
||
303 |