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 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 | * @Inyectable |
||
12 | * @var \PSFS\base\config\Config Servicio de configuración |
||
13 | */ |
||
14 | protected $config; |
||
15 | /** |
||
16 | * @Inyectable |
||
17 | * @var \PSFS\base\Security Servicio de autenticación |
||
18 | */ |
||
19 | protected $security; |
||
20 | /** |
||
21 | * @Inyectable |
||
22 | * @var \PSFS\base\Template Servicio de gestión de plantillas |
||
23 | */ |
||
24 | protected $tpl; |
||
25 | |||
26 | /** |
||
27 | * Método que revisa las traducciones directorio a directorio |
||
28 | * @param $path |
||
29 | * @param $locale |
||
30 | * @return array |
||
31 | */ |
||
32 | public static function findTranslations($path, $locale) |
||
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 | * @return mixed |
||
69 | */ |
||
70 | public function createStructureModule($module, $force = false, $type = "") |
||
82 | |||
83 | /** |
||
84 | * Servicio que genera el path base del módulo |
||
85 | * @param string $module |
||
86 | * @param string $mod_path |
||
87 | */ |
||
88 | private function createModulePath($module, $mod_path) |
||
95 | |||
96 | /** |
||
97 | * Servicio que genera la estructura base |
||
98 | * @param $module |
||
99 | * @param $mod_path |
||
100 | */ |
||
101 | private function createModulePathTree($module, $mod_path) |
||
115 | |||
116 | /** |
||
117 | * Servicio que genera las plantillas básicas de ficheros del módulo |
||
118 | * @param string $module |
||
119 | * @param string $mod_path |
||
120 | * @param boolean $force |
||
121 | * @param string $controllerType |
||
122 | */ |
||
123 | private function createModuleBaseFiles($module, $mod_path, $force = false, $controllerType = '') |
||
134 | |||
135 | /** |
||
136 | * Servicio que ejecuta Propel y genera el modelo de datos |
||
137 | * @param string $module |
||
138 | */ |
||
139 | private function createModuleModels($module) |
||
153 | |||
154 | /** |
||
155 | * @param string $module |
||
156 | * @param string $mod_path |
||
157 | * @param boolean $force |
||
158 | * @param string $controllerType |
||
159 | * @return boolean |
||
160 | */ |
||
161 | View Code Duplication | private function generateControllerTemplate($module, $mod_path, $force = false, $controllerType = "") |
|
171 | |||
172 | /** |
||
173 | * @param string $module |
||
174 | * @param string $mod_path |
||
175 | * @param boolean $force |
||
176 | * @param string $controllerType |
||
177 | * @return boolean |
||
178 | */ |
||
179 | private function generateBaseApiTemplate($module, $mod_path, $force = false, $controllerType = "") |
||
196 | |||
197 | /** |
||
198 | * @param string $module |
||
199 | * @param string $mod_path |
||
200 | * @param boolean $force |
||
201 | * @return boolean |
||
202 | */ |
||
203 | private function generateConfigTemplate($module, $mod_path, $force = false) |
||
209 | |||
210 | /** |
||
211 | * @param string $module |
||
212 | * @param string $mod_path |
||
213 | * @param boolean $force |
||
214 | * @return boolean |
||
215 | */ |
||
216 | private function generatePublicTemplates($module, $mod_path, $force = false) |
||
224 | |||
225 | /** |
||
226 | * @param string $module |
||
227 | * @param string $mod_path |
||
228 | * @param boolean $force |
||
229 | * @return boolean |
||
230 | */ |
||
231 | View Code Duplication | private function generateServiceTemplate($module, $mod_path, $force = false) |
|
240 | |||
241 | /** |
||
242 | * @param string $module |
||
243 | * @param string $mod_path |
||
244 | * @param boolean $force |
||
245 | * @return boolean |
||
246 | */ |
||
247 | private function genereateAutoloaderTemplate($module, $mod_path, $force = false) |
||
256 | |||
257 | /** |
||
258 | * @param string $module |
||
259 | * @param string $mod_path |
||
260 | * @param boolean $force |
||
261 | * @return boolean |
||
262 | */ |
||
263 | View Code Duplication | private function generateSchemaTemplate($module, $mod_path, $force = false) |
|
273 | |||
274 | /** |
||
275 | * @param string $module |
||
276 | * @param string $mod_path |
||
277 | * @param boolean $force |
||
278 | * @return boolean |
||
279 | */ |
||
280 | private function generatePropertiesTemplate($module, $mod_path, $force = false) |
||
293 | |||
294 | /** |
||
295 | * @param string $module |
||
296 | * @param string $mod_path |
||
297 | * @param boolean $force |
||
298 | * @return boolean |
||
299 | */ |
||
300 | View Code Duplication | private function generateIndexTemplate($module, $mod_path, $force = false) |
|
309 | |||
310 | /** |
||
311 | * Método que graba el contenido de una plantilla en un fichero |
||
312 | * @param string $fileContent |
||
313 | * @param string $filename |
||
314 | * @param boolean $force |
||
315 | * @return boolean |
||
316 | */ |
||
317 | private function writeTemplateToFile($fileContent, $filename, $force = false) { |
||
331 | |||
332 | /** |
||
333 | * Create ApiBase |
||
334 | * @param string $module |
||
335 | * @param string $mod_path |
||
336 | * @param string $api |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | private function createApiBaseFile($module, $mod_path, $api) |
||
349 | |||
350 | /** |
||
351 | * Create Api |
||
352 | * @param string $module |
||
353 | * @param string $mod_path |
||
354 | * @param bool $force |
||
355 | * @param string $api |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | View Code Duplication | private function createApi($module, $mod_path, $force, $api) |
|
368 | |||
369 | /** |
||
370 | * Method that copy resources recursively |
||
371 | * @param string $dest |
||
372 | * @param boolean $force |
||
373 | * @param $filename_path |
||
374 | * @param boolean $debug |
||
375 | */ |
||
376 | public static function copyResources($dest, $force, $filename_path, $debug) |
||
391 | |||
392 | /** |
||
393 | * Method that copy a resource |
||
394 | * @param string $src |
||
395 | * @param string $dst |
||
396 | * @throws ConfigException |
||
397 | */ |
||
398 | public static function copyr($src, $dst) { |
||
412 | } |
||
413 |
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.