Complex classes like Router 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 Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | trait Router { |
||
22 | /** |
||
23 | * Path that will be used by controller to render page |
||
24 | * |
||
25 | * @var string[] |
||
26 | */ |
||
27 | protected $controller_path; |
||
28 | /** |
||
29 | * Execute router |
||
30 | * |
||
31 | * Depending on module, files-based or controller-based router might be used |
||
32 | * |
||
33 | * @throws ExitException |
||
34 | */ |
||
35 | protected function execute_router () { |
||
79 | /** |
||
80 | * @param string $dir |
||
81 | * @param string $module_name |
||
82 | * @param string $basename |
||
83 | * @param array $structure |
||
84 | * @param array $result |
||
85 | */ |
||
86 | protected function print_cli_structure_internal ($dir, $module_name, $basename, $structure, &$result) { |
||
112 | /** |
||
113 | * @param array $result |
||
114 | * @param string $prefix |
||
115 | * |
||
116 | * @return string[] |
||
117 | */ |
||
118 | protected function print_cli_structure_normalize_result ($result, $prefix = '') { |
||
138 | /** |
||
139 | * Normalize `cs\Request::$route_path` and fill `cs\App::$controller_path` |
||
140 | * |
||
141 | * @param Request $Request |
||
142 | * |
||
143 | * @throws ExitException |
||
144 | */ |
||
145 | protected function check_and_normalize_route ($Request) { |
||
173 | /** |
||
174 | * @param string $path |
||
175 | * @param array $structure |
||
176 | * @param bool $cli_or_api_path |
||
177 | * |
||
178 | * @throws ExitException |
||
179 | */ |
||
180 | protected function check_and_normalize_route_internal (&$path, $structure, $cli_or_api_path) { |
||
200 | /** |
||
201 | * Include files necessary for module page rendering |
||
202 | * |
||
203 | * @param Request $Request |
||
204 | * |
||
205 | * @throws ExitException |
||
206 | */ |
||
207 | protected function files_router ($Request) { |
||
219 | /** |
||
220 | * Include files that corresponds for specific paths in URL |
||
221 | * |
||
222 | * @param Request $Request |
||
223 | * @param string $dir |
||
224 | * @param string $basename |
||
225 | * @param bool $required |
||
226 | * |
||
227 | * @throws ExitException |
||
228 | */ |
||
229 | protected function files_router_handler ($Request, $dir, $basename, $required = true) { |
||
232 | /** |
||
233 | * @param Request $Request |
||
234 | * @param string $dir |
||
235 | * @param string $basename |
||
236 | * @param bool $required |
||
237 | * |
||
238 | * @throws ExitException |
||
239 | */ |
||
240 | protected function files_router_handler_internal ($Request, $dir, $basename, $required) { |
||
256 | /** |
||
257 | * @param string $dir |
||
258 | * @param string $basename |
||
259 | * |
||
260 | * @return string[] |
||
261 | */ |
||
262 | protected function files_router_available_methods ($dir, $basename) { |
||
268 | /** |
||
269 | * If HTTP method handler not found we generate either `501 Not Implemented` if other methods are supported or `404 Not Found` if handlers for others |
||
270 | * methods also doesn't exist |
||
271 | * |
||
272 | * @param string[] $available_methods |
||
273 | * @param string $request_method |
||
274 | * @param Request $Request |
||
275 | * |
||
276 | * @throws ExitException |
||
277 | */ |
||
278 | protected function handler_not_found ($available_methods, $request_method, $Request) { |
||
295 | /** |
||
296 | * Call methods necessary for module page rendering |
||
297 | * |
||
298 | * @param Request $Request |
||
299 | * |
||
300 | * @throws ExitException |
||
301 | */ |
||
302 | protected function controller_router ($Request) { |
||
323 | /** |
||
324 | * Call methods that corresponds for specific paths in URL |
||
325 | * |
||
326 | * @param Request $Request |
||
327 | * @param string $controller_class |
||
328 | * @param string $method_name |
||
329 | * @param bool $required |
||
330 | * |
||
331 | * @throws ExitException |
||
332 | */ |
||
333 | protected function controller_router_handler ($Request, $controller_class, $method_name, $required = true) { |
||
337 | /** |
||
338 | * @param Request $Request |
||
339 | * @param string $controller_class |
||
340 | * @param string $method_name |
||
341 | * @param bool $required |
||
342 | * |
||
343 | * @throws ExitException |
||
344 | */ |
||
345 | protected function controller_router_handler_internal ($Request, $controller_class, $method_name, $required) { |
||
362 | /** |
||
363 | * @param string $working_directory |
||
364 | * @param string $controller_class |
||
365 | * @param string $method_name |
||
366 | * |
||
367 | * @return string[] |
||
368 | */ |
||
369 | protected function controller_router_available_methods ($working_directory, $controller_class, $method_name) { |
||
393 | /** |
||
394 | * @param array $structure |
||
395 | * @param string $prefix |
||
396 | * |
||
397 | * @return string[] |
||
398 | */ |
||
399 | protected function controller_router_available_methods_to_flat_structure ($structure, $prefix = '') { |
||
415 | /** |
||
416 | * @param string $controller_class |
||
417 | * @param string $method_name |
||
418 | * @param Request $Request |
||
419 | * @param Response $Response |
||
420 | * |
||
421 | * @return bool |
||
422 | */ |
||
423 | protected function controller_router_handler_internal_execute ($controller_class, $method_name, $Request, $Response) { |
||
433 | } |
||
434 |