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 | * 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 | 1 | } |
|
| 70 | 1 | $this->checkExternalModules(false); |
|
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Load routes and domains and store them |
||
| 75 | */ |
||
| 76 | 1 | private function debugLoad() { |
|
| 77 | 1 | Logger::log('Begin routes load', LOG_DEBUG); |
|
| 78 | 1 | $this->hydrateRouting(); |
|
| 79 | 1 | $this->simpatize(); |
|
| 80 | 1 | Logger::log('End routes load', LOG_DEBUG); |
|
| 81 | 1 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Método que deriva un error HTTP de página no encontrada |
||
| 85 | * |
||
| 86 | * @param \Exception $e |
||
| 87 | * |
||
| 88 | * @return string HTML |
||
| 89 | */ |
||
| 90 | 1 | public function httpNotFound(\Exception $e = NULL) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Método que devuelve las rutas |
||
| 114 | * @return string|null |
||
| 115 | */ |
||
| 116 | public function getSlugs() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | 1 | public function getRoutes() { |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Method that extract all routes in the platform |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function getAllRoutes() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Método que calcula el objeto a enrutar |
||
| 145 | * |
||
| 146 | * @param string|null $route |
||
| 147 | * |
||
| 148 | * @throws \Exception |
||
| 149 | * @return string HTML |
||
| 150 | */ |
||
| 151 | public function execute($route) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Método que busca el componente que ejecuta la ruta |
||
| 176 | * |
||
| 177 | * @param string $route |
||
| 178 | * |
||
| 179 | * @throws \PSFS\base\exception\RouterException |
||
| 180 | */ |
||
| 181 | protected function searchAction($route) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Método que manda las cabeceras de autenticación |
||
| 208 | * @return string HTML |
||
| 209 | */ |
||
| 210 | protected function sentAuthHeader() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Method that check if the proyect has sub project to include |
||
| 217 | * @param boolean $hydrateRoute |
||
| 218 | */ |
||
| 219 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Method that gather all the routes in the project |
||
| 248 | */ |
||
| 249 | 1 | private function generateRouting() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Método que regenera el fichero de rutas |
||
| 267 | * @throws ConfigException |
||
| 268 | */ |
||
| 269 | 1 | public function hydrateRouting() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 289 | * |
||
| 290 | * @param string $origen |
||
| 291 | * @param string $namespace |
||
| 292 | * @param array $routing |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | * @throws ConfigException |
||
| 296 | */ |
||
| 297 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Checks that a namespace exists |
||
| 311 | * @param string $namespace |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 1 | public static function exists($namespace) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Método que añade nuevas rutas al array de referencia |
||
| 321 | * |
||
| 322 | * @param string $namespace |
||
| 323 | * @param array $routing |
||
| 324 | * @param string $module |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | * @throws ConfigException |
||
| 328 | */ |
||
| 329 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 359 | * |
||
| 360 | * @param \ReflectionClass $class |
||
| 361 | * |
||
| 362 | * @return Router |
||
| 363 | * @throws ConfigException |
||
| 364 | */ |
||
| 365 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Método que genera las urls amigables para usar dentro del framework |
||
| 383 | * @return Router |
||
| 384 | */ |
||
| 385 | 1 | public function simpatize() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Método que devuelve una ruta del framework |
||
| 398 | * |
||
| 399 | * @param string $slug |
||
| 400 | * @param boolean $absolute |
||
| 401 | * @param array $params |
||
| 402 | * |
||
| 403 | * @return string|null |
||
| 404 | * @throws RouterException |
||
| 405 | */ |
||
| 406 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Método que devuelve las rutas de administración |
||
| 426 | * @deprecated |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function getAdminRoutes() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Método que devuelve le controlador del admin |
||
| 436 | * @deprecated |
||
| 437 | * @return Admin |
||
| 438 | */ |
||
| 439 | public function getAdmin() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Método que extrae los dominios |
||
| 446 | * @return array |
||
| 447 | */ |
||
| 448 | public function getDomains() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 455 | * |
||
| 456 | * @param string $route |
||
| 457 | * @param array|null $action |
||
| 458 | * @param types\Controller $class |
||
| 459 | * @param array $params |
||
| 460 | */ |
||
| 461 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Parse slugs to create translations |
||
| 490 | * |
||
| 491 | * @param string $absoluteTranslationFileName |
||
| 492 | */ |
||
| 493 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 511 | |||
| 512 | } |
||
| 513 |
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.