Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AssetsParser 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 AssetsParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class AssetsParser { |
||
| 16 | |||
| 17 | protected $files = []; |
||
| 18 | protected $hash = []; |
||
| 19 | protected $compiled_files; |
||
| 20 | protected $type; |
||
| 21 | protected $path; |
||
| 22 | protected $domains = []; |
||
| 23 | private $debug = false; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor por defecto |
||
| 27 | * |
||
| 28 | * @param string $type |
||
| 29 | */ |
||
| 30 | public function __construct($type = 'js') |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Método que calcula el path completo a copiar un recurso |
||
| 40 | * @param string $filename_path |
||
| 41 | * @param string[] $source |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | protected static function calculateResourcePathname($filename_path, $source) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Método que añade un nuevo fichero al proceso de generación de los assets |
||
| 61 | * @param $filename |
||
| 62 | * @return AssetsParser |
||
| 63 | * @internal param string $type |
||
| 64 | * |
||
| 65 | */ |
||
| 66 | public function addFile($filename) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Método que establece el hash con el que compilar los assets |
||
| 83 | * @param $hash |
||
| 84 | * |
||
| 85 | * @return AssetsParser |
||
| 86 | */ |
||
| 87 | public function setHash($hash) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Método que procesa los ficheros solicitados en función del modo de ejecución |
||
| 95 | * @return AssetsParser |
||
| 96 | * @internal param string $type |
||
| 97 | * @throws ConfigException |
||
| 98 | */ |
||
| 99 | public function compile() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Método que compila los ficheros css y los procesa en función del modo de ejecución |
||
| 114 | * @return AssetsParser |
||
| 115 | * @throws ConfigException |
||
| 116 | */ |
||
| 117 | protected function compileCss() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Método que compila los ficheros javascript en función del modo de ejecución |
||
| 135 | * @return $this |
||
| 136 | * @throws ConfigException |
||
| 137 | */ |
||
| 138 | protected function compileJs() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Método para guardar cualquier contenido y controlar que existe el directorio y se guarda correctamente |
||
| 167 | * @param string $path |
||
| 168 | * @param string $content |
||
| 169 | * @throws ConfigException |
||
| 170 | */ |
||
| 171 | private function storeContents($path, $content = "") { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Método que imprime el resultado de la generación de los assets |
||
| 180 | */ |
||
| 181 | public function printHtml() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Método que devuelve el html con la ruta compilada del recurso javascript |
||
| 192 | */ |
||
| 193 | View Code Duplication | protected function printJs() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Método que devuelve el html con la ruta compilada del recurso css |
||
| 206 | */ |
||
| 207 | View Code Duplication | protected function printCss() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $source |
||
| 220 | * @param string $file |
||
| 221 | */ |
||
| 222 | protected function extractCssResources($source, $file) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Método que procesa cada línea de la hoja de estilos para copiar los recursos asociados |
||
| 245 | * @param string $file |
||
| 246 | * @param string $base |
||
| 247 | * @param string $data |
||
| 248 | * @return string |
||
| 249 | * @throws ConfigException |
||
| 250 | */ |
||
| 251 | protected function processCssLine($file, $base, $data) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $path_parts |
||
| 277 | * @param string $base |
||
| 278 | * @param $file |
||
| 279 | * @return string |
||
| 280 | * @throws ConfigException |
||
| 281 | */ |
||
| 282 | protected function putDebugJs($path_parts, $base, $file) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $base |
||
| 295 | * @param $file |
||
| 296 | * @param string $data |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | * @throws ConfigException |
||
| 300 | */ |
||
| 301 | protected function putProductionJs($base, $file, $data) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Servicio que busca el path para un dominio dado |
||
| 313 | * @param $string |
||
| 314 | * @param string $file_path |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public static function findDomainPath($string, $file_path) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Método que calcula el path de un recurso web |
||
| 337 | * @param string $string |
||
| 338 | * @param string $name |
||
| 339 | * @param boolean $return |
||
| 340 | * @param string $filename_path |
||
| 341 | * |
||
| 342 | * @return string[] |
||
| 343 | */ |
||
| 344 | public static function calculateAssetPath($string, $name, $return, $filename_path) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Método que extrae el recurso css de una línea de estilos css |
||
| 396 | * @param $handle |
||
| 397 | * @param string $filename_path |
||
| 398 | * @throws ConfigException |
||
| 399 | */ |
||
| 400 | public static function extractCssLineResource($handle, $filename_path) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Método que extrae el nombre del fichero de un recurso |
||
| 419 | * @param string $source |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | protected function extractSourceFilename($source) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $file |
||
| 439 | */ |
||
| 440 | protected function loopCssLines($file) |
||
| 456 | } |
||
| 457 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.