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 | private function checkExternalModules() { |
||
218 | |||
219 | /** |
||
220 | * Method that gather all the routes in the project |
||
221 | */ |
||
222 | 1 | private function generateRouting() |
|
237 | 1 | ||
238 | 1 | /** |
|
239 | * Método que regenera el fichero de rutas |
||
240 | * @throws ConfigException |
||
241 | */ |
||
242 | public function hydrateRouting() |
||
259 | 1 | ||
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 | 1 | * @return array |
|
268 | 1 | * @throws ConfigException |
|
269 | */ |
||
270 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
||
281 | 1 | ||
282 | /** |
||
283 | 1 | * Checks that a namespace exists |
|
284 | 1 | * @param string $namespace |
|
285 | 1 | * @return bool |
|
286 | 1 | */ |
|
287 | 1 | public static function exists($namespace) { |
|
290 | 1 | ||
291 | /** |
||
292 | * Método que añade nuevas rutas al array de referencia |
||
293 | 1 | * |
|
294 | 1 | * @param string $namespace |
|
295 | 1 | * @param array $routing |
|
296 | 1 | * @param string $module |
|
297 | 1 | * |
|
298 | 1 | * @return array |
|
299 | 1 | * @throws ConfigException |
|
300 | 1 | */ |
|
301 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
325 | 1 | ||
326 | /** |
||
327 | 1 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
|
328 | * |
||
329 | * @param \ReflectionClass $class |
||
330 | * |
||
331 | * @return Router |
||
332 | * @throws ConfigException |
||
333 | */ |
||
334 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
349 | |||
350 | /** |
||
351 | * Método que genera las urls amigables para usar dentro del framework |
||
352 | * @return Router |
||
353 | */ |
||
354 | public function simpatize() |
||
364 | 1 | ||
365 | /** |
||
366 | 1 | * Método que devuelve una ruta del framework |
|
367 | 1 | * |
|
368 | 1 | * @param string $slug |
|
369 | * @param boolean $absolute |
||
370 | 1 | * @param array $params |
|
371 | * |
||
372 | * @return string|null |
||
373 | * @throws RouterException |
||
374 | */ |
||
375 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
||
392 | |||
393 | /** |
||
394 | * Método que devuelve las rutas de administración |
||
395 | * @return array |
||
396 | */ |
||
397 | public function getAdminRoutes() |
||
401 | |||
402 | /** |
||
403 | * Método que devuelve le controlador del admin |
||
404 | * @return Admin |
||
405 | */ |
||
406 | public function getAdmin() |
||
410 | |||
411 | /** |
||
412 | * Método que extrae los dominios |
||
413 | * @return array |
||
414 | */ |
||
415 | public function getDomains() |
||
419 | |||
420 | /** |
||
421 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
422 | * |
||
423 | * @param string $route |
||
424 | * @param array|null $action |
||
425 | * @param types\Controller $class |
||
426 | * @param array $params |
||
427 | */ |
||
428 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
453 | 1 | ||
454 | 1 | /** |
|
455 | 1 | * Parse slugs to create translations |
|
456 | 1 | * |
|
457 | * @param string $absoluteTranslationFileName |
||
458 | */ |
||
459 | private function generateSlugs($absoluteTranslationFileName) |
||
477 | |||
478 | } |
||
479 |
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.