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 | 143 | 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 | 22 | 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 | 19 | protected function handleAsControllerAction(array $callable) |
|
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * Handle as callable support callables where the method is not static. |
||
222 | * |
||
223 | * @param string|array $action base for the callable |
||
224 | * @param array $arguments optional arguments |
||
225 | * @param ContainerInjectableInterface $di container with services |
||
226 | * |
||
227 | * @return mixed as the result from the route handler. |
||
228 | */ |
||
229 | 117 | protected function handleAsCallable( |
|
259 | |||
260 | |||
261 | |||
262 | /** |
||
263 | * Load callable as a service from the $di container. |
||
264 | * |
||
265 | * @param string|array $action base for the callable |
||
266 | * @param array $arguments optional arguments |
||
267 | * @param ContainerInjectableInterface $di container with services |
||
268 | * |
||
269 | * @return mixed as the result from the route handler. |
||
270 | */ |
||
271 | 3 | protected function handleUsingDi( |
|
292 | } |
||
293 |