Complex classes like GeneratorService 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 GeneratorService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class GeneratorService extends Service |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @Inyectable |
||
| 13 | * @var \PSFS\base\config\Config Servicio de configuración |
||
| 14 | */ |
||
| 15 | protected $config; |
||
| 16 | /** |
||
| 17 | * @Inyectable |
||
| 18 | * @var \PSFS\base\Security Servicio de autenticación |
||
| 19 | */ |
||
| 20 | protected $security; |
||
| 21 | /** |
||
| 22 | * @Inyectable |
||
| 23 | * @var \PSFS\base\Template Servicio de gestión de plantillas |
||
| 24 | */ |
||
| 25 | protected $tpl; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Método que revisa las traducciones directorio a directorio |
||
| 29 | * @param $path |
||
| 30 | * @param $locale |
||
| 31 | * @return array |
||
| 32 | */ |
||
| 33 | public static function findTranslations($path, $locale) |
||
| 34 | { |
||
| 35 | $locale_path = realpath(BASE_DIR . DIRECTORY_SEPARATOR . 'locale'); |
||
| 36 | $locale_path .= DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR; |
||
| 37 | |||
| 38 | $translations = array(); |
||
| 39 | if (file_exists($path)) { |
||
| 40 | $d = dir($path); |
||
|
|
|||
| 41 | while (false !== ($dir = $d->read())) { |
||
| 42 | GeneratorHelper::createDir($locale_path); |
||
| 43 | if (!file_exists($locale_path . 'translations.po')) { |
||
| 44 | file_put_contents($locale_path . 'translations.po', ''); |
||
| 45 | } |
||
| 46 | $inspect_path = realpath($path . DIRECTORY_SEPARATOR . $dir); |
||
| 47 | $cmd_php = "export PATH=\$PATH:/opt/local/bin; xgettext " . |
||
| 48 | $inspect_path . DIRECTORY_SEPARATOR . |
||
| 49 | "*.php --from-code=UTF-8 -j -L PHP --debug --force-po -o {$locale_path}translations.po"; |
||
| 50 | if (is_dir($path . DIRECTORY_SEPARATOR . $dir) && preg_match('/^\./', $dir) == 0) { |
||
| 51 | $res = _('Revisando directorio: ') . $inspect_path; |
||
| 52 | $res .= _('Comando ejecutado: ') . $cmd_php; |
||
| 53 | $res .= shell_exec($cmd_php); |
||
| 54 | usleep(10); |
||
| 55 | $translations[] = $res; |
||
| 56 | $translations = array_merge($translations, self::findTranslations($inspect_path, $locale)); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | return $translations; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Servicio que genera la estructura de un módulo o lo actualiza en caso de ser necesario |
||
| 65 | * @param string $module |
||
| 66 | * @param boolean $force |
||
| 67 | * @param string $type |
||
| 68 | * @param boolean $isModule |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public function createStructureModule($module, $force = false, $type = "", $isModule = false) |
||
| 72 | { |
||
| 73 | $mod_path = CORE_DIR . DIRECTORY_SEPARATOR; |
||
| 74 | $module = ucfirst($module); |
||
| 75 | $this->createModulePath($module, $mod_path, $isModule); |
||
| 76 | $this->createModulePathTree($module, $mod_path, $isModule); |
||
| 77 | $this->createModuleBaseFiles($module, $mod_path, $force, $type, $isModule); |
||
| 78 | $this->createModuleModels($module, $mod_path, $isModule); |
||
| 79 | $this->generateBaseApiTemplate($module, $mod_path, $force, $isModule); |
||
| 80 | //Redireccionamos al home definido |
||
| 81 | $this->log->infoLog("Módulo generado correctamente"); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Service that creates the root paths for the modules |
||
| 86 | * @param string $module |
||
| 87 | * @param string $mod_path |
||
| 88 | * @param boolean $isModule |
||
| 89 | */ |
||
| 90 | private function createModulePath($module, $mod_path, $isModule = false) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Servicio que genera la estructura base |
||
| 102 | * @param string $module |
||
| 103 | * @param boolean $mod_path |
||
| 104 | * @param boolean $isModule |
||
| 105 | * @return boolean |
||
| 106 | */ |
||
| 107 | private function createModulePathTree($module, $mod_path, $isModule = false) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Servicio que genera las plantillas básicas de ficheros del módulo |
||
| 133 | * @param string $module |
||
| 134 | * @param string $mod_path |
||
| 135 | * @param boolean $force |
||
| 136 | * @param string $controllerType |
||
| 137 | * @param boolean $isModule |
||
| 138 | */ |
||
| 139 | private function createModuleBaseFiles($module, $mod_path, $force = false, $controllerType = '', $isModule = false) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Servicio que ejecuta Propel y genera el modelo de datos |
||
| 154 | * @param string $module |
||
| 155 | * @param string $path |
||
| 156 | * @param boolean $isModule |
||
| 157 | */ |
||
| 158 | private function createModuleModels($module, $path, $isModule = false) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $module |
||
| 187 | * @param string $mod_path |
||
| 188 | * @param boolean $force |
||
| 189 | * @param string $controllerType |
||
| 190 | * @return boolean |
||
| 191 | */ |
||
| 192 | private function generateControllerTemplate($module, $mod_path, $force = false, $controllerType = "") |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $module |
||
| 233 | * @param string $mod_path |
||
| 234 | * @param boolean $force |
||
| 235 | * @param boolean $isModule |
||
| 236 | * @return boolean |
||
| 237 | */ |
||
| 238 | private function generateBaseApiTemplate($module, $mod_path, $force = false, $isModule = false) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $mod_path |
||
| 266 | * @param boolean $force |
||
| 267 | * @return boolean |
||
| 268 | */ |
||
| 269 | private function generateConfigTemplate($mod_path, $force = false) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $mod_path |
||
| 280 | * @param string $mod_path |
||
| 281 | * @param boolean $force |
||
| 282 | * @return boolean |
||
| 283 | */ |
||
| 284 | private function generatePublicTemplates($mod_path, $force = false) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $module |
||
| 299 | * @param string $mod_path |
||
| 300 | * @param boolean $force |
||
| 301 | * @return boolean |
||
| 302 | */ |
||
| 303 | private function generateServiceTemplate($module, $mod_path, $force = false) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $module |
||
| 320 | * @param string $mod_path |
||
| 321 | * @param boolean $force |
||
| 322 | * @param boolean $isModule |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | private function genereateAutoloaderTemplate($module, $mod_path, $force = false, $isModule = false) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $module |
||
| 348 | * @param string $mod_path |
||
| 349 | * @param boolean $force |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | private function generateSchemaTemplate($module, $mod_path, $force = false) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $module |
||
| 369 | * @param string $mod_path |
||
| 370 | * @param boolean $force |
||
| 371 | * @return boolean |
||
| 372 | */ |
||
| 373 | private function generatePropertiesTemplate($module, $mod_path, $force = false) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $module |
||
| 392 | * @param string $mod_path |
||
| 393 | * @param boolean $force |
||
| 394 | * @return boolean |
||
| 395 | */ |
||
| 396 | private function generateIndexTemplate($module, $mod_path, $force = false) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Método que graba el contenido de una plantilla en un fichero |
||
| 410 | * @param string $fileContent |
||
| 411 | * @param string $filename |
||
| 412 | * @param boolean $force |
||
| 413 | * @return boolean |
||
| 414 | */ |
||
| 415 | private function writeTemplateToFile($fileContent, $filename, $force = false) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Create ApiBase |
||
| 433 | * @param string $module |
||
| 434 | * @param string $mod_path |
||
| 435 | * @param string $api |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | private function createApiBaseFile($module, $mod_path, $api) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Create Api |
||
| 456 | * @param string $module |
||
| 457 | * @param string $mod_path |
||
| 458 | * @param bool $force |
||
| 459 | * @param string $api |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | private function createApi($module, $mod_path, $force, $api) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Method that copy resources recursively |
||
| 479 | * @param string $dest |
||
| 480 | * @param boolean $force |
||
| 481 | * @param $filename_path |
||
| 482 | * @param boolean $debug |
||
| 483 | */ |
||
| 484 | public static function copyResources($dest, $force, $filename_path, $debug) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Method that copy a resource |
||
| 502 | * @param string $src |
||
| 503 | * @param string $dst |
||
| 504 | * @throws ConfigException |
||
| 505 | */ |
||
| 506 | public static function copyr($src, $dst) |
||
| 521 | } |
||
| 522 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.