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 |
||
25 | class Router |
||
26 | { |
||
27 | |||
28 | use SingletonTrait; |
||
29 | |||
30 | protected $routing; |
||
31 | protected $slugs; |
||
32 | private $domains; |
||
33 | /** |
||
34 | * @var Finder $finder |
||
35 | */ |
||
36 | private $finder; |
||
37 | /** |
||
38 | * @var \PSFS\base\Cache $cache |
||
39 | */ |
||
40 | private $cache; |
||
41 | /** |
||
42 | * @var bool headersSent |
||
43 | */ |
||
44 | protected $headersSent = false; |
||
45 | |||
46 | /** |
||
47 | * Constructor Router |
||
48 | * @throws ConfigException |
||
49 | */ |
||
50 | 6 | public function __construct() |
|
51 | { |
||
52 | 6 | $this->finder = new Finder(); |
|
53 | 6 | $this->cache = Cache::getInstance(); |
|
54 | 6 | $this->init(); |
|
55 | 6 | } |
|
56 | |||
57 | /** |
||
58 | * Inicializador Router |
||
59 | * @throws ConfigException |
||
60 | */ |
||
61 | 1 | public function init() |
|
62 | { |
||
63 | 1 | if (!file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json") || Config::getInstance()->getDebugMode()) { |
|
64 | 1 | $this->hydrateRouting(); |
|
65 | 1 | $this->simpatize(); |
|
66 | 1 | } else { |
|
67 | 1 | list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", Cache::JSON, TRUE); |
|
|
|||
68 | 1 | $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", Cache::JSON, TRUE); |
|
69 | } |
||
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * Método que deriva un error HTTP de página no encontrada |
||
74 | * |
||
75 | * @param \Exception $e |
||
76 | * |
||
77 | * @return string HTML |
||
78 | */ |
||
79 | public function httpNotFound(\Exception $e = NULL) |
||
80 | { |
||
81 | Logger::log('Throw not found exception'); |
||
82 | if (NULL === $e) { |
||
83 | Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
||
84 | $e = new \Exception(_('Page not found'), 404); |
||
85 | } |
||
86 | $template = Template::getInstance()->setStatus($e->getCode()); |
||
87 | if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE'))) { |
||
88 | return $template->output(json_encode(array( |
||
89 | "success" => FALSE, |
||
90 | "error" => $e->getMessage(), |
||
91 | )), 'application/json'); |
||
92 | } else { |
||
93 | return $template->render('error.html.twig', array( |
||
94 | 'exception' => $e, |
||
95 | 'trace' => $e->getTraceAsString(), |
||
96 | 'error_page' => TRUE, |
||
97 | )); |
||
98 | } |
||
99 | } |
||
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 | 1 | public function getAllRoutes() |
|
115 | { |
||
116 | $routes = []; |
||
117 | foreach ($this->routing as $path => $route) { |
||
118 | if (array_key_exists('slug', $route)) { |
||
119 | $routes[$route['slug']] = $path; |
||
120 | } |
||
121 | } |
||
122 | return $routes; |
||
123 | 1 | } |
|
124 | |||
125 | /** |
||
126 | * Método que calcula el objeto a enrutar |
||
127 | * |
||
128 | * @param string|null $route |
||
129 | * |
||
130 | * @throws \Exception |
||
131 | * @return string HTML |
||
132 | */ |
||
133 | public function execute($route) |
||
134 | { |
||
135 | Logger::log('Executing the request'); |
||
136 | try { |
||
137 | //Check CORS for requests |
||
138 | RequestHelper::checkCORS(); |
||
139 | // Checks restricted access |
||
140 | SecurityHelper::checkRestrictedAccess($route); |
||
141 | //Search action and execute |
||
142 | $this->searchAction($route); |
||
143 | } catch (AccessDeniedException $e) { |
||
144 | Logger::log(_('Solicitamos credenciales de acceso a zona restringida')); |
||
145 | return Admin::staticAdminLogon($route); |
||
146 | } catch (RouterException $r) { |
||
147 | Logger::log($r->getMessage(), LOG_WARNING); |
||
148 | } catch (\Exception $e) { |
||
149 | Logger::log($e->getMessage(), LOG_ERR); |
||
150 | throw $e; |
||
151 | } |
||
152 | |||
153 | return $this->httpNotFound(); |
||
154 | } |
||
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 | 1 | private function checkExternalModules() |
|
198 | { |
||
199 | 1 | $externalModules = Config::getParam('modules.extend'); |
|
200 | 1 | if (null !== $externalModules) { |
|
201 | $externalModules = explode(',', $externalModules); |
||
221 | |||
222 | /** |
||
223 | * Method that gather all the routes in the project |
||
224 | */ |
||
225 | 1 | private function generateRouting() |
|
240 | |||
241 | /** |
||
242 | * Método que regenera el fichero de rutas |
||
243 | * @throws ConfigException |
||
244 | */ |
||
245 | 1 | public function hydrateRouting() |
|
262 | |||
263 | /** |
||
264 | * Método que inspecciona los directorios en busca de clases que registren rutas |
||
265 | * |
||
266 | * @param string $origen |
||
267 | * @param string $namespace |
||
268 | * @param array $routing |
||
269 | * |
||
270 | * @return array |
||
271 | * @throws ConfigException |
||
272 | */ |
||
273 | 1 | private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
284 | |||
285 | /** |
||
286 | * Checks that a namespace exists |
||
287 | * @param string $namespace |
||
288 | * @return bool |
||
289 | */ |
||
290 | 1 | public static function exists($namespace) |
|
294 | |||
295 | /** |
||
296 | * Método que añade nuevas rutas al array de referencia |
||
297 | * |
||
298 | * @param string $namespace |
||
299 | * @param array $routing |
||
300 | * @param string $module |
||
301 | * |
||
302 | * @return array |
||
303 | * @throws ConfigException |
||
304 | */ |
||
305 | 1 | private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
331 | |||
332 | /** |
||
333 | * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
||
334 | * |
||
335 | * @param \ReflectionClass $class |
||
336 | * |
||
337 | * @return Router |
||
338 | * @throws ConfigException |
||
339 | */ |
||
340 | 1 | protected function extractDomain(\ReflectionClass $class) |
|
355 | |||
356 | /** |
||
357 | * Método que genera las urls amigables para usar dentro del framework |
||
358 | * @return Router |
||
359 | */ |
||
360 | 1 | public function simpatize() |
|
370 | |||
371 | /** |
||
372 | * Método que devuelve una ruta del framework |
||
373 | * |
||
374 | * @param string $slug |
||
375 | * @param boolean $absolute |
||
376 | * @param array $params |
||
377 | * |
||
378 | * @return string|null |
||
379 | * @throws RouterException |
||
380 | */ |
||
381 | 1 | public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
398 | |||
399 | /** |
||
400 | * Método que devuelve las rutas de administración |
||
401 | * @return array |
||
402 | */ |
||
403 | 1 | public function getAdminRoutes() |
|
407 | |||
408 | /** |
||
409 | * Método que devuelve le controlador del admin |
||
410 | * @return Admin |
||
411 | */ |
||
412 | public function getAdmin() |
||
416 | |||
417 | /** |
||
418 | * Método que extrae los dominios |
||
419 | * @return array |
||
420 | */ |
||
421 | public function getDomains() |
||
425 | |||
426 | /** |
||
427 | * @param $class |
||
428 | * @param $method |
||
429 | * @param array $params |
||
430 | * @return \ReflectionMethod |
||
431 | */ |
||
432 | private function checkAction($class, $method, array $params) |
||
443 | |||
444 | /** |
||
445 | * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
||
446 | * |
||
447 | * @param string $route |
||
448 | * @param array|null $action |
||
449 | * @param types\Controller $class |
||
450 | * @param array $params |
||
451 | */ |
||
452 | protected function executeCachedRoute($route, $action, $class, $params = NULL) |
||
479 | |||
480 | /** |
||
481 | * Parse slugs to create translations |
||
482 | * |
||
483 | * @param string $absoluteTranslationFileName |
||
484 | */ |
||
485 | 1 | private function generateSlugs($absoluteTranslationFileName) |
|
503 | |||
504 | } |
||
505 |
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.