Complex classes like Template 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 Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Template |
||
17 | { |
||
18 | use SingletonTrait; |
||
19 | const STATUS_OK = 'HTTP/1.0 200 OK'; |
||
20 | /** |
||
21 | * @var \Twig_Environment tpl |
||
22 | */ |
||
23 | protected $tpl; |
||
24 | protected $filters = array(); |
||
25 | |||
26 | protected $debug = false; |
||
27 | protected $public_zone = true; |
||
28 | private $status_code = Template::STATUS_OK; |
||
29 | |||
30 | /** |
||
31 | * @var \PSFS\base\Cache $cache |
||
32 | */ |
||
33 | protected $cache; |
||
34 | |||
35 | /** |
||
36 | * Constructor por defecto |
||
37 | */ |
||
38 | 1 | public function __construct() |
|
45 | |||
46 | /** |
||
47 | * Método que devuelve el loader del Template |
||
48 | * @return \Twig_LoaderInterface |
||
49 | */ |
||
50 | public function getLoader() |
||
54 | |||
55 | /** |
||
56 | * Método que activa la zona pública |
||
57 | * @param bool $public |
||
58 | * |
||
59 | * @return Template |
||
60 | */ |
||
61 | public function setPublicZone($public = true) |
||
66 | |||
67 | /** |
||
68 | * Método que establece un header de http status code |
||
69 | * @param string $status |
||
70 | * |
||
71 | * @return Template |
||
72 | */ |
||
73 | public function setStatus($status = null) |
||
98 | |||
99 | /** |
||
100 | * Método que procesa la plantilla |
||
101 | * |
||
102 | * @param string $tpl |
||
103 | * @param array $vars |
||
104 | * @param array $cookies |
||
105 | * |
||
106 | * @return string HTML |
||
|
|||
107 | */ |
||
108 | 1 | public function render($tpl, array $vars = array(), array $cookies = array()) |
|
109 | { |
||
110 | 1 | Logger::log('Start render response'); |
|
111 | 1 | $vars = ResponseHelper::setDebugHeaders($vars); |
|
112 | $output = $this->dump($tpl, $vars); |
||
113 | |||
114 | return $this->output($output, 'text/html', $cookies); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Servicio que establece las cabeceras de la respuesta |
||
119 | * @param string $contentType |
||
120 | * @param array $cookies |
||
121 | */ |
||
122 | 1 | private function setReponseHeaders($contentType = 'text/html', array $cookies = array()) |
|
136 | |||
137 | /** |
||
138 | * Servicio que devuelve el output |
||
139 | * @param string $output |
||
140 | * @param string $contentType |
||
141 | * @param array $cookies |
||
142 | * @return string HTML |
||
143 | */ |
||
144 | public function output($output = '', $contentType = 'text/html', array $cookies = array()) |
||
166 | |||
167 | /** |
||
168 | * Método que cierra y limpia los buffers de salida |
||
169 | */ |
||
170 | public function closeRender() |
||
181 | |||
182 | /** |
||
183 | * Método que devuelve los datos cacheados con las cabeceras que tenía por entonces |
||
184 | * @param string $data |
||
185 | * @param array $headers |
||
186 | */ |
||
187 | public function renderCache($data, $headers = array()) |
||
199 | |||
200 | /** |
||
201 | * Método que añade una nueva ruta al path de Twig |
||
202 | * @param $path |
||
203 | * @param $domain |
||
204 | * |
||
205 | * @return Template |
||
206 | */ |
||
207 | 1 | public function addPath($path, $domain = '') |
|
212 | |||
213 | /** |
||
214 | * Método que devuelve el contenido de una plantilla |
||
215 | * @param string $tpl |
||
216 | * @param array $vars |
||
217 | * @return string |
||
218 | */ |
||
219 | public function dump($tpl, array $vars = array()) |
||
233 | |||
234 | /** |
||
235 | * Método que añade una función al motor de plantillas |
||
236 | * @param string $templateFunction |
||
237 | * @param $functionName |
||
238 | * |
||
239 | * @return Template |
||
240 | */ |
||
241 | 1 | protected function addTemplateFunction($templateFunction, $functionName) |
|
247 | |||
248 | /** |
||
249 | * Funcion Twig para los assets en las plantillas |
||
250 | * @return Template |
||
251 | */ |
||
252 | 1 | private function addAssetFunction() |
|
256 | |||
257 | /** |
||
258 | * Función que pinta un formulario |
||
259 | * @return Template |
||
260 | */ |
||
261 | 1 | private function addFormsFunction() |
|
265 | |||
266 | /** |
||
267 | * Función que pinta un campo de un formulario |
||
268 | * @return Template |
||
269 | */ |
||
270 | 1 | private function addFormWidgetFunction() |
|
274 | |||
275 | /** |
||
276 | * Función que pinta un botón de un formulario |
||
277 | * @return Template |
||
278 | */ |
||
279 | 1 | private function addFormButtonFunction() |
|
283 | |||
284 | /** |
||
285 | * Método que devuelve un parámetro de configuración en la plantilla |
||
286 | * @return Template |
||
287 | */ |
||
288 | 1 | private function addConfigFunction() |
|
292 | |||
293 | /** |
||
294 | * Método que añade la función path a Twig |
||
295 | * @return Template |
||
296 | */ |
||
297 | 1 | private function addRouteFunction() |
|
301 | |||
302 | /** |
||
303 | * Método que copia directamente el recurso solicitado a la carpeta pública |
||
304 | * @return Template |
||
305 | */ |
||
306 | 1 | private function addResourceFunction() |
|
310 | |||
311 | /** |
||
312 | * @return Template |
||
313 | */ |
||
314 | 1 | private function addSessionFunction() |
|
318 | |||
319 | /** |
||
320 | * @return Template |
||
321 | */ |
||
322 | 1 | private function addExistsFlashFunction() |
|
326 | |||
327 | /** |
||
328 | * @return Template |
||
329 | */ |
||
330 | 1 | private function addGetFlashFunction() |
|
334 | |||
335 | /** |
||
336 | * Servicio que regenera todas las plantillas |
||
337 | * @return array |
||
338 | */ |
||
339 | public function regenerateTemplates() |
||
350 | |||
351 | /** |
||
352 | * @param $tplDir |
||
353 | * @param string $domain |
||
354 | * |
||
355 | * @return mixed |
||
356 | */ |
||
357 | protected function generateTemplate($tplDir, $domain = '') |
||
372 | |||
373 | /** |
||
374 | * Método que extrae el path de un string |
||
375 | * @param $path |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | public static function extractPath($path) |
||
388 | |||
389 | /** |
||
390 | * Método que devuelve los dominios de una plataforma |
||
391 | * @param bool $append |
||
392 | * @return array |
||
393 | */ |
||
394 | static public function getDomains($append = false) |
||
406 | |||
407 | /** |
||
408 | * Método que añade todas las funciones de las plantillas |
||
409 | */ |
||
410 | 1 | private function addTemplateFunctions() |
|
424 | |||
425 | /** |
||
426 | * Método que devuelve el motod de plantillas |
||
427 | * @return \Twig_Environment |
||
428 | */ |
||
429 | public function getTemplateEngine() |
||
433 | |||
434 | /** |
||
435 | * Method that extract all domains for using them with the templates |
||
436 | */ |
||
437 | 1 | private function loadDomains() |
|
446 | |||
447 | /** |
||
448 | * Método que inicializa el motor de plantillas |
||
449 | */ |
||
450 | 1 | private function setup() |
|
462 | |||
463 | /** |
||
464 | * Método que inyecta los parseadores |
||
465 | */ |
||
466 | 1 | private function addTemplateTokens() |
|
472 | |||
473 | /** |
||
474 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
475 | */ |
||
476 | 1 | private function optimizeTemplates() |
|
481 | |||
482 | /** |
||
483 | * Method that extract all path tag for extracting translations |
||
484 | * @param array $domains |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | private function parsePathTranslations($domains) |
||
502 | |||
503 | /** |
||
504 | * Method that generate all template caches |
||
505 | */ |
||
506 | private function generateTemplatesCache() |
||
517 | } |
||
518 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.