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 | private const DEFAULT_MODULE = 'index'; |
||
34 | private const DEFAULT_CONTROLLER = 'index'; |
||
35 | private const ERROR_MODULE = 'error'; |
||
36 | private 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 | 604 | public function __construct() |
|
105 | |||
106 | /** |
||
107 | * Initial routers data from controllers |
||
108 | * |
||
109 | * @return array[] |
||
110 | * @throws \Bluz\Common\Exception\CommonException |
||
111 | * @throws \Bluz\Common\Exception\ComponentException |
||
112 | * @throws \Bluz\Controller\ControllerException |
||
113 | * @throws \ReflectionException |
||
114 | */ |
||
115 | 604 | private function prepareRouterData() |
|
156 | |||
157 | /** |
||
158 | * Get the base URL. |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 71 | public function getBaseUrl(): string |
|
166 | |||
167 | /** |
||
168 | * Set the base URL. |
||
169 | * |
||
170 | * @param string $baseUrl |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | 604 | public function setBaseUrl($baseUrl): void |
|
178 | |||
179 | /** |
||
180 | * Get an action parameter |
||
181 | * |
||
182 | * @param string $key |
||
183 | * @param mixed $default Default value to use if key not found |
||
184 | * |
||
185 | * @return mixed |
||
186 | */ |
||
187 | public function getParam($key, $default = null) |
||
191 | |||
192 | /** |
||
193 | * Set an action parameter |
||
194 | * |
||
195 | * A $value of null will unset the $key if it exists |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @param mixed $value |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | 5 | public function setParam($key, $value): void |
|
212 | |||
213 | /** |
||
214 | * Get parameters |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getParams(): array |
||
222 | |||
223 | /** |
||
224 | * Get raw params, w/out module and controller |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | public function getRawParams(): array |
||
232 | |||
233 | /** |
||
234 | * Get default module |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 48 | public function getDefaultModule(): string |
|
242 | |||
243 | /** |
||
244 | * Set default module |
||
245 | * |
||
246 | * @param string $defaultModule |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | public function setDefaultModule($defaultModule): void |
||
254 | |||
255 | /** |
||
256 | * Get default controller |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | 48 | public function getDefaultController(): string |
|
264 | |||
265 | /** |
||
266 | * Set default controller |
||
267 | * |
||
268 | * @param string $defaultController |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | public function setDefaultController($defaultController): void |
||
276 | |||
277 | /** |
||
278 | * Get error module |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | 3 | public function getErrorModule(): string |
|
286 | |||
287 | /** |
||
288 | * Set error module |
||
289 | * |
||
290 | * @param string $errorModule |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | public function setErrorModule($errorModule): void |
||
298 | |||
299 | /** |
||
300 | * Get error controller |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 3 | public function getErrorController(): string |
|
308 | |||
309 | /** |
||
310 | * Set error controller |
||
311 | * |
||
312 | * @param string $errorController |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | public function setErrorController($errorController): void |
||
320 | |||
321 | /** |
||
322 | * Get the request URI without baseUrl |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 34 | public function getCleanUri(): string |
|
337 | |||
338 | /** |
||
339 | * Build URL to controller |
||
340 | * |
||
341 | * @param string $module |
||
342 | * @param string $controller |
||
343 | * @param array $params |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | 34 | public function getUrl( |
|
361 | |||
362 | /** |
||
363 | * Build full URL to controller |
||
364 | * |
||
365 | * @param string $module |
||
366 | * @param string $controller |
||
367 | * @param array $params |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | 4 | public function getFullUrl( |
|
385 | |||
386 | /** |
||
387 | * Build URL by custom route |
||
388 | * |
||
389 | * @param string $module |
||
390 | * @param string $controller |
||
391 | * @param array $params |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | 5 | protected function urlCustom($module, $controller, $params): string |
|
424 | |||
425 | /** |
||
426 | * Build URL by default route |
||
427 | * |
||
428 | * @param string $module |
||
429 | * @param string $controller |
||
430 | * @param array $params |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | 29 | protected function urlRoute($module, $controller, $params): string |
|
460 | |||
461 | /** |
||
462 | * Process routing |
||
463 | * |
||
464 | * @return \Bluz\Router\Router |
||
465 | */ |
||
466 | 9 | public function process() |
|
475 | |||
476 | /** |
||
477 | * Process default router |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | 9 | protected function processDefault(): bool |
|
486 | |||
487 | /** |
||
488 | * Process custom router |
||
489 | * |
||
490 | * @return bool |
||
491 | */ |
||
492 | 5 | protected function processCustom(): bool |
|
510 | |||
511 | /** |
||
512 | * Process router by default rules |
||
513 | * |
||
514 | * Default routers examples |
||
515 | * / |
||
516 | * /:module/ |
||
517 | * /:module/:controller/ |
||
518 | * /:module/:controller/:key1/:value1/:key2/:value2... |
||
519 | * |
||
520 | * @return bool |
||
521 | */ |
||
522 | 5 | protected function processRoute(): bool |
|
557 | |||
558 | /** |
||
559 | * Reset Request |
||
560 | * |
||
561 | * @return void |
||
562 | */ |
||
563 | 9 | protected function resetRequest(): void |
|
583 | } |
||
584 |
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..