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 |
||
| 17 | class Template |
||
| 18 | { |
||
| 19 | use SingletonTrait; |
||
| 20 | use OutputTrait; |
||
| 21 | use RouteTrait; |
||
| 22 | const STATUS_OK = 'HTTP/1.0 200 OK'; |
||
| 23 | /** |
||
| 24 | * @var \Twig_Environment tpl |
||
| 25 | */ |
||
| 26 | protected $tpl; |
||
| 27 | protected $filters = array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor por defecto |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 39 | 1 | ||
| 40 | 1 | /** |
|
| 41 | 1 | * Método que devuelve el loader del Template |
|
| 42 | 1 | * @return \Twig_LoaderInterface |
|
| 43 | 1 | */ |
|
| 44 | public function getLoader() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Método que activa la zona pública |
||
| 51 | * @param bool $public |
||
| 52 | * |
||
| 53 | * @return Template |
||
| 54 | */ |
||
| 55 | public function setPublicZone($public = true) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Método que procesa la plantilla |
||
| 63 | * |
||
| 64 | * @param string $tpl |
||
| 65 | * @param array $vars |
||
| 66 | * @param array $cookies |
||
| 67 | * |
||
| 68 | * @return string HTML |
||
|
|
|||
| 69 | */ |
||
| 70 | public function render($tpl, array $vars = array(), array $cookies = array()) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Método que añade una nueva ruta al path de Twig |
||
| 81 | * @param $path |
||
| 82 | * @param $domain |
||
| 83 | * |
||
| 84 | * @return Template |
||
| 85 | */ |
||
| 86 | public function addPath($path, $domain = '') |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Método que devuelve el contenido de una plantilla |
||
| 94 | * @param string $tpl |
||
| 95 | * @param array $vars |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function dump($tpl, array $vars = array()) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Método que añade una función al motor de plantillas |
||
| 115 | * @param string $templateFunction |
||
| 116 | * @param $functionName |
||
| 117 | * |
||
| 118 | * @return Template |
||
| 119 | */ |
||
| 120 | protected function addTemplateFunction($templateFunction, $functionName) |
||
| 126 | 1 | ||
| 127 | 1 | /** |
|
| 128 | 1 | * Funcion Twig para los assets en las plantillas |
|
| 129 | 1 | * @return Template |
|
| 130 | */ |
||
| 131 | private function addAssetFunction() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Función que pinta un formulario |
||
| 138 | * @return Template |
||
| 139 | */ |
||
| 140 | private function addFormsFunction() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Función que pinta un campo de un formulario |
||
| 147 | * @return Template |
||
| 148 | */ |
||
| 149 | private function addFormWidgetFunction() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Función que pinta un botón de un formulario |
||
| 156 | * @return Template |
||
| 157 | */ |
||
| 158 | private function addFormButtonFunction() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Método que devuelve un parámetro de configuración en la plantilla |
||
| 165 | * @return Template |
||
| 166 | */ |
||
| 167 | private function addConfigFunction() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Método que añade la función path a Twig |
||
| 174 | * @return Template |
||
| 175 | */ |
||
| 176 | private function addRouteFunction() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Método que copia directamente el recurso solicitado a la carpeta pública |
||
| 183 | * @return Template |
||
| 184 | */ |
||
| 185 | private function addResourceFunction() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return Template |
||
| 192 | */ |
||
| 193 | private function addSessionFunction() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return Template |
||
| 200 | */ |
||
| 201 | private function addExistsFlashFunction() |
||
| 205 | |||
| 206 | 1 | /** |
|
| 207 | * @return Template |
||
| 208 | 1 | */ |
|
| 209 | 1 | private function addGetFlashFunction() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Servicio que regenera todas las plantillas |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function regenerateTemplates() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param $tplDir |
||
| 232 | * @param string $domain |
||
| 233 | * |
||
| 234 | * @return mixed |
||
| 235 | */ |
||
| 236 | protected function generateTemplate($tplDir, $domain = '') |
||
| 251 | 1 | ||
| 252 | /** |
||
| 253 | 1 | * Método que extrae el path de un string |
|
| 254 | * @param $path |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public static function extractPath($path) |
||
| 267 | |||
| 268 | /** |
||
| 269 | 1 | * Método que devuelve los dominios de una plataforma |
|
| 270 | * @param bool $append |
||
| 271 | 1 | * @return array |
|
| 272 | */ |
||
| 273 | static public function getDomains($append = false) |
||
| 285 | |||
| 286 | /** |
||
| 287 | 1 | * Método que añade todas las funciones de las plantillas |
|
| 288 | */ |
||
| 289 | 1 | private function addTemplateFunctions() |
|
| 303 | |||
| 304 | /** |
||
| 305 | 1 | * Método que devuelve el motod de plantillas |
|
| 306 | * @return \Twig_Environment |
||
| 307 | 1 | */ |
|
| 308 | public function getTemplateEngine() |
||
| 312 | |||
| 313 | 1 | /** |
|
| 314 | * Method that extract all domains for using them with the templates |
||
| 315 | 1 | */ |
|
| 316 | private function loadDomains() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Método que inicializa el motor de plantillas |
||
| 328 | */ |
||
| 329 | 1 | private function setup() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Método que inyecta los parseadores |
||
| 342 | */ |
||
| 343 | private function addTemplateTokens() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
| 352 | */ |
||
| 353 | private function optimizeTemplates() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Method that extract all path tag for extracting translations |
||
| 361 | * @param array $domains |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | private function parsePathTranslations($domains) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Method that generate all template caches |
||
| 382 | */ |
||
| 383 | private function generateTemplatesCache() |
||
| 394 | } |
||
| 395 |
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.