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 | * @var bool |
||
50 | */ |
||
51 | protected $loaded = false; |
||
52 | 1 | ||
53 | /** |
||
54 | 1 | * Constructor Router |
|
55 | 1 | * @throws ConfigException |
|
56 | 1 | */ |
|
57 | 1 | public function __construct() |
|
63 | 1 | ||
64 | /** |
||
65 | 1 | * Inicializador Router |
|
66 | 1 | * @throws ConfigException |
|
67 | 1 | */ |
|
68 | 1 | public function init() |
|
79 | 1 | ||
80 | 1 | /** |
|
81 | 1 | * Load routes and domains and store them |
|
82 | */ |
||
83 | private function debugLoad() { |
||
89 | |||
90 | /** |
||
91 | * Método que deriva un error HTTP de página no encontrada |
||
92 | * |
||
93 | * @param \Exception $e |
||
94 | * @param boolean $isJson |
||
95 | * |
||
96 | * @return string HTML |
||
97 | */ |
||
98 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
122 | 1 | ||
123 | /** |
||
124 | 1 | * Método que devuelve las rutas |
|
125 | * @return string|null |
||
126 | */ |
||
127 | public function getSlugs() |
||
131 | 2 | ||
132 | /** |
||
133 | * @return mixed |
||
134 | */ |
||
135 | public function getRoutes() { |
||
138 | 1 | ||
139 | /** |
||
140 | 1 | * Method that extract all routes in the platform |
|
141 | 1 | * @return array |
|
142 | 1 | */ |
|
143 | 1 | public function getAllRoutes() |
|
153 | |||
154 | /** |
||
155 | * Método que calcula el objeto a enrutar |
||
156 | * |
||
157 | 1 | * @param string|null $route |
|
158 | * |
||
159 | 1 | * @throws \Exception |
|
160 | * @return string HTML |
||
161 | */ |
||
162 | 1 | public function execute($route) |
|
180 | |||
181 | /** |
||
182 | * Método que busca el componente que ejecuta la ruta |
||
183 | * |
||
184 | * @param string $route |
||
185 | * |
||
186 | * @throws \PSFS\base\exception\RouterException |
||
187 | 1 | */ |
|
188 | protected function searchAction($route) |
||
218 | |||
219 | /** |
||
220 | * @param array $action |
||
221 | * @param array $params |
||
222 | * @return bool |
||
223 | */ |
||
224 | 1 | private function checkRequirements(array $action, array $params = []) { |
|
241 | |||
242 | /** |
||
243 | * Método que manda las cabeceras de autenticación |
||
244 | * @return string HTML |
||
245 | */ |
||
246 | protected function sentAuthHeader() |
||
250 | |||
251 | /** |
||
252 | * @return string|null |
||
253 | */ |
||
254 | private function getExternalModules() { |
||
261 | 1 | ||
262 | /** |
||
263 | * Method that check if the proyect has sub project to include |
||
264 | * @param boolean $hydrateRoute |
||
265 | */ |
||
266 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
292 | 1 | ||
293 | 1 | /** |
|
294 | 1 | * Method that gather all the routes in the project |
|
295 | */ |
||
296 | private function generateRouting() |
||
311 | |||
312 | /** |
||
313 | * Método que regenera el fichero de rutas |
||
314 | 1 | * @throws ConfigException |
|
315 | */ |
||
316 | 1 | public function hydrateRouting() |
|
333 | 1 | ||
334 | /** |
||
335 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
336 | * |
||
337 | * @param string $origen |
||
338 | * @param string $namespace |
||
339 | * @param array $routing |
||
340 | * |
||
341 | * @return array |
||
342 | * @throws ConfigException |
||
343 | */ |
||
344 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
||
355 | 1 | ||
356 | /** |
||
357 | * Checks that a namespace exists |
||
358 | 1 | * @param string $namespace |
|
359 | 1 | * @return bool |
|
360 | 1 | */ |
|
361 | public static function exists($namespace) |
||
365 | |||
366 | /** |
||
367 | * Método que añade nuevas rutas al array de referencia |
||
368 | * |
||
369 | * @param string $namespace |
||
370 | * @param array $routing |
||
371 | 1 | * @param string $module |
|
372 | * |
||
373 | * @return array |
||
374 | * @throws ConfigException |
||
375 | */ |
||
376 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
||
403 | |||
404 | 1 | /** |
|
405 | 1 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
|
406 | 1 | * |
|
407 | 1 | * @param \ReflectionClass $class |
|
408 | 1 | * |
|
409 | * @return Router |
||
410 | 1 | * @throws ConfigException |
|
411 | */ |
||
412 | protected function extractDomain(\ReflectionClass $class) |
||
427 | |||
428 | 1 | /** |
|
429 | * Método que genera las urls amigables para usar dentro del framework |
||
430 | * @return Router |
||
431 | 1 | */ |
|
432 | 1 | public function simpatize() |
|
442 | |||
443 | /** |
||
444 | * Método que devuelve una ruta del framework |
||
445 | * |
||
446 | * @param string $slug |
||
447 | * @param boolean $absolute |
||
448 | * @param array $params |
||
449 | * |
||
450 | * @return string|null |
||
451 | * @throws RouterException |
||
452 | */ |
||
453 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
||
470 | |||
471 | /** |
||
472 | * Método que devuelve las rutas de administración |
||
473 | * @deprecated |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getAdminRoutes() |
||
480 | |||
481 | /** |
||
482 | * Método que devuelve le controlador del admin |
||
483 | * @deprecated |
||
484 | * @return Admin |
||
485 | */ |
||
486 | public function getAdmin() |
||
490 | |||
491 | /** |
||
492 | * Método que extrae los dominios |
||
493 | * @return array |
||
494 | */ |
||
495 | public function getDomains() |
||
499 | |||
500 | /** |
||
501 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
502 | * |
||
503 | * @param string $route |
||
504 | * @param array|null $action |
||
505 | * @param types\Controller $class |
||
506 | * @param array $params |
||
507 | */ |
||
508 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
533 | |||
534 | /** |
||
535 | * Parse slugs to create translations |
||
536 | * |
||
537 | * @param string $absoluteTranslationFileName |
||
538 | */ |
||
539 | private function generateSlugs($absoluteTranslationFileName) |
||
557 | |||
558 | } |
||
559 |
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.