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 | Config::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 $module |
||
103 | * @param $mod_path |
||
104 | * @param boolean $isModule |
||
105 | */ |
||
106 | private function createModulePathTree($module, $mod_path, $isModule = false) |
||
129 | |||
130 | /** |
||
131 | * Servicio que genera las plantillas básicas de ficheros del módulo |
||
132 | * @param string $module |
||
133 | * @param string $mod_path |
||
134 | * @param boolean $force |
||
135 | * @param string $controllerType |
||
136 | * @param boolean $isModule |
||
137 | */ |
||
138 | private function createModuleBaseFiles($module, $mod_path, $force = false, $controllerType = '', $isModule = false) |
||
150 | |||
151 | /** |
||
152 | * Servicio que ejecuta Propel y genera el modelo de datos |
||
153 | * @param string $module |
||
154 | * @param string $path |
||
155 | * @param boolean $isModule |
||
156 | */ |
||
157 | private function createModuleModels($module, $path, $isModule = false) |
||
179 | |||
180 | /** |
||
181 | * @param string $module |
||
182 | * @param string $mod_path |
||
183 | * @param boolean $force |
||
184 | * @param string $controllerType |
||
185 | * @return boolean |
||
186 | */ |
||
187 | private function generateControllerTemplate($module, $mod_path, $force = false, $controllerType = "") |
||
216 | |||
217 | /** |
||
218 | * @param string $module |
||
219 | * @param string $mod_path |
||
220 | * @param boolean $force |
||
221 | * @param boolean $isModule |
||
222 | * @return boolean |
||
223 | */ |
||
224 | private function generateBaseApiTemplate($module, $mod_path, $force = false, $isModule = false) |
||
249 | |||
250 | /** |
||
251 | * @param string $mod_path |
||
252 | * @param boolean $force |
||
253 | * @return boolean |
||
254 | */ |
||
255 | private function generateConfigTemplate($mod_path, $force = false) |
||
263 | |||
264 | /** |
||
265 | * @param string $mod_path |
||
266 | * @param string $mod_path |
||
267 | * @param boolean $force |
||
268 | * @return boolean |
||
269 | */ |
||
270 | private function generatePublicTemplates($mod_path, $force = false) |
||
282 | |||
283 | /** |
||
284 | * @param string $module |
||
285 | * @param string $mod_path |
||
286 | * @param boolean $force |
||
287 | * @return boolean |
||
288 | */ |
||
289 | private function generateServiceTemplate($module, $mod_path, $force = false) |
||
303 | |||
304 | /** |
||
305 | * @param string $module |
||
306 | * @param string $mod_path |
||
307 | * @param boolean $force |
||
308 | * @param boolean $isModule |
||
309 | * @return boolean |
||
310 | */ |
||
311 | private function genereateAutoloaderTemplate($module, $mod_path, $force = false, $isModule = false) |
||
323 | |||
324 | /** |
||
325 | * @param string $module |
||
326 | * @param string $mod_path |
||
327 | * @param boolean $force |
||
328 | * @return boolean |
||
329 | */ |
||
330 | private function generateSchemaTemplate($module, $mod_path, $force = false) |
||
344 | |||
345 | /** |
||
346 | * @param string $module |
||
347 | * @param string $mod_path |
||
348 | * @param boolean $force |
||
349 | * @return boolean |
||
350 | */ |
||
351 | private function generatePropertiesTemplate($module, $mod_path, $force = false) |
||
367 | |||
368 | /** |
||
369 | * @param string $module |
||
370 | * @param string $mod_path |
||
371 | * @param boolean $force |
||
372 | * @return boolean |
||
373 | */ |
||
374 | private function generateIndexTemplate($module, $mod_path, $force = false) |
||
385 | |||
386 | /** |
||
387 | * Método que graba el contenido de una plantilla en un fichero |
||
388 | * @param string $fileContent |
||
389 | * @param string $filename |
||
390 | * @param boolean $force |
||
391 | * @return boolean |
||
392 | */ |
||
393 | private function writeTemplateToFile($fileContent, $filename, $force = false) |
||
408 | |||
409 | /** |
||
410 | * Create ApiBase |
||
411 | * @param string $module |
||
412 | * @param string $mod_path |
||
413 | * @param string $api |
||
414 | * |
||
415 | * @return bool |
||
416 | */ |
||
417 | private function createApiBaseFile($module, $mod_path, $api) |
||
431 | |||
432 | /** |
||
433 | * Create Api |
||
434 | * @param string $module |
||
435 | * @param string $mod_path |
||
436 | * @param bool $force |
||
437 | * @param string $api |
||
438 | * |
||
439 | * @return bool |
||
440 | */ |
||
441 | private function createApi($module, $mod_path, $force, $api) |
||
454 | |||
455 | /** |
||
456 | * Method that copy resources recursively |
||
457 | * @param string $dest |
||
458 | * @param boolean $force |
||
459 | * @param $filename_path |
||
460 | * @param boolean $debug |
||
461 | */ |
||
462 | public static function copyResources($dest, $force, $filename_path, $debug) |
||
477 | |||
478 | /** |
||
479 | * Method that copy a resource |
||
480 | * @param string $src |
||
481 | * @param string $dst |
||
482 | * @throws ConfigException |
||
483 | */ |
||
484 | public static function copyr($src, $dst) |
||
499 | } |
||
500 |
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.