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() |
|
| 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) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Método que devuelve las rutas |
||
| 103 | * @return string|null |
||
| 104 | */ |
||
| 105 | public function getSlugs() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Método que calcula el objeto a enrutar |
||
| 112 | * |
||
| 113 | * @param string $route |
||
| 114 | * |
||
| 115 | * @throws \Exception |
||
| 116 | * @return string HTML |
||
| 117 | */ |
||
| 118 | public function execute($route) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Check CROS requests |
||
| 155 | */ |
||
| 156 | private function checkCORS() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Function that checks if the long of the patterns match |
||
| 182 | * @param $routePattern |
||
| 183 | * @param $path |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | private function compareSlashes($routePattern, $path) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Método que busca el componente que ejecuta la ruta |
||
| 201 | * |
||
| 202 | * @param string $route |
||
| 203 | * |
||
| 204 | * @throws \PSFS\base\exception\RouterException |
||
| 205 | */ |
||
| 206 | protected function searchAction($route) |
||
| 207 | { |
||
| 208 | Logger::log('Searching action to execute'); |
||
| 209 | //Revisamos si tenemos la ruta registrada |
||
| 210 | $parts = parse_url($route); |
||
| 211 | $path = (array_key_exists('path', $parts)) ? $parts['path'] : $route; |
||
| 212 | $httpRequest = Request::getInstance()->getMethod(); |
||
| 213 | foreach ($this->routing as $pattern => $action) { |
||
| 214 | list($httpMethod, $routePattern) = $this->extractHttpRoute($pattern); |
||
| 215 | $matched = $this->matchRoutePattern($routePattern, $path); |
||
| 216 | if ($matched && ($httpMethod === "ALL" || $httpRequest === $httpMethod) && $this->compareSlashes($routePattern, $path)) { |
||
| 217 | $get = $this->extractComponents($route, $routePattern); |
||
| 218 | /** @var $class \PSFS\base\types\Controller */ |
||
| 219 | $class = $this->getClassToCall($action); |
||
| 220 | try { |
||
| 221 | $this->executeCachedRoute($route, $action, $class, $get); |
||
| 222 | } catch (\Exception $e) { |
||
| 223 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 224 | throw new RouterException($e->getMessage(), 404, $e); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | throw new RouterException(_("Ruta no encontrada")); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Método que manda las cabeceras de autenticación |
||
| 233 | * @return string HTML |
||
| 234 | */ |
||
| 235 | protected function sentAuthHeader() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Método que redirige a la pantalla web del login |
||
| 242 | * |
||
| 243 | * @param string $route |
||
| 244 | * |
||
| 245 | * @return string HTML |
||
| 246 | */ |
||
| 247 | public function redirectLogin($route) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Método que chequea el acceso a una zona restringida |
||
| 254 | * |
||
| 255 | * @param string $route |
||
| 256 | * |
||
| 257 | * @throws AccessDeniedException |
||
| 258 | */ |
||
| 259 | protected function checkRestrictedAccess($route) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Método que extrae de la url los parámetros REST |
||
| 275 | * |
||
| 276 | * @param string $route |
||
| 277 | * |
||
| 278 | * @param string $pattern |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | protected function extractComponents($route, $pattern) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Método que regenera el fichero de rutas |
||
| 302 | * @throws ConfigException |
||
| 303 | */ |
||
| 304 | 1 | public function hydrateRouting() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 330 | * |
||
| 331 | * @param string $origen |
||
| 332 | * @param string $namespace |
||
| 333 | * @param array $routing |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | * @throws ConfigException |
||
| 337 | */ |
||
| 338 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Método que añade nuevas rutas al array de referencia |
||
| 352 | * |
||
| 353 | * @param string $namespace |
||
| 354 | * @param array $routing |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | * @throws ConfigException |
||
| 358 | */ |
||
| 359 | 1 | private function addRouting($namespace, $routing) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 404 | * |
||
| 405 | * @param \ReflectionClass $class |
||
| 406 | * |
||
| 407 | * @return Router |
||
| 408 | * @throws ConfigException |
||
| 409 | */ |
||
| 410 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Método que genera las urls amigables para usar dentro del framework |
||
| 440 | * @return Router |
||
| 441 | */ |
||
| 442 | 1 | public function simpatize() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Método que devuelve el slug de un string dado |
||
| 455 | * |
||
| 456 | * @param string $text |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 1 | private function slugify($text) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Método que devuelve una ruta del framework |
||
| 488 | * |
||
| 489 | * @param string $slug |
||
| 490 | * @param boolean $absolute |
||
| 491 | * @param array $params |
||
| 492 | * |
||
| 493 | * @return string|null |
||
| 494 | * @throws RouterException |
||
| 495 | */ |
||
| 496 | public function getRoute($slug = '', $absolute = FALSE, $params = NULL) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Método que devuelve las rutas de administración |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | 1 | public function getAdminRoutes() |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Método que devuelve le controlador del admin |
||
| 552 | * @return Admin |
||
| 553 | */ |
||
| 554 | public function getAdmin() |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Método que extrae los dominios |
||
| 561 | * @return array|null |
||
| 562 | */ |
||
| 563 | public function getDomains() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Método que extrae el controller a invocar |
||
| 570 | * |
||
| 571 | * @param string $action |
||
| 572 | * |
||
| 573 | * @return Object |
||
| 574 | */ |
||
| 575 | protected function getClassToCall($action) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Método que compara la ruta web con la guardada en la cache |
||
| 585 | * |
||
| 586 | * @param $routePattern |
||
| 587 | * @param $path |
||
| 588 | * |
||
| 589 | * @return bool |
||
| 590 | */ |
||
| 591 | protected function matchRoutePattern($routePattern, $path) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @param $pattern |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 2 | protected function extractHttpRoute($pattern) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Método que extrae los parámetros de una función |
||
| 619 | * |
||
| 620 | * @param array $sr |
||
| 621 | * @param \ReflectionMethod $method |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | 1 | private function extractReflectionParams($sr, $method) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Método que extrae el método http |
||
| 643 | * |
||
| 644 | * @param string $docComments |
||
| 645 | * |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | 1 | private function extractReflectionHttpMethod($docComments) |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Método que extrae la visibilidad de una ruta |
||
| 657 | * |
||
| 658 | * @param string $docComments |
||
| 659 | * |
||
| 660 | * @return bool |
||
| 661 | */ |
||
| 662 | 1 | private function extractReflectionVisibility($docComments) |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Método que extrae el parámetro de caché |
||
| 670 | * |
||
| 671 | * @param string $docComments |
||
| 672 | * |
||
| 673 | * @return bool |
||
| 674 | */ |
||
| 675 | 1 | private function extractReflectionCacheability($docComments) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 684 | * |
||
| 685 | * @param string $route |
||
| 686 | * @param array $action |
||
| 687 | * @param types\Controller $class |
||
| 688 | * @param array $params |
||
| 689 | */ |
||
| 690 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Parse slugs to create translations |
||
| 714 | * |
||
| 715 | * @param string $absoluteTranslationFileName |
||
| 716 | */ |
||
| 717 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Create translation file if not exists |
||
| 740 | * |
||
| 741 | * @param string $absoluteTranslationFileName |
||
| 742 | * |
||
| 743 | * @return array |
||
| 744 | */ |
||
| 745 | 1 | private function generateTranslationsFile($absoluteTranslationFileName) |
|
| 756 | |||
| 757 | } |
||
| 758 |
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.