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()) |
||
| 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 = '') |
||
| 356 | { |
||
| 357 | $templatesDir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tplDir), \RecursiveIteratorIterator::LEAVES_ONLY); |
||
| 358 | foreach ($templatesDir as $file) { |
||
| 359 | // force compilation |
||
| 360 | if ($file->isFile()) { |
||
| 361 | try { |
||
| 362 | $this->tpl->load(str_replace($tplDir . '/', '', $file)); |
||
| 363 | } catch (\Exception $e) { |
||
| 364 | Logger::log($e->getMessage(), LOG_ERR, ['file' => $e->getFile(), 'line' => $e->getLine()]); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | return str_replace("%d", $domain, str_replace("%s", $tplDir, _("Generando plantillas en path '%s' para el dominio '%d'"))); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Método que extrae el path de un string |
||
| 373 | * @param $path |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public static function extractPath($path) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Método que devuelve los dominios de una plataforma |
||
| 389 | * @param bool $append |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | static public function getDomains($append = false) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Método que añade todas las funciones de las plantillas |
||
| 407 | */ |
||
| 408 | 1 | private function addTemplateFunctions() |
|
| 409 | { |
||
| 410 | //Asignamos las funciones especiales |
||
| 411 | 1 | $this->addAssetFunction() |
|
| 412 | 1 | ->addFormsFunction() |
|
| 413 | 1 | ->addFormWidgetFunction() |
|
| 414 | 1 | ->addFormButtonFunction() |
|
| 415 | 1 | ->addConfigFunction() |
|
| 416 | 1 | ->addRouteFunction() |
|
| 417 | 1 | ->addSessionFunction() |
|
| 418 | 1 | ->addExistsFlashFunction() |
|
| 419 | 1 | ->addGetFlashFunction() |
|
| 420 | 1 | ->addResourceFunction(); |
|
| 421 | 1 | } |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Método que devuelve el motod de plantillas |
||
| 425 | * @return \Twig_Environment |
||
| 426 | */ |
||
| 427 | public function getTemplateEngine() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Method that extract all domains for using them with the templates |
||
| 434 | */ |
||
| 435 | 1 | private function loadDomains() |
|
| 436 | { |
||
| 437 | 1 | $domains = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', Cache::JSON, true); |
|
| 438 | 1 | if(null !== $domains) { |
|
| 439 | 1 | foreach($domains as $domain => $paths) { |
|
| 440 | 1 | $this->addPath($paths['template'], preg_replace('/(@|\/)/', '', $domain)); |
|
| 441 | 1 | } |
|
| 442 | 1 | } |
|
| 443 | 1 | } |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Método que inicializa el motor de plantillas |
||
| 447 | */ |
||
| 448 | 1 | private function setup() |
|
| 449 | { |
||
| 450 | 1 | $this->debug = Config::getInstance()->getDebugMode() ?: FALSE; |
|
| 451 | 1 | $this->cache = Cache::getInstance(); |
|
| 452 | 1 | $loader = new \Twig_Loader_Filesystem(Config::getInstance()->getTemplatePath()); |
|
| 453 | 1 | $this->tpl = new \Twig_Environment($loader, array( |
|
| 454 | 1 | 'cache' => Config::getInstance()->getCachePath() . DIRECTORY_SEPARATOR . 'twig', |
|
| 455 | 1 | 'debug' => (bool)$this->debug, |
|
| 456 | 1 | 'auto_reload' => Config::getParam('twig.auto_reload', TRUE), |
|
| 457 | 1 | )); |
|
| 458 | 1 | $this->loadDomains(); |
|
| 459 | 1 | } |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Método que inyecta los parseadores |
||
| 463 | */ |
||
| 464 | 1 | private function addTemplateTokens() |
|
| 465 | { |
||
| 466 | //Añadimos las extensiones de los tags |
||
| 467 | 1 | $this->tpl->addTokenParser(new AssetsTokenParser("css")); |
|
| 468 | 1 | $this->tpl->addTokenParser(new AssetsTokenParser("js")); |
|
| 469 | 1 | } |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
| 473 | */ |
||
| 474 | 1 | private function optimizeTemplates() |
|
| 475 | { |
||
| 476 | //Optimizamos |
||
| 477 | 1 | $this->tpl->addExtension(new \Twig_Extensions_Extension_I18n()); |
|
| 478 | 1 | } |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Method that extract all path tag for extracting translations |
||
| 482 | * @param array $domains |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | private function parsePathTranslations($domains) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Method that generate all template caches |
||
| 503 | */ |
||
| 504 | private function generateTemplatesCache() |
||
| 505 | { |
||
| 506 | /** @var \Twig_Loader_Filesystem $loader */ |
||
| 507 | $loader = $this->tpl->getLoader(); |
||
| 515 | } |
||
| 516 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.