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 SingletonTrait; |
||
29 | |||
30 | protected $routing; |
||
31 | protected $slugs; |
||
32 | private $domains = []; |
||
33 | /** |
||
34 | * @var Finder $finder |
||
35 | */ |
||
36 | private $finder; |
||
37 | /** |
||
38 | * @var \PSFS\base\Cache $cache |
||
39 | */ |
||
40 | private $cache; |
||
41 | /** |
||
42 | * @var bool headersSent |
||
43 | */ |
||
44 | protected $headersSent = false; |
||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $cacheType = Cache::JSON; |
||
49 | |||
50 | /** |
||
51 | * Constructor Router |
||
52 | * @throws ConfigException |
||
53 | */ |
||
54 | 1 | public function __construct() |
|
60 | |||
61 | /** |
||
62 | * Inicializador Router |
||
63 | * @throws ConfigException |
||
64 | */ |
||
65 | 1 | public function init() |
|
76 | |||
77 | /** |
||
78 | * Load routes and domains and store them |
||
79 | */ |
||
80 | 1 | private function debugLoad() { |
|
86 | |||
87 | /** |
||
88 | * Método que deriva un error HTTP de página no encontrada |
||
89 | * |
||
90 | * @param \Exception $e |
||
91 | * @param boolean $isJson |
||
92 | * |
||
93 | * @return string HTML |
||
94 | */ |
||
95 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
119 | |||
120 | /** |
||
121 | * Método que devuelve las rutas |
||
122 | * @return string|null |
||
123 | */ |
||
124 | 1 | public function getSlugs() |
|
128 | |||
129 | /** |
||
130 | * @return mixed |
||
131 | */ |
||
132 | 2 | public function getRoutes() { |
|
135 | |||
136 | /** |
||
137 | * Method that extract all routes in the platform |
||
138 | * @return array |
||
139 | */ |
||
140 | 1 | public function getAllRoutes() |
|
150 | |||
151 | /** |
||
152 | * Método que calcula el objeto a enrutar |
||
153 | * |
||
154 | * @param string|null $route |
||
155 | * |
||
156 | * @throws \Exception |
||
157 | * @return string HTML |
||
158 | */ |
||
159 | 1 | public function execute($route) |
|
177 | |||
178 | /** |
||
179 | * Método que busca el componente que ejecuta la ruta |
||
180 | * |
||
181 | * @param string $route |
||
182 | * |
||
183 | * @throws \PSFS\base\exception\RouterException |
||
184 | */ |
||
185 | 1 | protected function searchAction($route) |
|
215 | |||
216 | /** |
||
217 | * @param array $action |
||
218 | * @param array $params |
||
219 | * @return bool |
||
220 | */ |
||
221 | private function checkRequirements(array $action, array $params = []) { |
||
238 | |||
239 | /** |
||
240 | * Método que manda las cabeceras de autenticación |
||
241 | * @return string HTML |
||
242 | */ |
||
243 | protected function sentAuthHeader() |
||
247 | |||
248 | /** |
||
249 | * @return string|null |
||
250 | */ |
||
251 | 1 | private function getExternalModules() { |
|
258 | |||
259 | /** |
||
260 | * Method that check if the proyect has sub project to include |
||
261 | * @param boolean $hydrateRoute |
||
262 | */ |
||
263 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
273 | |||
274 | /** |
||
275 | * Method that gather all the routes in the project |
||
276 | */ |
||
277 | 1 | private function generateRouting() |
|
292 | |||
293 | /** |
||
294 | * Método que regenera el fichero de rutas |
||
295 | * @throws ConfigException |
||
296 | */ |
||
297 | 1 | public function hydrateRouting() |
|
314 | |||
315 | /** |
||
316 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
317 | * |
||
318 | * @param string $origen |
||
319 | * @param string $namespace |
||
320 | * @param array $routing |
||
321 | * |
||
322 | * @return array |
||
323 | * @throws ConfigException |
||
324 | */ |
||
325 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
336 | |||
337 | /** |
||
338 | * Checks that a namespace exists |
||
339 | * @param string $namespace |
||
340 | * @return bool |
||
341 | */ |
||
342 | 2 | public static function exists($namespace) |
|
346 | |||
347 | /** |
||
348 | * Método que añade nuevas rutas al array de referencia |
||
349 | * |
||
350 | * @param string $namespace |
||
351 | * @param array $routing |
||
352 | * @param string $module |
||
353 | * |
||
354 | * @return array |
||
355 | * @throws ConfigException |
||
356 | */ |
||
357 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
384 | |||
385 | /** |
||
386 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
387 | * |
||
388 | * @param \ReflectionClass $class |
||
389 | * |
||
390 | * @return Router |
||
391 | * @throws ConfigException |
||
392 | */ |
||
393 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
408 | |||
409 | /** |
||
410 | * Método que genera las urls amigables para usar dentro del framework |
||
411 | * @return Router |
||
412 | */ |
||
413 | 1 | public function simpatize() |
|
423 | |||
424 | /** |
||
425 | * Método que devuelve una ruta del framework |
||
426 | * |
||
427 | * @param string $slug |
||
428 | * @param boolean $absolute |
||
429 | * @param array $params |
||
430 | * |
||
431 | * @return string|null |
||
432 | * @throws RouterException |
||
433 | */ |
||
434 | 3 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
435 | { |
||
436 | 3 | if (strlen($slug) === 0) { |
|
437 | 1 | return ($absolute) ? Request::getInstance()->getRootUrl() . '/' : '/'; |
|
438 | } |
||
439 | 3 | if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) { |
|
440 | 1 | throw new RouterException(_("No existe la ruta especificada")); |
|
441 | } |
||
442 | 3 | $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; |
|
443 | 3 | if (!empty($params)) foreach ($params as $key => $value) { |
|
444 | $url = str_replace("{" . $key . "}", $value, $url); |
||
445 | 3 | } elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) { |
|
446 | 3 | $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"]; |
|
447 | } |
||
448 | |||
449 | 3 | return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url); |
|
450 | } |
||
451 | |||
452 | /** |
||
453 | * Método que devuelve las rutas de administración |
||
454 | * @deprecated |
||
455 | * @return array |
||
456 | */ |
||
457 | public function getAdminRoutes() |
||
461 | |||
462 | /** |
||
463 | * Método que devuelve le controlador del admin |
||
464 | * @deprecated |
||
465 | * @return Admin |
||
466 | */ |
||
467 | public function getAdmin() |
||
471 | |||
472 | /** |
||
473 | * Método que extrae los dominios |
||
474 | * @return array |
||
475 | */ |
||
476 | 1 | public function getDomains() |
|
480 | |||
481 | /** |
||
482 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
483 | * |
||
484 | * @param string $route |
||
485 | * @param array|null $action |
||
486 | * @param types\Controller $class |
||
487 | * @param array $params |
||
488 | */ |
||
489 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
514 | |||
515 | /** |
||
516 | * Parse slugs to create translations |
||
517 | * |
||
518 | * @param string $absoluteTranslationFileName |
||
519 | */ |
||
520 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
538 | |||
539 | /** |
||
540 | * @param bool $hydrateRoute |
||
541 | * @param $modulePath |
||
542 | * @param $externalModulePath |
||
543 | */ |
||
544 | private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
||
555 | |||
556 | /** |
||
557 | * @param $hydrateRoute |
||
558 | * @param $module |
||
559 | * @return mixed |
||
560 | */ |
||
561 | private function loadExternalModule($hydrateRoute, $module) |
||
575 | |||
576 | } |
||
577 |
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.