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() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Inicializador 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 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
|
| 67 | 1 | if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
| 68 | 1 | $this->debugLoad(); |
|
| 69 | } |
||
| 70 | 1 | $this->checkExternalModules(false); |
|
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Load routes and domains and store them |
||
| 75 | */ |
||
| 76 | 1 | private function debugLoad() { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Método que deriva un error HTTP de página no encontrada |
||
| 85 | * |
||
| 86 | * @param \Exception $e |
||
| 87 | * @param boolean $isJson |
||
| 88 | * |
||
| 89 | * @return string HTML |
||
| 90 | */ |
||
| 91 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
||
| 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) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Método que busca el componente que ejecuta la ruta |
||
| 182 | * |
||
| 183 | * @param string $route |
||
| 184 | * |
||
| 185 | * @throws \PSFS\base\exception\RouterException |
||
| 186 | */ |
||
| 187 | 1 | protected function searchAction($route) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Método que manda las cabeceras de autenticación |
||
| 214 | * @return string HTML |
||
| 215 | */ |
||
| 216 | protected function sentAuthHeader() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return string|null |
||
| 223 | */ |
||
| 224 | 1 | private function getExternalModules() { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Method that check if the proyect has sub project to include |
||
| 234 | * @param boolean $hydrateRoute |
||
| 235 | */ |
||
| 236 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Method that gather all the routes in the project |
||
| 265 | */ |
||
| 266 | 1 | private function generateRouting() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Método que regenera el fichero de rutas |
||
| 284 | * @throws ConfigException |
||
| 285 | */ |
||
| 286 | 1 | public function hydrateRouting() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 306 | * |
||
| 307 | * @param string $origen |
||
| 308 | * @param string $namespace |
||
| 309 | * @param array $routing |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | * @throws ConfigException |
||
| 313 | */ |
||
| 314 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Checks that a namespace exists |
||
| 328 | * @param string $namespace |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | 1 | public static function exists($namespace) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Método que añade nuevas rutas al array de referencia |
||
| 338 | * |
||
| 339 | * @param string $namespace |
||
| 340 | * @param array $routing |
||
| 341 | * @param string $module |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | * @throws ConfigException |
||
| 345 | */ |
||
| 346 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 376 | * |
||
| 377 | * @param \ReflectionClass $class |
||
| 378 | * |
||
| 379 | * @return Router |
||
| 380 | * @throws ConfigException |
||
| 381 | */ |
||
| 382 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Método que genera las urls amigables para usar dentro del framework |
||
| 400 | * @return Router |
||
| 401 | */ |
||
| 402 | 1 | public function simpatize() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Método que devuelve una ruta del framework |
||
| 415 | * |
||
| 416 | * @param string $slug |
||
| 417 | * @param boolean $absolute |
||
| 418 | * @param array $params |
||
| 419 | * |
||
| 420 | * @return string|null |
||
| 421 | * @throws RouterException |
||
| 422 | */ |
||
| 423 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Método que devuelve las rutas de administración |
||
| 443 | * @deprecated |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | public function getAdminRoutes() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Método que devuelve le controlador del admin |
||
| 453 | * @deprecated |
||
| 454 | * @return Admin |
||
| 455 | */ |
||
| 456 | public function getAdmin() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Método que extrae los dominios |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | 1 | public function getDomains() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 472 | * |
||
| 473 | * @param string $route |
||
| 474 | * @param array|null $action |
||
| 475 | * @param types\Controller $class |
||
| 476 | * @param array $params |
||
| 477 | */ |
||
| 478 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Parse slugs to create translations |
||
| 507 | * |
||
| 508 | * @param string $absoluteTranslationFileName |
||
| 509 | */ |
||
| 510 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 528 | |||
| 529 | } |
||
| 530 |
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.