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 | |||
| 27 | use SingletonTrait; |
||
| 28 | |||
| 29 | protected $routing; |
||
| 30 | protected $slugs; |
||
| 31 | private $domains; |
||
| 32 | /** |
||
| 33 | * @var Finder $finder |
||
| 34 | */ |
||
| 35 | private $finder; |
||
| 36 | /** |
||
| 37 | * @var \PSFS\base\Cache $cache |
||
| 38 | */ |
||
| 39 | private $cache; |
||
| 40 | /** |
||
| 41 | * @var bool headersSent |
||
| 42 | */ |
||
| 43 | protected $headersSent = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor Router |
||
| 47 | * @throws ConfigException |
||
| 48 | */ |
||
| 49 | 6 | public function __construct() |
|
| 50 | { |
||
| 51 | 6 | $this->finder = new Finder(); |
|
| 52 | 6 | $this->cache = Cache::getInstance(); |
|
| 53 | 6 | $this->init(); |
|
| 54 | 6 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Inicializador Router |
||
| 58 | * @throws ConfigException |
||
| 59 | */ |
||
| 60 | 1 | public function init() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Método que deriva un error HTTP de página no encontrada |
||
| 73 | * |
||
| 74 | * @param \Exception $e |
||
| 75 | * |
||
| 76 | * @return string HTML |
||
| 77 | */ |
||
| 78 | public function httpNotFound(\Exception $e = NULL) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Método que devuelve las rutas |
||
| 103 | * @return string|null |
||
| 104 | */ |
||
| 105 | public function getSlugs() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Method that extract all routes in the platform |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getAllRoutes() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Método que calcula el objeto a enrutar |
||
| 126 | * |
||
| 127 | * @param string|null $route |
||
| 128 | * |
||
| 129 | * @throws \Exception |
||
| 130 | * @return string HTML |
||
| 131 | */ |
||
| 132 | public function execute($route) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Método que busca el componente que ejecuta la ruta |
||
| 160 | * |
||
| 161 | * @param string $route |
||
| 162 | * |
||
| 163 | * @throws \PSFS\base\exception\RouterException |
||
| 164 | */ |
||
| 165 | protected function searchAction($route) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Método que manda las cabeceras de autenticación |
||
| 192 | * @return string HTML |
||
| 193 | */ |
||
| 194 | protected function sentAuthHeader() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Method that gather all the routes in the project |
||
| 201 | */ |
||
| 202 | 1 | private function generateRouting() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Método que regenera el fichero de rutas |
||
| 220 | * @throws ConfigException |
||
| 221 | */ |
||
| 222 | 1 | public function hydrateRouting() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
| 242 | * |
||
| 243 | * @param string $origen |
||
| 244 | * @param string $namespace |
||
| 245 | * @param array $routing |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | * @throws ConfigException |
||
| 249 | */ |
||
| 250 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Checks that a namespace exists |
||
| 264 | * @param string $namespace |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | 1 | public static function exists($namespace) { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Método que añade nuevas rutas al array de referencia |
||
| 273 | * |
||
| 274 | * @param string $namespace |
||
| 275 | * @param array $routing |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | * @throws ConfigException |
||
| 279 | */ |
||
| 280 | 1 | private function addRouting($namespace, &$routing) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
| 307 | * |
||
| 308 | * @param \ReflectionClass $class |
||
| 309 | * |
||
| 310 | * @return Router |
||
| 311 | * @throws ConfigException |
||
| 312 | */ |
||
| 313 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Método que genera las urls amigables para usar dentro del framework |
||
| 326 | * @return Router |
||
| 327 | */ |
||
| 328 | 1 | public function simpatize() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Método que devuelve una ruta del framework |
||
| 341 | * |
||
| 342 | * @param string $slug |
||
| 343 | * @param boolean $absolute |
||
| 344 | * @param array $params |
||
| 345 | * |
||
| 346 | * @return string|null |
||
| 347 | * @throws RouterException |
||
| 348 | */ |
||
| 349 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Método que devuelve las rutas de administración |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | 1 | public function getAdminRoutes() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Método que devuelve le controlador del admin |
||
| 378 | * @return Admin |
||
| 379 | */ |
||
| 380 | public function getAdmin() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Método que extrae los dominios |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getDomains() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
| 396 | * |
||
| 397 | * @param string $route |
||
| 398 | * @param array|null $action |
||
| 399 | * @param types\Controller $class |
||
| 400 | * @param array $params |
||
| 401 | */ |
||
| 402 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Parse slugs to create translations |
||
| 426 | * |
||
| 427 | * @param string $absoluteTranslationFileName |
||
| 428 | */ |
||
| 429 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
| 447 | |||
| 448 | } |
||
| 449 |
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.