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 | public function render($tpl, array $vars = array(), array $cookies = array()) |
||
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()) |
||
165 | |||
166 | /** |
||
167 | * Método que cierra y limpia los buffers de salida |
||
168 | */ |
||
169 | public function closeRender() |
||
180 | |||
181 | /** |
||
182 | * Método que devuelve los datos cacheados con las cabeceras que tenía por entonces |
||
183 | * @param string $data |
||
184 | * @param array $headers |
||
185 | */ |
||
186 | public function renderCache($data, $headers = array()) |
||
197 | |||
198 | /** |
||
199 | * Método que añade una nueva ruta al path de Twig |
||
200 | * @param $path |
||
201 | * @param $domain |
||
202 | * |
||
203 | * @return Template |
||
204 | */ |
||
205 | 1 | public function addPath($path, $domain = '') |
|
210 | |||
211 | /** |
||
212 | * Método que devuelve el contenido de una plantilla |
||
213 | * @param string $tpl |
||
214 | * @param array $vars |
||
215 | * @return string |
||
216 | */ |
||
217 | public function dump($tpl, array $vars = array()) |
||
231 | |||
232 | /** |
||
233 | * Método que añade una función al motor de plantillas |
||
234 | * @param string $templateFunction |
||
235 | * @param $functionName |
||
236 | * |
||
237 | * @return Template |
||
238 | */ |
||
239 | 1 | protected function addTemplateFunction($templateFunction, $functionName) |
|
245 | |||
246 | /** |
||
247 | * Funcion Twig para los assets en las plantillas |
||
248 | * @return Template |
||
249 | */ |
||
250 | 1 | private function addAssetFunction() |
|
254 | |||
255 | /** |
||
256 | * Función que pinta un formulario |
||
257 | * @return Template |
||
258 | */ |
||
259 | 1 | private function addFormsFunction() |
|
263 | |||
264 | /** |
||
265 | * Función que pinta un campo de un formulario |
||
266 | * @return Template |
||
267 | */ |
||
268 | 1 | private function addFormWidgetFunction() |
|
272 | |||
273 | /** |
||
274 | * Función que pinta un botón de un formulario |
||
275 | * @return Template |
||
276 | */ |
||
277 | 1 | private function addFormButtonFunction() |
|
281 | |||
282 | /** |
||
283 | * Método que devuelve un parámetro de configuración en la plantilla |
||
284 | * @return Template |
||
285 | */ |
||
286 | 1 | private function addConfigFunction() |
|
290 | |||
291 | /** |
||
292 | * Método que añade la función path a Twig |
||
293 | * @return Template |
||
294 | */ |
||
295 | 1 | private function addRouteFunction() |
|
299 | |||
300 | /** |
||
301 | * Método que copia directamente el recurso solicitado a la carpeta pública |
||
302 | * @return Template |
||
303 | */ |
||
304 | 1 | private function addResourceFunction() |
|
308 | |||
309 | /** |
||
310 | * @return Template |
||
311 | */ |
||
312 | 1 | private function addSessionFunction() |
|
316 | |||
317 | /** |
||
318 | * @return Template |
||
319 | */ |
||
320 | 1 | private function addExistsFlashFunction() |
|
324 | |||
325 | /** |
||
326 | * @return Template |
||
327 | */ |
||
328 | 1 | private function addGetFlashFunction() |
|
332 | |||
333 | /** |
||
334 | * Servicio que regenera todas las plantillas |
||
335 | * @return array |
||
336 | */ |
||
337 | public function regenerateTemplates() |
||
348 | |||
349 | /** |
||
350 | * @param $tplDir |
||
351 | * @param string $domain |
||
352 | * |
||
353 | * @return mixed |
||
354 | */ |
||
355 | protected function generateTemplate($tplDir, $domain = '') |
||
371 | |||
372 | /** |
||
373 | * Método que extrae el path de un string |
||
374 | * @param $path |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | public static function extractPath($path) |
||
387 | |||
388 | /** |
||
389 | * Método que devuelve los dominios de una plataforma |
||
390 | * @param bool $append |
||
391 | * @return array |
||
392 | */ |
||
393 | static public function getDomains($append = false) |
||
405 | |||
406 | /** |
||
407 | * Método que añade todas las funciones de las plantillas |
||
408 | */ |
||
409 | 1 | private function addTemplateFunctions() |
|
423 | |||
424 | /** |
||
425 | * Método que devuelve el motod de plantillas |
||
426 | * @return \Twig_Environment |
||
427 | */ |
||
428 | public function getTemplateEngine() |
||
432 | |||
433 | /** |
||
434 | * Method that extract all domains for using them with the templates |
||
435 | */ |
||
436 | 1 | private function loadDomains() |
|
445 | |||
446 | /** |
||
447 | * Método que inicializa el motor de plantillas |
||
448 | */ |
||
449 | 1 | private function setup() |
|
461 | |||
462 | /** |
||
463 | * Método que inyecta los parseadores |
||
464 | */ |
||
465 | 1 | private function addTemplateTokens() |
|
471 | |||
472 | /** |
||
473 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
474 | */ |
||
475 | 1 | private function optimizeTemplates() |
|
480 | |||
481 | /** |
||
482 | * Method that extract all path tag for extracting translations |
||
483 | * @param array $domains |
||
484 | * |
||
485 | * @return array |
||
486 | */ |
||
487 | private function parsePathTranslations($domains) |
||
501 | |||
502 | /** |
||
503 | * Method that generate all template caches |
||
504 | */ |
||
505 | private function generateTemplatesCache() |
||
516 | } |
||
517 |
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.