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 | |||
| 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 | 6 | public function __construct() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Inicializador Router |
||
| 63 | * @throws ConfigException |
||
| 64 | */ |
||
| 65 | 1 | public function init() |
|
| 66 | { |
||
| 67 | 1 | if(Cache::canUseMemcache()) { |
|
| 68 | $this->cacheType = Cache::MEMCACHE; |
||
| 69 | } |
||
| 70 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", $this->cacheType, TRUE); |
|
|
|
|||
| 71 | 1 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
|
| 72 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
| 73 | 1 | $this->debugLoad(); |
|
| 74 | } |
||
| 75 | 1 | $this->checkExternalModules(false); |
|
| 76 | 1 | } |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Load routes and domains and store them |
||
| 80 | */ |
||
| 81 | 1 | private function debugLoad() { |
|
| 82 | 1 | Logger::log('Begin routes load', LOG_DEBUG); |
|
| 83 | 1 | $this->hydrateRouting(); |
|
| 84 | 1 | $this->simpatize(); |
|
| 85 | 1 | Logger::log('End routes load', LOG_DEBUG); |
|
| 86 | 1 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Método que deriva un error HTTP de página no encontrada |
||
| 90 | * |
||
| 91 | * @param \Exception $e |
||
| 92 | * |
||
| 93 | * @return string HTML |
||
| 94 | */ |
||
| 95 | public function httpNotFound(\Exception $e = NULL) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Método que devuelve las rutas |
||
| 119 | * @return string|null |
||
| 120 | */ |
||
| 121 | public function getSlugs() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | 1 | public function getRoutes() { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Method that extract all routes in the platform |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getAllRoutes() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Método que calcula el objeto a enrutar |
||
| 150 | * |
||
| 151 | * @param string|null $route |
||
| 152 | * |
||
| 153 | * @throws \Exception |
||
| 154 | * @return string HTML |
||
| 155 | */ |
||
| 156 | public function execute($route) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Método que busca el componente que ejecuta la ruta |
||
| 181 | * |
||
| 182 | * @param string $route |
||
| 183 | * |
||
| 184 | * @throws \PSFS\base\exception\RouterException |
||
| 185 | */ |
||
| 186 | protected function searchAction($route) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Método que manda las cabeceras de autenticación |
||
| 213 | * @return string HTML |
||
| 214 | */ |
||
| 215 | protected function sentAuthHeader() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Method that check if the proyect has sub project to include |
||
| 222 | * @param boolean $hydrateRoute |
||
| 223 | */ |
||
| 224 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Method that gather all the routes in the project |
||
| 253 | */ |
||
| 254 | 1 | private function generateRouting() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Método que regenera el fichero de rutas |
||
| 272 | * @throws ConfigException |
||
| 273 | */ |
||
| 274 | 1 | public function hydrateRouting() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 294 | * |
||
| 295 | * @param string $origen |
||
| 296 | * @param string $namespace |
||
| 297 | * @param array $routing |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | * @throws ConfigException |
||
| 301 | */ |
||
| 302 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Checks that a namespace exists |
||
| 316 | * @param string $namespace |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | 1 | public static function exists($namespace) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Método que añade nuevas rutas al array de referencia |
||
| 326 | * |
||
| 327 | * @param string $namespace |
||
| 328 | * @param array $routing |
||
| 329 | * @param string $module |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | * @throws ConfigException |
||
| 333 | */ |
||
| 334 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 364 | * |
||
| 365 | * @param \ReflectionClass $class |
||
| 366 | * |
||
| 367 | * @return Router |
||
| 368 | * @throws ConfigException |
||
| 369 | */ |
||
| 370 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Método que genera las urls amigables para usar dentro del framework |
||
| 388 | * @return Router |
||
| 389 | */ |
||
| 390 | 1 | public function simpatize() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Método que devuelve una ruta del framework |
||
| 403 | * |
||
| 404 | * @param string $slug |
||
| 405 | * @param boolean $absolute |
||
| 406 | * @param array $params |
||
| 407 | * |
||
| 408 | * @return string|null |
||
| 409 | * @throws RouterException |
||
| 410 | */ |
||
| 411 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Método que devuelve las rutas de administración |
||
| 431 | * @deprecated |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | public function getAdminRoutes() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Método que devuelve le controlador del admin |
||
| 441 | * @deprecated |
||
| 442 | * @return Admin |
||
| 443 | */ |
||
| 444 | public function getAdmin() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Método que extrae los dominios |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getDomains() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 460 | * |
||
| 461 | * @param string $route |
||
| 462 | * @param array|null $action |
||
| 463 | * @param types\Controller $class |
||
| 464 | * @param array $params |
||
| 465 | */ |
||
| 466 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Parse slugs to create translations |
||
| 496 | * |
||
| 497 | * @param string $absoluteTranslationFileName |
||
| 498 | */ |
||
| 499 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 517 | |||
| 518 | } |
||
| 519 |
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.