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 |
||
22 | class Router |
||
23 | { |
||
24 | |||
25 | use SingletonTrait; |
||
26 | |||
27 | protected $routing; |
||
28 | protected $slugs; |
||
29 | private $domains; |
||
30 | /** |
||
31 | * @var Finder $finder |
||
32 | */ |
||
33 | private $finder; |
||
34 | /** |
||
35 | * @var \PSFS\base\Cache $cache |
||
36 | */ |
||
37 | private $cache; |
||
38 | /** |
||
39 | * @var bool headersSent |
||
40 | */ |
||
41 | protected $headersSent = false; |
||
42 | |||
43 | /** |
||
44 | * Constructor Router |
||
45 | * @throws ConfigException |
||
46 | */ |
||
47 | 6 | public function __construct() |
|
53 | |||
54 | /** |
||
55 | * Inicializador Router |
||
56 | * @throws ConfigException |
||
57 | */ |
||
58 | 1 | public function init() |
|
68 | |||
69 | /** |
||
70 | * Método que deriva un error HTTP de página no encontrada |
||
71 | * |
||
72 | * @param \Exception $e |
||
73 | * |
||
74 | * @return string HTML |
||
75 | */ |
||
76 | public function httpNotFound(\Exception $e = NULL) |
||
98 | |||
99 | /** |
||
100 | * Método que devuelve las rutas |
||
101 | * @return string|null |
||
102 | */ |
||
103 | public function getSlugs() |
||
107 | |||
108 | /** |
||
109 | * Method that extract all routes in the platform |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getAllRoutes() { |
||
121 | |||
122 | /** |
||
123 | * Método que calcula el objeto a enrutar |
||
124 | * |
||
125 | * @param string|null $route |
||
126 | * |
||
127 | * @throws \Exception |
||
128 | * @return string HTML |
||
129 | */ |
||
130 | public function execute($route) |
||
155 | |||
156 | /** |
||
157 | * Método que busca el componente que ejecuta la ruta |
||
158 | * |
||
159 | * @param string $route |
||
160 | * |
||
161 | * @throws \PSFS\base\exception\RouterException |
||
162 | */ |
||
163 | protected function searchAction($route) |
||
187 | |||
188 | /** |
||
189 | * Método que manda las cabeceras de autenticación |
||
190 | * @return string HTML |
||
191 | */ |
||
192 | protected function sentAuthHeader() |
||
196 | |||
197 | /** |
||
198 | * Method that gather all the routes in the project |
||
199 | */ |
||
200 | private function generateRouting() |
||
215 | |||
216 | /** |
||
217 | * Método que regenera el fichero de rutas |
||
218 | * @throws ConfigException |
||
219 | */ |
||
220 | 1 | public function hydrateRouting() |
|
237 | |||
238 | 1 | /** |
|
239 | 1 | * Método que inspecciona los directorios en busca de clases que registren rutas |
|
240 | * |
||
241 | * @param string $origen |
||
242 | 1 | * @param string $namespace |
|
243 | 1 | * @param array $routing |
|
244 | * |
||
245 | * @return array |
||
246 | * @throws ConfigException |
||
247 | */ |
||
248 | private function inspectDir($origen, $namespace = 'PSFS', $routing) |
||
259 | 1 | ||
260 | 1 | /** |
|
261 | 1 | * Checks that a namespace exists |
|
262 | 1 | * @param string $namespace |
|
263 | * @return bool |
||
264 | 1 | */ |
|
265 | public static function exists($namespace) { |
||
268 | |||
269 | /** |
||
270 | * Método que añade nuevas rutas al array de referencia |
||
271 | * |
||
272 | 1 | * @param string $namespace |
|
273 | 1 | * @param array $routing |
|
274 | * |
||
275 | * @return array |
||
276 | * @throws ConfigException |
||
277 | */ |
||
278 | private function addRouting($namespace, &$routing) |
||
302 | 1 | ||
303 | 1 | /** |
|
304 | 1 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
|
305 | 1 | * |
|
306 | * @param \ReflectionClass $class |
||
307 | 1 | * |
|
308 | * @return Router |
||
309 | * @throws ConfigException |
||
310 | */ |
||
311 | protected function extractDomain(\ReflectionClass $class) |
||
321 | 1 | ||
322 | 1 | /** |
|
323 | 1 | * Método que genera las urls amigables para usar dentro del framework |
|
324 | 1 | * @return Router |
|
325 | */ |
||
326 | 1 | public function simpatize() |
|
336 | 1 | ||
337 | 1 | /** |
|
338 | 1 | * Método que devuelve el slug de un string dado |
|
339 | 1 | * |
|
340 | * @param string $text |
||
341 | 1 | * |
|
342 | * @return string |
||
343 | */ |
||
344 | private function slugify($text) |
||
369 | |||
370 | 1 | /** |
|
371 | * Método que devuelve una ruta del framework |
||
372 | * |
||
373 | * @param string $slug |
||
374 | 1 | * @param boolean $absolute |
|
375 | * @param array $params |
||
376 | * |
||
377 | * @return string|null |
||
378 | * @throws RouterException |
||
379 | */ |
||
380 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
||
397 | |||
398 | /** |
||
399 | * Método que devuelve las rutas de administración |
||
400 | * @return array |
||
401 | */ |
||
402 | public function getAdminRoutes() |
||
433 | 1 | ||
434 | 1 | /** |
|
435 | 1 | * Método que devuelve le controlador del admin |
|
436 | * @return Admin |
||
437 | */ |
||
438 | 1 | public function getAdmin() |
|
442 | |||
443 | /** |
||
444 | * Método que extrae los dominios |
||
445 | * @return array |
||
446 | */ |
||
447 | public function getDomains() |
||
451 | |||
452 | /** |
||
453 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
454 | * |
||
455 | * @param string $route |
||
456 | * @param array|null $action |
||
457 | * @param types\Controller $class |
||
458 | * @param array $params |
||
459 | */ |
||
460 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
481 | |||
482 | /** |
||
483 | * Parse slugs to create translations |
||
484 | * |
||
485 | * @param string $absoluteTranslationFileName |
||
486 | */ |
||
487 | private function generateSlugs($absoluteTranslationFileName) |
||
505 | 1 | ||
506 | 1 | /** |
|
507 | 1 | * Create translation file if not exists |
|
508 | 1 | * |
|
509 | 1 | * @param string $absoluteTranslationFileName |
|
510 | 1 | * |
|
511 | 1 | * @return array |
|
512 | */ |
||
513 | private function generateTranslationsFile($absoluteTranslationFileName) |
||
524 | |||
525 | } |
||
526 |
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.