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 | 713 | public function __construct() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Initial routers data from controllers |
||
| 108 | * |
||
| 109 | * @return array[] |
||
| 110 | */ |
||
| 111 | 713 | private function prepareRouterData() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Get the base URL. |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 53 | public function getBaseUrl() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Set the base URL. |
||
| 165 | * |
||
| 166 | * @param string $baseUrl |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | 713 | public function setBaseUrl($baseUrl) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get an action parameter |
||
| 176 | * |
||
| 177 | * @param string $key |
||
| 178 | * @param mixed $default Default value to use if key not found |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | public function getParam($key, $default = null) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set an action parameter |
||
| 188 | * |
||
| 189 | * A $value of null will unset the $key if it exists |
||
| 190 | * |
||
| 191 | * @param string $key |
||
| 192 | * @param mixed $value |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | 5 | public function setParam($key, $value) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Get parameters |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function getParams() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get raw params, w/out module and controller |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getRawParams() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get default module |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | 44 | public function getDefaultModule() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Set default module |
||
| 238 | * |
||
| 239 | * @param string $defaultModule |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function setDefaultModule($defaultModule) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get default controller |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 44 | public function getDefaultController() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Set default controller |
||
| 259 | * |
||
| 260 | * @param string $defaultController |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function setDefaultController($defaultController) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get error module |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 3 | public function getErrorModule() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Set error module |
||
| 280 | * |
||
| 281 | * @param string $errorModule |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function setErrorModule($errorModule) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get error controller |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 3 | public function getErrorController() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Set error controller |
||
| 301 | * |
||
| 302 | * @param string $errorController |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function setErrorController($errorController) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Build URL to controller |
||
| 312 | * |
||
| 313 | * @param string $module |
||
| 314 | * @param string $controller |
||
| 315 | * @param array $params |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 34 | public function getUrl( |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Build full URL to controller |
||
| 335 | * |
||
| 336 | * @param string $module |
||
| 337 | * @param string $controller |
||
| 338 | * @param array $params |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | 1 | public function getFullUrl( |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Build URL by custom route |
||
| 354 | * |
||
| 355 | * @param string $module |
||
| 356 | * @param string $controller |
||
| 357 | * @param array $params |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 5 | protected function urlCustom($module, $controller, $params) |
|
| 361 | { |
||
| 362 | 5 | $url = $this->reverse[$module][$controller]['route']; |
|
| 363 | |||
| 364 | 5 | $getParams = []; |
|
| 365 | 5 | foreach ($params as $key => $value) { |
|
| 366 | // sub-array as GET params |
||
| 367 | 4 | if (is_array($value)) { |
|
| 368 | 1 | $getParams[$key] = $value; |
|
| 369 | 1 | continue; |
|
| 370 | } |
||
| 371 | 3 | $url = str_replace('{$' . $key . '}', $value, $url, $replaced); |
|
| 372 | // if not replaced, setup param as GET |
||
| 373 | 3 | if (!$replaced) { |
|
| 374 | $getParams[$key] = $value; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | // clean optional params |
||
| 378 | 5 | $url = preg_replace('/\{\$[a-z0-9-_]+\}/i', '', $url); |
|
| 379 | // clean regular expression (.*) |
||
| 380 | 5 | $url = preg_replace('/\(\.\*\)/', '', $url); |
|
| 381 | // replace "//" with "/" |
||
| 382 | 5 | $url = str_replace('//', '/', $url); |
|
| 383 | |||
| 384 | 5 | if (!empty($getParams)) { |
|
| 385 | 1 | $url .= '?' . http_build_query($getParams); |
|
| 386 | } |
||
| 387 | 5 | return $this->getBaseUrl() . ltrim($url, '/'); |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Build URL by default route |
||
| 392 | * |
||
| 393 | * @param string $module |
||
| 394 | * @param string $controller |
||
| 395 | * @param array $params |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 29 | protected function urlRoute($module, $controller, $params) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Process routing |
||
| 429 | * |
||
| 430 | * @return \Bluz\Router\Router |
||
| 431 | */ |
||
| 432 | 6 | public function process() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Process default router |
||
| 444 | * |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | 6 | protected function processDefault() : bool |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Process custom router |
||
| 455 | * |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | 5 | protected function processCustom() : bool |
|
| 459 | { |
||
| 460 | 5 | $uri = '/' . $this->getCleanUri(); |
|
| 461 | 5 | foreach ($this->routers as $router) { |
|
| 462 | 5 | if (preg_match($router['pattern'], $uri, $matches)) { |
|
| 463 | $this->setParam('_module', $router['module']); |
||
| 464 | $this->setParam('_controller', $router['controller']); |
||
| 465 | |||
| 466 | foreach ($router['params'] as $param => $type) { |
||
| 467 | if (isset($matches[$param])) { |
||
| 468 | $this->setParam($param, $matches[$param]); |
||
| 469 | } |
||
| 470 | } |
||
| 471 | return true; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | 5 | return false; |
|
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Process router by default rules |
||
| 479 | * |
||
| 480 | * Default routers examples |
||
| 481 | * / |
||
| 482 | * /:module/ |
||
| 483 | * /:module/:controller/ |
||
| 484 | * /:module/:controller/:key1/:value1/:key2/:value2... |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | 5 | protected function processRoute() : bool |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Reset Request |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 6 | protected function resetRequest() |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Get the request URI without baseUrl |
||
| 552 | * |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | 17 | public function getCleanUri() |
|
| 566 | } |
||
| 567 |
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..