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 |
||
| 18 | trait Router { |
||
| 19 | /** |
||
| 20 | * Path that will be used by controller to render page |
||
| 21 | * |
||
| 22 | * @var string[] |
||
| 23 | */ |
||
| 24 | protected $controller_path; |
||
| 25 | /** |
||
| 26 | * Execute router |
||
| 27 | * |
||
| 28 | * Depending on module, files-based or controller-based router might be used |
||
| 29 | * |
||
| 30 | * @throws ExitException |
||
| 31 | */ |
||
| 32 | protected function execute_router () { |
||
| 41 | /** |
||
| 42 | * Normalize `cs\Request::$route_path` and fill `cs\App::$controller_path` |
||
| 43 | * |
||
| 44 | * @param Request $Request |
||
| 45 | * |
||
| 46 | * @throws ExitException |
||
| 47 | */ |
||
| 48 | protected function check_and_normalize_route ($Request) { |
||
| 76 | /** |
||
| 77 | * @param string $path |
||
| 78 | * @param array $structure |
||
| 79 | * @param bool $api_path |
||
| 80 | * |
||
| 81 | * @throws ExitException |
||
| 82 | */ |
||
| 83 | protected function check_and_normalize_route_internal (&$path, $structure, $api_path) { |
||
| 103 | /** |
||
| 104 | * Include files necessary for module page rendering |
||
| 105 | * |
||
| 106 | * @param Request $Request |
||
| 107 | * |
||
| 108 | * @throws ExitException |
||
| 109 | */ |
||
| 110 | protected function files_router ($Request) { |
||
| 122 | /** |
||
| 123 | * Include files that corresponds for specific paths in URL |
||
| 124 | * |
||
| 125 | * @param Request $Request |
||
| 126 | * @param string $dir |
||
| 127 | * @param string $basename |
||
| 128 | * @param bool $required |
||
| 129 | * |
||
| 130 | * @throws ExitException |
||
| 131 | */ |
||
| 132 | protected function files_router_handler ($Request, $dir, $basename, $required = true) { |
||
| 135 | /** |
||
| 136 | * @param Request $Request |
||
| 137 | * @param string $dir |
||
| 138 | * @param string $basename |
||
| 139 | * @param bool $required |
||
| 140 | * |
||
| 141 | * @throws ExitException |
||
| 142 | */ |
||
| 143 | protected function files_router_handler_internal ($Request, $dir, $basename, $required) { |
||
| 157 | /** |
||
| 158 | * 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 |
||
| 159 | * methods also doesn't exist |
||
| 160 | * |
||
| 161 | * @param string[] $available_methods |
||
| 162 | * @param string $request_method |
||
| 163 | * |
||
| 164 | * @throws ExitException |
||
| 165 | */ |
||
| 166 | protected function handler_not_found ($available_methods, $request_method) { |
||
| 176 | /** |
||
| 177 | * Call methods necessary for module page rendering |
||
| 178 | * |
||
| 179 | * @param Request $Request |
||
| 180 | * |
||
| 181 | * @throws ExitException |
||
| 182 | */ |
||
| 183 | protected function controller_router ($Request) { |
||
| 202 | /** |
||
| 203 | * Call methods that corresponds for specific paths in URL |
||
| 204 | * |
||
| 205 | * @param Request $Request |
||
| 206 | * @param string $controller_class |
||
| 207 | * @param string $method_name |
||
| 208 | * @param bool $required |
||
| 209 | * |
||
| 210 | * @throws ExitException |
||
| 211 | */ |
||
| 212 | protected function controller_router_handler ($Request, $controller_class, $method_name, $required = true) { |
||
| 216 | /** |
||
| 217 | * @param Request $Request |
||
| 218 | * @param string $controller_class |
||
| 219 | * @param string $method_name |
||
| 220 | * @param bool $required |
||
| 221 | * |
||
| 222 | * @throws ExitException |
||
| 223 | */ |
||
| 224 | protected function controller_router_handler_internal ($Request, $controller_class, $method_name, $required) { |
||
| 244 | /** |
||
| 245 | * @param string $controller_class |
||
| 246 | * @param string $method_name |
||
| 247 | * @param Request $Request |
||
| 248 | * @param Response $Response |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | protected function controller_router_handler_internal_execute ($controller_class, $method_name, $Request, $Response) { |
||
| 262 | } |
||
| 263 |