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 |
||
| 19 | class Router |
||
| 20 | { |
||
| 21 | |||
| 22 | use SingletonTrait; |
||
| 23 | |||
| 24 | protected $routing; |
||
| 25 | protected $slugs; |
||
| 26 | private $domains; |
||
| 27 | /** |
||
| 28 | * @var Finder $finder |
||
| 29 | */ |
||
| 30 | private $finder; |
||
| 31 | /** |
||
| 32 | * @var \PSFS\base\Cache $cache |
||
| 33 | */ |
||
| 34 | private $cache; |
||
| 35 | /** |
||
| 36 | * @var \PSFS\base\Security $session |
||
| 37 | */ |
||
| 38 | private $session; |
||
| 39 | /** |
||
| 40 | * @var bool headersSent |
||
| 41 | */ |
||
| 42 | protected $headersSent = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor Router |
||
| 46 | * @throws ConfigException |
||
| 47 | */ |
||
| 48 | 6 | public function __construct() |
|
| 49 | { |
||
| 50 | 6 | $this->finder = new Finder(); |
|
| 51 | 6 | $this->cache = Cache::getInstance(); |
|
| 52 | 6 | $this->session = Security::getInstance(); |
|
| 53 | 6 | $this->init(); |
|
| 54 | 6 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Inicializador Router |
||
| 58 | * @throws ConfigException |
||
| 59 | */ |
||
| 60 | 1 | public function init() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Método que deriva un error HTTP de página no encontrada |
||
| 73 | * |
||
| 74 | * @param \Exception $e |
||
| 75 | * |
||
| 76 | * @return string HTML |
||
| 77 | */ |
||
| 78 | public function httpNotFound(\Exception $e = NULL) |
||
| 79 | { |
||
| 80 | $template = Template::getInstance() |
||
| 81 | ->setStatus($e->getCode()); |
||
| 82 | if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE'))) { |
||
| 83 | return $template->output(json_encode(array( |
||
| 84 | "success" => FALSE, |
||
| 85 | "error" => $e->getMessage(), |
||
| 86 | )), 'application/json'); |
||
| 87 | } else { |
||
| 88 | if (NULL === $e) { |
||
| 89 | $e = new \Exception(_('Página no encontrada'), 404); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $template->render('error.html.twig', array( |
||
| 93 | 'exception' => $e, |
||
| 94 | 'trace' => $e->getTraceAsString(), |
||
| 95 | 'error_page' => TRUE, |
||
| 96 | )); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Método que devuelve las rutas |
||
| 102 | * @return string|null |
||
| 103 | */ |
||
| 104 | public function getSlugs() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Método que calcula el objeto a enrutar |
||
| 111 | * |
||
| 112 | * @param string $route |
||
| 113 | * |
||
| 114 | * @throws \Exception |
||
| 115 | * @return string HTML |
||
| 116 | */ |
||
| 117 | public function execute($route) |
||
| 118 | { |
||
| 119 | try { |
||
| 120 | //Check CORS for requests |
||
| 121 | $this->checkCORS(); |
||
| 122 | // Checks restricted access |
||
| 123 | $this->checkRestrictedAccess($route); |
||
| 124 | |||
| 125 | //Search action and execute |
||
| 126 | return $this->searchAction($route); |
||
| 127 | } catch (AccessDeniedException $e) { |
||
| 128 | Logger::getInstance()->debugLog(_('Solicitamos credenciales de acceso a zona restringida')); |
||
| 129 | if ('login' === Config::getInstance()->get('admin_login')) { |
||
| 130 | return $this->redirectLogin($route); |
||
| 131 | } else { |
||
| 132 | return $this->sentAuthHeader(); |
||
| 133 | } |
||
| 134 | } catch (RouterException $r) { |
||
| 135 | if (FALSE !== preg_match('/\/$/', $route)) { |
||
| 136 | if (preg_match('/admin/', $route)) { |
||
| 137 | $default = Config::getInstance()->get('admin_action'); |
||
| 138 | } else { |
||
| 139 | $default = Config::getInstance()->get('home_action'); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $this->execute($this->getRoute($default)); |
||
| 143 | } |
||
| 144 | } catch (\Exception $e) { |
||
| 145 | Logger::getInstance()->errorLog($e->getMessage()); |
||
| 146 | throw $e; |
||
| 147 | } |
||
| 148 | |||
| 149 | return $this->httpNotFound(); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check CROS requests |
||
| 154 | */ |
||
| 155 | private function checkCORS() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Function that checks if the long of the patterns match |
||
| 178 | * @param $routePattern |
||
| 179 | * @param $path |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | private function compareSlashes($routePattern, $path) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Método que busca el componente que ejecuta la ruta |
||
| 196 | * |
||
| 197 | * @param string $route |
||
| 198 | * |
||
| 199 | * @throws \Exception |
||
| 200 | */ |
||
| 201 | protected function searchAction($route) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Método que manda las cabeceras de autenticación |
||
| 227 | * @return string HTML |
||
| 228 | */ |
||
| 229 | protected function sentAuthHeader() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Método que redirige a la pantalla web del login |
||
| 236 | * |
||
| 237 | * @param string $route |
||
| 238 | * |
||
| 239 | * @return string HTML |
||
| 240 | */ |
||
| 241 | public function redirectLogin($route) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Método que chequea el acceso a una zona restringida |
||
| 248 | * |
||
| 249 | * @param string $route |
||
| 250 | * |
||
| 251 | * @throws AccessDeniedException |
||
| 252 | */ |
||
| 253 | protected function checkRestrictedAccess($route) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Método que extrae de la url los parámetros REST |
||
| 268 | * |
||
| 269 | * @param string $route |
||
| 270 | * |
||
| 271 | * @param string $pattern |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | protected function extractComponents($route, $pattern) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Método que regenera el fichero de rutas |
||
| 294 | * @throws ConfigException |
||
| 295 | */ |
||
| 296 | 1 | public function hydrateRouting() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 322 | * |
||
| 323 | * @param string $origen |
||
| 324 | * @param string $namespace |
||
| 325 | * @param array $routing |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | * @throws ConfigException |
||
| 329 | */ |
||
| 330 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Método que añade nuevas rutas al array de referencia |
||
| 344 | * |
||
| 345 | * @param string $namespace |
||
| 346 | * @param array $routing |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | * @throws ConfigException |
||
| 350 | */ |
||
| 351 | 1 | private function addRouting($namespace, $routing) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 396 | * |
||
| 397 | * @param \ReflectionClass $class |
||
| 398 | * |
||
| 399 | * @return Router |
||
| 400 | * @throws ConfigException |
||
| 401 | */ |
||
| 402 | 1 | protected function extractDomain($class) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Método que genera las urls amigables para usar dentro del framework |
||
| 432 | * @return Router |
||
| 433 | */ |
||
| 434 | 1 | public function simpatize() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Método que devuelve el slug de un string dado |
||
| 447 | * |
||
| 448 | * @param string $text |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | 1 | private function slugify($text) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Método que devuelve una ruta del framework |
||
| 480 | * |
||
| 481 | * @param string $slug |
||
| 482 | * @param boolean $absolute |
||
| 483 | * @param array $params |
||
| 484 | * |
||
| 485 | * @return string|null |
||
| 486 | * @throws RouterException |
||
| 487 | */ |
||
| 488 | public function getRoute($slug = '', $absolute = FALSE, $params = NULL) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Método que devuelve las rutas de administración |
||
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | 1 | public function getAdminRoutes() |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Método que devuelve le controlador del admin |
||
| 545 | * @return Admin |
||
| 546 | */ |
||
| 547 | public function getAdmin() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Método que extrae los dominios |
||
| 554 | * @return array|null |
||
| 555 | */ |
||
| 556 | public function getDomains() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Método que extrae el controller a invocar |
||
| 563 | * |
||
| 564 | * @param string $action |
||
| 565 | * |
||
| 566 | * @return Object |
||
| 567 | */ |
||
| 568 | protected function getClassToCall($action) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Método que compara la ruta web con la guardada en la cache |
||
| 580 | * |
||
| 581 | * @param $routePattern |
||
| 582 | * @param $path |
||
| 583 | * |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | protected function matchRoutePattern($routePattern, $path) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @param $pattern |
||
| 598 | * |
||
| 599 | * @return array |
||
| 600 | */ |
||
| 601 | 2 | protected function extractHttpRoute($pattern) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Método que extrae los parámetros de una función |
||
| 614 | * |
||
| 615 | * @param array $sr |
||
| 616 | * @param \ReflectionMethod $method |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | 1 | private function extractReflectionParams($sr, $method) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Método que extrae el método http |
||
| 638 | * |
||
| 639 | * @param string $docComments |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | 1 | private function extractReflectionHttpMethod($docComments) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Método que extrae la visibilidad de una ruta |
||
| 652 | * |
||
| 653 | * @param string $docComments |
||
| 654 | * |
||
| 655 | * @return bool |
||
| 656 | */ |
||
| 657 | 1 | private function extractReflectionVisibility($docComments) |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Método que extrae el parámetro de caché |
||
| 666 | * |
||
| 667 | * @param string $docComments |
||
| 668 | * |
||
| 669 | * @return bool |
||
| 670 | */ |
||
| 671 | 1 | private function extractReflectionCacheability($docComments) |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 680 | * |
||
| 681 | * @param string $route |
||
| 682 | * @param array $action |
||
| 683 | * @param types\Controller $class |
||
| 684 | * @param array $params |
||
| 685 | */ |
||
| 686 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Parse slugs to create translations |
||
| 710 | * |
||
| 711 | * @param string $absoluteTranslationFileName |
||
| 712 | */ |
||
| 713 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Create translation file if not exists |
||
| 736 | * |
||
| 737 | * @param string $absoluteTranslationFileName |
||
| 738 | * |
||
| 739 | * @return array |
||
| 740 | */ |
||
| 741 | 1 | private function generateTranslationsFile($absoluteTranslationFileName) |
|
| 752 | |||
| 753 | } |
||
| 754 |
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.