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 |
||
| 26 | class Router |
||
| 27 | { |
||
| 28 | use Options; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Or should be as properties? |
||
| 32 | */ |
||
| 33 | const DEFAULT_MODULE = 'index'; |
||
| 34 | const DEFAULT_CONTROLLER = 'index'; |
||
| 35 | const ERROR_MODULE = 'error'; |
||
| 36 | const ERROR_CONTROLLER = 'index'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string base URL |
||
| 40 | */ |
||
| 41 | protected $baseUrl; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string REQUEST_URI minus Base URL |
||
| 45 | */ |
||
| 46 | protected $cleanUri; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string default module |
||
| 50 | */ |
||
| 51 | protected $defaultModule = self::DEFAULT_MODULE; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string default Controller |
||
| 55 | */ |
||
| 56 | protected $defaultController = self::DEFAULT_CONTROLLER; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string error module |
||
| 60 | */ |
||
| 61 | protected $errorModule = self::ERROR_MODULE; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string error Controller |
||
| 65 | */ |
||
| 66 | protected $errorController = self::ERROR_CONTROLLER; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array instance parameters |
||
| 70 | */ |
||
| 71 | protected $params = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array instance raw parameters |
||
| 75 | */ |
||
| 76 | protected $rawParams = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array[] routers map |
||
| 80 | */ |
||
| 81 | protected $routers = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array[] reverse map |
||
| 85 | */ |
||
| 86 | protected $reverse = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Constructor of Router |
||
| 90 | */ |
||
| 91 | 554 | public function __construct() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Initial routers data from controllers |
||
| 108 | * |
||
| 109 | * @return array[] |
||
| 110 | */ |
||
| 111 | 554 | private function prepareRouterData() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Get the base URL. |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 62 | public function getBaseUrl() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Set the base URL. |
||
| 165 | * |
||
| 166 | * @param string $baseUrl |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | 554 | public function setBaseUrl($baseUrl) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Get an action parameter |
||
| 177 | * |
||
| 178 | * @param string $key |
||
| 179 | * @param mixed $default Default value to use if key not found |
||
| 180 | * |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function getParam($key, $default = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set an action parameter |
||
| 190 | * |
||
| 191 | * A $value of null will unset the $key if it exists |
||
| 192 | * |
||
| 193 | * @param string $key |
||
| 194 | * @param mixed $value |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | 5 | public function setParam($key, $value) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get parameters |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function getParams() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get raw params, w/out module and controller |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function getRawParams() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get default module |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 47 | public function getDefaultModule() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Set default module |
||
| 241 | * |
||
| 242 | * @param string $defaultModule |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | public function setDefaultModule($defaultModule) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get default controller |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 47 | public function getDefaultController() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Set default controller |
||
| 263 | * |
||
| 264 | * @param string $defaultController |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | public function setDefaultController($defaultController) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get error module |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 3 | public function getErrorModule() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Set error module |
||
| 285 | * |
||
| 286 | * @param string $errorModule |
||
| 287 | * |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | public function setErrorModule($errorModule) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get error controller |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 3 | public function getErrorController() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Set error controller |
||
| 307 | * |
||
| 308 | * @param string $errorController |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | public function setErrorController($errorController) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Build URL to controller |
||
| 319 | * |
||
| 320 | * @param string $module |
||
| 321 | * @param string $controller |
||
| 322 | * @param array $params |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 34 | public function getUrl( |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Build full URL to controller |
||
| 343 | * |
||
| 344 | * @param string $module |
||
| 345 | * @param string $controller |
||
| 346 | * @param array $params |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 1 | public function getFullUrl( |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Build URL by custom route |
||
| 367 | * |
||
| 368 | * @param string $module |
||
| 369 | * @param string $controller |
||
| 370 | * @param array $params |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 5 | protected function urlCustom($module, $controller, $params) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Build URL by default route |
||
| 406 | * |
||
| 407 | * @param string $module |
||
| 408 | * @param string $controller |
||
| 409 | * @param array $params |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | 29 | protected function urlRoute($module, $controller, $params) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Process routing |
||
| 444 | * |
||
| 445 | * @return \Bluz\Router\Router |
||
| 446 | */ |
||
| 447 | 9 | public function process() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Process default router |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | 9 | protected function processDefault(): bool |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Process custom router |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | 5 | protected function processCustom(): bool |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Process router by default rules |
||
| 494 | * |
||
| 495 | * Default routers examples |
||
| 496 | * / |
||
| 497 | * /:module/ |
||
| 498 | * /:module/:controller/ |
||
| 499 | * /:module/:controller/:key1/:value1/:key2/:value2... |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | 5 | protected function processRoute(): bool |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Reset Request |
||
| 541 | * |
||
| 542 | * @return void |
||
| 543 | */ |
||
| 544 | 9 | protected function resetRequest() |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Get the request URI without baseUrl |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | 25 | public function getCleanUri() |
|
| 581 | } |
||
| 582 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..