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 |
||
| 22 | class GeneratorService extends Service |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @Injectable |
||
| 26 | * @var \PSFS\base\config\Config Servicio de configuración |
||
| 27 | */ |
||
| 28 | protected $config; |
||
| 29 | /** |
||
| 30 | * @Injectable |
||
| 31 | * @var \PSFS\base\Security Servicio de autenticación |
||
| 32 | */ |
||
| 33 | protected $security; |
||
| 34 | /** |
||
| 35 | * @Injectable |
||
| 36 | * @var \PSFS\base\Template Servicio de gestión de plantillas |
||
| 37 | */ |
||
| 38 | protected $tpl; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Método que revisa las traducciones directorio a directorio |
||
| 42 | * @param $path |
||
| 43 | * @param $locale |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | public static function findTranslations($path, $locale) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Servicio que genera la estructura de un módulo o lo actualiza en caso de ser necesario |
||
| 78 | * @param string $module |
||
| 79 | * @param boolean $force |
||
| 80 | * @param string $type |
||
| 81 | * @param string $apiClass |
||
| 82 | * @return mixed |
||
| 83 | */ |
||
| 84 | public function createStructureModule($module, $force = false, $type = "", $apiClass = "") |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Service that creates the root paths for the modules |
||
| 99 | * @param string $module |
||
| 100 | * @param string $mod_path |
||
| 101 | */ |
||
| 102 | private function createModulePath($module, $mod_path) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Servicio que genera la estructura base |
||
| 112 | * @param string $module |
||
| 113 | * @param boolean $mod_path |
||
| 114 | * @return boolean |
||
| 115 | */ |
||
| 116 | private function createModulePathTree($module, $mod_path) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Servicio que genera las plantillas básicas de ficheros del módulo |
||
| 137 | * @param string $module |
||
| 138 | * @param string $mod_path |
||
| 139 | * @param boolean $force |
||
| 140 | * @param string $controllerType |
||
| 141 | */ |
||
| 142 | private function createModuleBaseFiles($module, $mod_path, $force = false, $controllerType = '') |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Servicio que ejecuta Propel y genera el modelo de datos |
||
| 157 | * @param string $module |
||
| 158 | * @param string $path |
||
| 159 | */ |
||
| 160 | private function createModuleModels($module, $path) |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $module |
||
| 181 | * @param string $mod_path |
||
| 182 | * @param boolean $force |
||
| 183 | * @param string $controllerType |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | private function generateControllerTemplate($module, $mod_path, $force = false, $controllerType = "") |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $module |
||
| 230 | * @param string $mod_path |
||
| 231 | * @param boolean $force |
||
| 232 | * @param string $apiClass |
||
| 233 | * @return boolean |
||
| 234 | */ |
||
| 235 | private function generateBaseApiTemplate($module, $mod_path, $force = false, $apiClass = "") |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $mod_path |
||
| 259 | * @param boolean $force |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | private function generateConfigTemplate($mod_path, $force = false) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $mod_path |
||
| 273 | * @param string $mod_path |
||
| 274 | * @param boolean $force |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | private function generatePublicTemplates($mod_path, $force = false) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $module |
||
| 292 | * @param string $mod_path |
||
| 293 | * @param boolean $force |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | private function generateServiceTemplate($module, $mod_path, $force = false) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $module |
||
| 313 | * @param string $mod_path |
||
| 314 | * @param boolean $force |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | private function genereateAutoloaderTemplate($module, $mod_path, $force = false) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $module |
||
| 338 | * @param string $mod_path |
||
| 339 | * @param boolean $force |
||
| 340 | * @return boolean |
||
| 341 | */ |
||
| 342 | private function generateSchemaTemplate($module, $mod_path, $force = false) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $module |
||
| 359 | * @param string $mod_path |
||
| 360 | * @param boolean $force |
||
| 361 | * @return boolean |
||
| 362 | */ |
||
| 363 | private function generatePropertiesTemplate($module, $mod_path, $force = false) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $module |
||
| 377 | * @param string $mod_path |
||
| 378 | * @param boolean $force |
||
| 379 | * @return boolean |
||
| 380 | */ |
||
| 381 | private function generateIndexTemplate($module, $mod_path, $force = false) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Método que graba el contenido de una plantilla en un fichero |
||
| 395 | * @param string $fileContent |
||
| 396 | * @param string $filename |
||
| 397 | * @param boolean $force |
||
| 398 | * @return boolean |
||
| 399 | */ |
||
| 400 | private function writeTemplateToFile($fileContent, $filename, $force = false) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Create ApiBase |
||
| 418 | * @param string $module |
||
| 419 | * @param string $mod_path |
||
| 420 | * @param string $api |
||
| 421 | * @param string $apiClass |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | private function createApiBaseFile($module, $mod_path, $api, $apiClass = '') |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Create Api |
||
| 445 | * @param string $module |
||
| 446 | * @param string $mod_path |
||
| 447 | * @param bool $force |
||
| 448 | * @param string $api |
||
| 449 | * |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | private function createApi($module, $mod_path, $force, $api) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Method that copy resources recursively |
||
| 468 | * @param string $dest |
||
| 469 | * @param boolean $force |
||
| 470 | * @param $filename_path |
||
| 471 | * @param boolean $debug |
||
| 472 | */ |
||
| 473 | 1 | public static function copyResources($dest, $force, $filename_path, $debug) |
|
| 474 | { |
||
| 475 | 1 | if (file_exists($filename_path)) { |
|
| 476 | 1 | $destfolder = basename($filename_path); |
|
| 477 | 1 | if (!file_exists(WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder) || $debug || $force) { |
|
| 478 | 1 | if (is_dir($filename_path)) { |
|
| 479 | 1 | self::copyr($filename_path, WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder); |
|
| 480 | } else { |
||
| 481 | if (@copy($filename_path, WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder) === FALSE) { |
||
| 482 | throw new ConfigException("Can't copy " . $filename_path . " to " . WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | 1 | } |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Method that copy a resource |
||
| 491 | * @param string $src |
||
| 492 | * @param string $dst |
||
| 493 | * @throws ConfigException |
||
| 494 | */ |
||
| 495 | 1 | public static function copyr($src, $dst) |
|
| 496 | { |
||
| 497 | 1 | $dir = opendir($src); |
|
| 498 | 1 | GeneratorHelper::createDir($dst); |
|
| 499 | 1 | while (false !== ($file = readdir($dir))) { |
|
| 500 | 1 | if (($file != '.') && ($file != '..')) { |
|
| 501 | 1 | if (is_dir($src . '/' . $file)) { |
|
| 502 | self::copyr($src . '/' . $file, $dst . '/' . $file); |
||
| 503 | 1 | } elseif (@copy($src . '/' . $file, $dst . '/' . $file) === false) { |
|
| 504 | throw new ConfigException("Can't copy " . $src . " to " . $dst); |
||
| 505 | } |
||
| 506 | } |
||
| 507 | } |
||
| 508 | 1 | closedir($dir); |
|
| 509 | 1 | } |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @param $module_path |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | private function getPropelPaths($module_path) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @param string $module_path |
||
| 537 | * @return GeneratorConfig |
||
| 538 | */ |
||
| 539 | private function getConfigGenerator($module_path) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param GeneratorConfig $configGenerator |
||
| 556 | */ |
||
| 557 | private function buildModels(GeneratorConfig $configGenerator) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param GeneratorConfig $configGenerator |
||
| 567 | */ |
||
| 568 | private function buildSql(GeneratorConfig $configGenerator) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param GeneratorConfig $configGenerator |
||
| 581 | * @param AbstractManager $manager |
||
| 582 | * @param string $workingDir |
||
| 583 | */ |
||
| 584 | private function setupManager(GeneratorConfig $configGenerator, AbstractManager &$manager, $workingDir = CORE_DIR) |
||
| 594 | } |
||
| 595 |
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.