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 |
||
25 | class Router |
||
26 | { |
||
27 | use SingletonTrait; |
||
28 | |||
29 | protected $routing; |
||
30 | protected $slugs; |
||
31 | private $domains = []; |
||
32 | /** |
||
33 | * @var Finder $finder |
||
34 | */ |
||
35 | private $finder; |
||
36 | /** |
||
37 | * @var \PSFS\base\Cache $cache |
||
38 | */ |
||
39 | private $cache; |
||
40 | /** |
||
41 | * @var bool headersSent |
||
42 | */ |
||
43 | protected $headersSent = false; |
||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $cacheType = Cache::JSON; |
||
48 | |||
49 | /** |
||
50 | * Constructor Router |
||
51 | * @throws ConfigException |
||
52 | */ |
||
53 | 1 | public function __construct() |
|
59 | |||
60 | /** |
||
61 | * Initializer Router |
||
62 | * @throws ConfigException |
||
63 | */ |
||
64 | 1 | public function init() |
|
75 | |||
76 | /** |
||
77 | * Load routes and domains and store them |
||
78 | */ |
||
79 | 1 | private function debugLoad() { |
|
85 | |||
86 | /** |
||
87 | * Método que deriva un error HTTP de página no encontrada |
||
88 | * |
||
89 | * @param \Exception $e |
||
90 | * @param boolean $isJson |
||
91 | * |
||
92 | * @return string HTML |
||
93 | */ |
||
94 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
118 | |||
119 | /** |
||
120 | * Método que devuelve las rutas |
||
121 | * @return string|null |
||
122 | */ |
||
123 | 1 | public function getSlugs() |
|
127 | |||
128 | /** |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 2 | public function getRoutes() { |
|
134 | |||
135 | /** |
||
136 | * Method that extract all routes in the platform |
||
137 | * @return array |
||
138 | */ |
||
139 | 1 | public function getAllRoutes() |
|
149 | |||
150 | /** |
||
151 | * Método que calcula el objeto a enrutar |
||
152 | * |
||
153 | * @param string|null $route |
||
154 | * |
||
155 | * @throws \Exception |
||
156 | * @return string HTML |
||
157 | */ |
||
158 | 1 | public function execute($route) |
|
176 | |||
177 | /** |
||
178 | * Método que busca el componente que ejecuta la ruta |
||
179 | * |
||
180 | * @param string $route |
||
181 | * |
||
182 | * @throws \PSFS\base\exception\RouterException |
||
183 | */ |
||
184 | 1 | protected function searchAction($route) |
|
214 | |||
215 | /** |
||
216 | * @param array $action |
||
217 | * @param array $params |
||
218 | * @return bool |
||
219 | */ |
||
220 | private function checkRequirements(array $action, array $params = []) { |
||
234 | |||
235 | /** |
||
236 | * Método que manda las cabeceras de autenticación |
||
237 | * @return string HTML |
||
238 | */ |
||
239 | protected function sentAuthHeader() |
||
243 | |||
244 | /** |
||
245 | * @return string|null |
||
246 | */ |
||
247 | 1 | private function getExternalModules() { |
|
252 | |||
253 | /** |
||
254 | * Method that check if the project has sub project to include |
||
255 | * @param boolean $hydrateRoute |
||
256 | */ |
||
257 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
267 | |||
268 | /** |
||
269 | * Method that gather all the routes in the project |
||
270 | */ |
||
271 | 1 | private function generateRouting() |
|
286 | |||
287 | /** |
||
288 | * Método que regenera el fichero de rutas |
||
289 | * @throws ConfigException |
||
290 | */ |
||
291 | 1 | public function hydrateRouting() |
|
308 | |||
309 | /** |
||
310 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
311 | * |
||
312 | * @param string $origen |
||
313 | * @param string $namespace |
||
314 | * @param array $routing |
||
315 | * |
||
316 | * @return array |
||
317 | * @throws ConfigException |
||
318 | */ |
||
319 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
330 | |||
331 | /** |
||
332 | * Checks that a namespace exists |
||
333 | * @param string $namespace |
||
334 | * @return bool |
||
335 | */ |
||
336 | 3 | public static function exists($namespace) |
|
340 | |||
341 | /** |
||
342 | * Método que añade nuevas rutas al array de referencia |
||
343 | * |
||
344 | * @param string $namespace |
||
345 | * @param array $routing |
||
346 | * @param string $module |
||
347 | * |
||
348 | * @return array |
||
349 | * @throws ConfigException |
||
350 | */ |
||
351 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
381 | |||
382 | /** |
||
383 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
384 | * |
||
385 | * @param \ReflectionClass $class |
||
386 | * |
||
387 | * @return Router |
||
388 | * @throws ConfigException |
||
389 | */ |
||
390 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
405 | |||
406 | /** |
||
407 | * Método que genera las urls amigables para usar dentro del framework |
||
408 | * @return Router |
||
409 | */ |
||
410 | 1 | public function simpatize() |
|
420 | |||
421 | /** |
||
422 | * Método que devuelve una ruta del framework |
||
423 | * |
||
424 | * @param string $slug |
||
425 | * @param boolean $absolute |
||
426 | * @param array $params |
||
427 | * |
||
428 | * @return string|null |
||
429 | * @throws RouterException |
||
430 | */ |
||
431 | 3 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
448 | |||
449 | /** |
||
450 | * Método que devuelve las rutas de administración |
||
451 | * @deprecated |
||
452 | * @return array |
||
453 | */ |
||
454 | public function getAdminRoutes() |
||
458 | |||
459 | /** |
||
460 | * Método que devuelve le controlador del admin |
||
461 | * @deprecated |
||
462 | * @return Admin |
||
463 | */ |
||
464 | public function getAdmin() |
||
468 | |||
469 | /** |
||
470 | * Método que extrae los dominios |
||
471 | * @return array |
||
472 | */ |
||
473 | 1 | public function getDomains() |
|
477 | |||
478 | /** |
||
479 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
480 | * |
||
481 | * @param string $route |
||
482 | * @param array|null $action |
||
483 | * @param types\Controller $class |
||
484 | * @param array $params |
||
485 | */ |
||
486 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
511 | |||
512 | /** |
||
513 | * Parse slugs to create translations |
||
514 | * |
||
515 | * @param string $absoluteTranslationFileName |
||
516 | */ |
||
517 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
532 | |||
533 | /** |
||
534 | * @param bool $hydrateRoute |
||
535 | * @param $modulePath |
||
536 | * @param $externalModulePath |
||
537 | */ |
||
538 | private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
||
549 | |||
550 | /** |
||
551 | * @param $hydrateRoute |
||
552 | * @param $module |
||
553 | * @return mixed |
||
554 | */ |
||
555 | 1 | private function loadExternalModule($hydrateRoute, $module) |
|
570 | |||
571 | } |
||
572 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.