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() |
|
| 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 | 1 | public function httpNotFound(\Exception $e = NULL, $isJson = false) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Método que devuelve las rutas |
||
| 115 | * @return string|null |
||
| 116 | */ |
||
| 117 | 1 | public function getSlugs() |
|
| 118 | { |
||
| 119 | 1 | return $this->slugs; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | 2 | public function getRoutes() { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Method that extract all routes in the platform |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 1 | public function getAllRoutes() |
|
| 134 | { |
||
| 135 | 1 | $routes = []; |
|
| 136 | 1 | foreach ($this->getRoutes() as $path => $route) { |
|
| 137 | 1 | if (array_key_exists('slug', $route)) { |
|
| 138 | 1 | $routes[$route['slug']] = $path; |
|
| 139 | 1 | } |
|
| 140 | 1 | } |
|
| 141 | 1 | return $routes; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Método que calcula el objeto a enrutar |
||
| 146 | * |
||
| 147 | * @param string|null $route |
||
| 148 | * |
||
| 149 | * @throws \Exception |
||
| 150 | * @return string HTML |
||
| 151 | */ |
||
| 152 | 1 | public function execute($route) |
|
| 153 | { |
||
| 154 | 1 | Logger::log('Executing the request'); |
|
| 155 | try { |
||
| 156 | //Check CORS for requests |
||
| 157 | 1 | RequestHelper::checkCORS(); |
|
| 158 | // Checks restricted access |
||
| 159 | 1 | SecurityHelper::checkRestrictedAccess($route); |
|
| 160 | //Search action and execute |
||
| 161 | 1 | $this->searchAction($route); |
|
| 162 | 1 | } catch (AccessDeniedException $e) { |
|
| 163 | Logger::log(_('Solicitamos credenciales de acceso a zona restringida')); |
||
| 164 | return Admin::staticAdminLogon($route); |
||
| 165 | 1 | } catch (RouterException $r) { |
|
| 166 | 1 | Logger::log($r->getMessage(), LOG_WARNING); |
|
| 167 | 1 | } catch (\Exception $e) { |
|
| 168 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 169 | throw $e; |
||
| 170 | } |
||
| 171 | |||
| 172 | 1 | throw new RouterException(_("Página no encontrada"), 404); |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Método que busca el componente que ejecuta la ruta |
||
| 177 | * |
||
| 178 | * @param string $route |
||
| 179 | * |
||
| 180 | * @throws \PSFS\base\exception\RouterException |
||
| 181 | */ |
||
| 182 | 1 | protected function searchAction($route) |
|
| 183 | { |
||
| 184 | 1 | Logger::log('Searching action to execute: ' . $route, LOG_INFO); |
|
| 185 | //Revisamos si tenemos la ruta registrada |
||
| 186 | 1 | $parts = parse_url($route); |
|
| 187 | 1 | $path = (array_key_exists('path', $parts)) ? $parts['path'] : $route; |
|
| 188 | 1 | $httpRequest = Request::getInstance()->getMethod(); |
|
| 189 | 1 | foreach ($this->routing as $pattern => $action) { |
|
| 190 | 1 | list($httpMethod, $routePattern) = RouterHelper::extractHttpRoute($pattern); |
|
| 191 | 1 | $matched = RouterHelper::matchRoutePattern($routePattern, $path); |
|
| 192 | 1 | if ($matched && ($httpMethod === "ALL" || $httpRequest === $httpMethod) && RouterHelper::compareSlashes($routePattern, $path)) { |
|
| 193 | $get = RouterHelper::extractComponents($route, $routePattern); |
||
| 194 | /** @var $class \PSFS\base\types\Controller */ |
||
| 195 | $class = RouterHelper::getClassToCall($action); |
||
| 196 | try { |
||
| 197 | $this->executeCachedRoute($route, $action, $class, $get); |
||
| 198 | } catch (\Exception $e) { |
||
| 199 | Logger::log($e->getMessage(), LOG_ERR); |
||
| 200 | throw new \RuntimeException($e->getMessage(), 404, $e); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | 1 | } |
|
| 204 | 1 | throw new RouterException(_("Ruta no encontrada")); |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Método que manda las cabeceras de autenticación |
||
| 209 | * @return string HTML |
||
| 210 | */ |
||
| 211 | protected function sentAuthHeader() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return string|null |
||
| 218 | */ |
||
| 219 | 1 | private function getExternalModules() { |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Method that check if the proyect has sub project to include |
||
| 229 | * @param boolean $hydrateRoute |
||
| 230 | */ |
||
| 231 | 1 | private function checkExternalModules($hydrateRoute = true) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Method that gather all the routes in the project |
||
| 260 | */ |
||
| 261 | 1 | private function generateRouting() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Método que regenera el fichero de rutas |
||
| 279 | * @throws ConfigException |
||
| 280 | */ |
||
| 281 | 1 | public function hydrateRouting() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 301 | * |
||
| 302 | * @param string $origen |
||
| 303 | * @param string $namespace |
||
| 304 | * @param array $routing |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | * @throws ConfigException |
||
| 308 | */ |
||
| 309 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Checks that a namespace exists |
||
| 323 | * @param string $namespace |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | 1 | public static function exists($namespace) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Método que añade nuevas rutas al array de referencia |
||
| 333 | * |
||
| 334 | * @param string $namespace |
||
| 335 | * @param array $routing |
||
| 336 | * @param string $module |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | * @throws ConfigException |
||
| 340 | */ |
||
| 341 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 371 | * |
||
| 372 | * @param \ReflectionClass $class |
||
| 373 | * |
||
| 374 | * @return Router |
||
| 375 | * @throws ConfigException |
||
| 376 | */ |
||
| 377 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Método que genera las urls amigables para usar dentro del framework |
||
| 395 | * @return Router |
||
| 396 | */ |
||
| 397 | 1 | public function simpatize() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Método que devuelve una ruta del framework |
||
| 410 | * |
||
| 411 | * @param string $slug |
||
| 412 | * @param boolean $absolute |
||
| 413 | * @param array $params |
||
| 414 | * |
||
| 415 | * @return string|null |
||
| 416 | * @throws RouterException |
||
| 417 | */ |
||
| 418 | 2 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Método que devuelve las rutas de administración |
||
| 438 | * @deprecated |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getAdminRoutes() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Método que devuelve le controlador del admin |
||
| 448 | * @deprecated |
||
| 449 | * @return Admin |
||
| 450 | */ |
||
| 451 | public function getAdmin() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Método que extrae los dominios |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 1 | public function getDomains() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 467 | * |
||
| 468 | * @param string $route |
||
| 469 | * @param array|null $action |
||
| 470 | * @param types\Controller $class |
||
| 471 | * @param array $params |
||
| 472 | */ |
||
| 473 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Parse slugs to create translations |
||
| 502 | * |
||
| 503 | * @param string $absoluteTranslationFileName |
||
| 504 | */ |
||
| 505 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 523 | |||
| 524 | } |
||
| 525 |
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.