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 |
||
| 24 | class Router |
||
| 25 | { |
||
| 26 | use SingletonTrait; |
||
| 27 | |||
| 28 | protected $routing; |
||
| 29 | protected $slugs; |
||
| 30 | private $domains = []; |
||
| 31 | /** |
||
| 32 | * @var Finder $finder |
||
| 33 | */ |
||
| 34 | private $finder; |
||
| 35 | /** |
||
| 36 | * @var \PSFS\base\Cache $cache |
||
| 37 | */ |
||
| 38 | private $cache; |
||
| 39 | /** |
||
| 40 | * @var bool headersSent |
||
| 41 | */ |
||
| 42 | protected $headersSent = false; |
||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $cacheType = Cache::JSON; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor Router |
||
| 50 | * @throws ConfigException |
||
| 51 | */ |
||
| 52 | 1 | public function __construct() |
|
| 53 | { |
||
| 54 | 1 | $this->finder = new Finder(); |
|
| 55 | 1 | $this->cache = Cache::getInstance(); |
|
| 56 | 1 | $this->init(); |
|
| 57 | 1 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Initializer Router |
||
| 61 | * @throws ConfigException |
||
| 62 | */ |
||
| 63 | 1 | public function init() |
|
| 64 | { |
||
| 65 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", $this->cacheType, TRUE); |
|
|
|
|||
| 66 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
| 67 | 1 | $this->debugLoad(); |
|
| 68 | } else { |
||
| 69 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
||
| 70 | } |
||
| 71 | 1 | $this->checkExternalModules(false); |
|
| 72 | 1 | $this->setLoaded(true); |
|
| 73 | 1 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Load routes and domains and store them |
||
| 77 | */ |
||
| 78 | 1 | private function debugLoad() { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Método que deriva un error HTTP de página no encontrada |
||
| 87 | * |
||
| 88 | * @param \Exception $e |
||
| 89 | * @param boolean $isJson |
||
| 90 | * |
||
| 91 | * @return string HTML |
||
| 92 | */ |
||
| 93 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
| 94 | { |
||
| 95 | Logger::log('Throw not found exception'); |
||
| 96 | if (NULL === $e) { |
||
| 97 | Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
||
| 98 | $e = new \Exception(_('Page not found'), 404); |
||
| 99 | } |
||
| 100 | $template = Template::getInstance()->setStatus($e->getCode()); |
||
| 101 | if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE')) || $isJson) { |
||
| 102 | $response = new JsonResponse(null, false, 0, 0, $e->getMessage()); |
||
| 103 | return $template->output(json_encode($response), 'application/json'); |
||
| 104 | } else { |
||
| 105 | $not_found_route = Config::getParam('route.404'); |
||
| 106 | if(null !== $not_found_route) { |
||
| 107 | Request::getInstance()->redirect($this->getRoute($not_found_route, true)); |
||
| 108 | } else { |
||
| 109 | return $template->render('error.html.twig', array( |
||
| 110 | 'exception' => $e, |
||
| 111 | 'trace' => $e->getTraceAsString(), |
||
| 112 | 'error_page' => TRUE, |
||
| 113 | )); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Método que devuelve las rutas |
||
| 120 | * @return string|null |
||
| 121 | */ |
||
| 122 | 1 | public function getSlugs() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | 2 | public function getRoutes() { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Method that extract all routes in the platform |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | 1 | public function getAllRoutes() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Método que calcula el objeto a enrutar |
||
| 151 | * |
||
| 152 | * @param string|null $route |
||
| 153 | * |
||
| 154 | * @throws \Exception |
||
| 155 | * @return string HTML |
||
| 156 | */ |
||
| 157 | 1 | public function execute($route) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Método que busca el componente que ejecuta la ruta |
||
| 178 | * |
||
| 179 | * @param string $route |
||
| 180 | * |
||
| 181 | * @throws \PSFS\base\exception\RouterException |
||
| 182 | */ |
||
| 183 | 1 | protected function searchAction($route) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param array $action |
||
| 216 | * @param array $params |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | private function checkRequirements(array $action, array $params = []) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Método que manda las cabeceras de autenticación |
||
| 236 | * @return string HTML |
||
| 237 | */ |
||
| 238 | protected function sentAuthHeader() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return string|null |
||
| 245 | */ |
||
| 246 | 1 | private function getExternalModules() { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Method that check if the project has sub project to include |
||
| 256 | * @param boolean $hydrateRoute |
||
| 257 | */ |
||
| 258 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Method that gather all the routes in the project |
||
| 271 | */ |
||
| 272 | 1 | private function generateRouting() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Método que regenera el fichero de rutas |
||
| 290 | * @throws ConfigException |
||
| 291 | */ |
||
| 292 | 1 | public function hydrateRouting() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 312 | * |
||
| 313 | * @param string $origen |
||
| 314 | * @param string $namespace |
||
| 315 | * @param array $routing |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | * @throws ConfigException |
||
| 319 | */ |
||
| 320 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Checks that a namespace exists |
||
| 334 | * @param string $namespace |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 2 | public static function exists($namespace) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Método que añade nuevas rutas al array de referencia |
||
| 344 | * |
||
| 345 | * @param string $namespace |
||
| 346 | * @param array $routing |
||
| 347 | * @param string $module |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | * @throws ConfigException |
||
| 351 | */ |
||
| 352 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 385 | * |
||
| 386 | * @param \ReflectionClass $class |
||
| 387 | * |
||
| 388 | * @return Router |
||
| 389 | * @throws ConfigException |
||
| 390 | */ |
||
| 391 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Método que genera las urls amigables para usar dentro del framework |
||
| 409 | * @return Router |
||
| 410 | */ |
||
| 411 | 1 | public function simpatize() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Método que devuelve una ruta del framework |
||
| 424 | * |
||
| 425 | * @param string $slug |
||
| 426 | * @param boolean $absolute |
||
| 427 | * @param array $params |
||
| 428 | * |
||
| 429 | * @return string|null |
||
| 430 | * @throws RouterException |
||
| 431 | */ |
||
| 432 | 3 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Método que devuelve las rutas de administración |
||
| 452 | * @deprecated |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function getAdminRoutes() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Método que devuelve le controlador del admin |
||
| 462 | * @deprecated |
||
| 463 | * @return Admin |
||
| 464 | */ |
||
| 465 | public function getAdmin() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Método que extrae los dominios |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | 1 | public function getDomains() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 481 | * |
||
| 482 | * @param string $route |
||
| 483 | * @param array|null $action |
||
| 484 | * @param types\Controller $class |
||
| 485 | * @param array $params |
||
| 486 | */ |
||
| 487 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Parse slugs to create translations |
||
| 515 | * |
||
| 516 | * @param string $absoluteTranslationFileName |
||
| 517 | */ |
||
| 518 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param bool $hydrateRoute |
||
| 536 | * @param $modulePath |
||
| 537 | * @param $externalModulePath |
||
| 538 | */ |
||
| 539 | private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param $hydrateRoute |
||
| 551 | * @param $module |
||
| 552 | * @return mixed |
||
| 553 | */ |
||
| 554 | private function loadExternalModule($hydrateRoute, $module) |
||
| 569 | |||
| 570 | } |
||
| 571 |
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.