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 () { |
||
83 | /** |
||
84 | * @param string $dir |
||
85 | * @param string $module_name |
||
86 | * @param string $basename |
||
87 | * @param array $structure |
||
88 | * @param array $result |
||
89 | */ |
||
90 | protected function print_cli_structure_internal ($dir, $module_name, $basename, $structure, &$result) { |
||
115 | /** |
||
116 | * @param array $result |
||
117 | * @param string $prefix |
||
118 | * |
||
119 | * @return string[] |
||
120 | */ |
||
121 | protected function print_cli_structure_normalize_result ($result, $prefix = '') { |
||
141 | /** |
||
142 | * Normalize `cs\Request::$route_path` and fill `cs\App::$controller_path` |
||
143 | * |
||
144 | * @param Request $Request |
||
145 | * |
||
146 | * @throws ExitException |
||
147 | */ |
||
148 | protected function check_and_normalize_route ($Request) { |
||
176 | /** |
||
177 | * @param string $path |
||
178 | * @param array $structure |
||
179 | * @param bool $cli_or_api_path |
||
180 | * |
||
181 | * @throws ExitException |
||
182 | */ |
||
183 | protected function check_and_normalize_route_internal (&$path, $structure, $cli_or_api_path) { |
||
203 | /** |
||
204 | * Include files necessary for module page rendering |
||
205 | * |
||
206 | * @param Request $Request |
||
207 | * |
||
208 | * @throws ExitException |
||
209 | */ |
||
210 | protected function files_router ($Request) { |
||
222 | /** |
||
223 | * Include files that corresponds for specific paths in URL |
||
224 | * |
||
225 | * @param Request $Request |
||
226 | * @param string $dir |
||
227 | * @param string $basename |
||
228 | * @param bool $required |
||
229 | * |
||
230 | * @throws ExitException |
||
231 | */ |
||
232 | protected function files_router_handler ($Request, $dir, $basename, $required = true) { |
||
235 | /** |
||
236 | * @param Request $Request |
||
237 | * @param string $dir |
||
238 | * @param string $basename |
||
239 | * @param bool $required |
||
240 | * |
||
241 | * @throws ExitException |
||
242 | */ |
||
243 | protected function files_router_handler_internal ($Request, $dir, $basename, $required) { |
||
259 | /** |
||
260 | * @param string $dir |
||
261 | * @param string $basename |
||
262 | * |
||
263 | * @return string[] |
||
264 | */ |
||
265 | protected function files_router_available_methods ($dir, $basename) { |
||
271 | /** |
||
272 | * 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 |
||
273 | * methods also doesn't exist |
||
274 | * |
||
275 | * @param string[] $available_methods |
||
276 | * @param string $request_method |
||
277 | * @param Request $Request |
||
278 | * |
||
279 | * @throws ExitException |
||
280 | */ |
||
281 | protected function handler_not_found ($available_methods, $request_method, $Request) { |
||
296 | /** |
||
297 | * Call methods necessary for module page rendering |
||
298 | * |
||
299 | * @param Request $Request |
||
300 | * |
||
301 | * @throws ExitException |
||
302 | */ |
||
303 | protected function controller_router ($Request) { |
||
324 | /** |
||
325 | * Call methods that corresponds for specific paths in URL |
||
326 | * |
||
327 | * @param Request $Request |
||
328 | * @param string $controller_class |
||
329 | * @param string $method_name |
||
330 | * @param bool $required |
||
331 | * |
||
332 | * @throws ExitException |
||
333 | */ |
||
334 | protected function controller_router_handler ($Request, $controller_class, $method_name, $required = true) { |
||
338 | /** |
||
339 | * @param Request $Request |
||
340 | * @param string $controller_class |
||
341 | * @param string $method_name |
||
342 | * @param bool $required |
||
343 | * |
||
344 | * @throws ExitException |
||
345 | */ |
||
346 | protected function controller_router_handler_internal ($Request, $controller_class, $method_name, $required) { |
||
363 | /** |
||
364 | * @param string $working_directory |
||
365 | * @param string $controller_class |
||
366 | * @param string $method_name |
||
367 | * |
||
368 | * @return string[] |
||
369 | */ |
||
370 | protected function controller_router_available_methods ($working_directory, $controller_class, $method_name) { |
||
394 | /** |
||
395 | * @param array $structure |
||
396 | * @param string $prefix |
||
397 | * |
||
398 | * @return string[] |
||
399 | */ |
||
400 | protected function controller_router_available_methods_to_flat_structure ($structure, $prefix = '') { |
||
416 | /** |
||
417 | * @param string $controller_class |
||
418 | * @param string $method_name |
||
419 | * @param Request $Request |
||
420 | * @param Response $Response |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | protected function controller_router_handler_internal_execute ($controller_class, $method_name, $Request, $Response) { |
||
434 | } |
||
435 |