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) |
||
99 | |||
100 | /** |
||
101 | * Método que devuelve las rutas |
||
102 | * @return string|null |
||
103 | */ |
||
104 | public function getSlugs() |
||
108 | |||
109 | /** |
||
110 | * Method that extract all routes in the platform |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getAllRoutes() { |
||
122 | |||
123 | /** |
||
124 | * Método que calcula el objeto a enrutar |
||
125 | * |
||
126 | * @param string|null $route |
||
127 | * |
||
128 | * @throws \Exception |
||
129 | * @return string HTML |
||
130 | */ |
||
131 | public function execute($route) |
||
153 | |||
154 | /** |
||
155 | * Método que busca el componente que ejecuta la ruta |
||
156 | * |
||
157 | * @param string $route |
||
158 | * |
||
159 | * @throws \PSFS\base\exception\RouterException |
||
160 | */ |
||
161 | protected function searchAction($route) |
||
185 | |||
186 | /** |
||
187 | * Método que manda las cabeceras de autenticación |
||
188 | * @return string HTML |
||
189 | */ |
||
190 | protected function sentAuthHeader() |
||
194 | |||
195 | 1 | private function checkExternalModules() { |
|
218 | |||
219 | /** |
||
220 | * Method that gather all the routes in the project |
||
221 | */ |
||
222 | 1 | private function generateRouting() |
|
237 | |||
238 | /** |
||
239 | * Método que regenera el fichero de rutas |
||
240 | * @throws ConfigException |
||
241 | */ |
||
242 | 1 | public function hydrateRouting() |
|
259 | |||
260 | /** |
||
261 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
262 | * |
||
263 | * @param string $origen |
||
264 | * @param string $namespace |
||
265 | * @param array $routing |
||
266 | * |
||
267 | * @return array |
||
268 | * @throws ConfigException |
||
269 | */ |
||
270 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
281 | |||
282 | /** |
||
283 | * Checks that a namespace exists |
||
284 | * @param string $namespace |
||
285 | * @return bool |
||
286 | */ |
||
287 | 1 | public static function exists($namespace) { |
|
290 | |||
291 | /** |
||
292 | * Método que añade nuevas rutas al array de referencia |
||
293 | * |
||
294 | * @param string $namespace |
||
295 | * @param array $routing |
||
296 | * @param string $module |
||
297 | * |
||
298 | * @return array |
||
299 | * @throws ConfigException |
||
300 | */ |
||
301 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
327 | |||
328 | /** |
||
329 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
330 | * |
||
331 | * @param \ReflectionClass $class |
||
332 | * |
||
333 | * @return Router |
||
334 | * @throws ConfigException |
||
335 | */ |
||
336 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
351 | |||
352 | /** |
||
353 | * Método que genera las urls amigables para usar dentro del framework |
||
354 | * @return Router |
||
355 | */ |
||
356 | 1 | public function simpatize() |
|
366 | |||
367 | /** |
||
368 | * Método que devuelve una ruta del framework |
||
369 | * |
||
370 | * @param string $slug |
||
371 | * @param boolean $absolute |
||
372 | * @param array $params |
||
373 | * |
||
374 | * @return string|null |
||
375 | * @throws RouterException |
||
376 | */ |
||
377 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
394 | |||
395 | /** |
||
396 | * Método que devuelve las rutas de administración |
||
397 | * @return array |
||
398 | */ |
||
399 | 1 | public function getAdminRoutes() |
|
403 | |||
404 | /** |
||
405 | * Método que devuelve le controlador del admin |
||
406 | * @return Admin |
||
407 | */ |
||
408 | public function getAdmin() |
||
412 | |||
413 | /** |
||
414 | * Método que extrae los dominios |
||
415 | * @return array |
||
416 | */ |
||
417 | public function getDomains() |
||
421 | |||
422 | /** |
||
423 | * @param $class |
||
424 | * @param $method |
||
425 | * @param array $params |
||
426 | * @return \ReflectionMethod |
||
427 | */ |
||
428 | private function checkAction($class, $method, array $params) { |
||
438 | |||
439 | /** |
||
440 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
441 | * |
||
442 | * @param string $route |
||
443 | * @param array|null $action |
||
444 | * @param types\Controller $class |
||
445 | * @param array $params |
||
446 | */ |
||
447 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
471 | |||
472 | /** |
||
473 | * Parse slugs to create translations |
||
474 | * |
||
475 | * @param string $absoluteTranslationFileName |
||
476 | */ |
||
477 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
495 | |||
496 | } |
||
497 |
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.