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 ModuleCreateCommand 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 ModuleCreateCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ModuleCreateCommand extends AbstractCommand |
||
27 | { |
||
28 | const TPL_ENGINE_LATTE = 'latte'; |
||
29 | const TPL_ENGINE_PLATES = 'plates'; |
||
30 | const TPL_ENGINE_PHP = 'php'; |
||
31 | const TPL_ENGINE_TWIG = 'twig'; |
||
32 | const TPL_ENGINE_SMARTY = 'smarty'; |
||
33 | |||
34 | const ROUTING_ENGINE_SYMFONY = 'symfony'; |
||
35 | const ROUTING_ENGINE_AURA = 'aura'; |
||
36 | const ROUTING_ENGINE_LARAVEL = 'laravel'; |
||
37 | const ROUTING_ENGINE_FASTROUTE = 'fastroute'; |
||
38 | const ROUTING_ENGINE_NULL = 'NullRouter'; |
||
39 | |||
40 | protected $skeletonModuleDir; |
||
41 | protected $modulesDir; |
||
42 | protected $moduleDir; |
||
43 | protected $moduleName; |
||
44 | protected $tplEngine; |
||
45 | protected $routingEngine; |
||
46 | protected $configEnabledTemplatingEngines = []; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $coreDirs = [ |
||
52 | 'src', |
||
53 | 'src/Controller', |
||
54 | 'tests', |
||
55 | 'resources', |
||
56 | 'resources/config' |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $coreFiles = [ |
||
63 | 'Module.php', |
||
64 | 'resources/config/config.php', |
||
65 | ]; |
||
66 | |||
67 | protected $tplEngineCoreFiles = [ |
||
68 | 'resources/views', |
||
69 | 'resources/views/index' |
||
70 | ]; |
||
71 | |||
72 | protected $routingEngineCoreFiles = [ |
||
73 | 'resources/routes' |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $tplEngineFilesMap = [ |
||
80 | self::TPL_ENGINE_LATTE => [ |
||
81 | 'resources/views/index/index.html.latte', |
||
82 | ], |
||
83 | self::TPL_ENGINE_PLATES => [ |
||
84 | 'resources/views/index/index.html.plates', |
||
85 | ], |
||
86 | self::TPL_ENGINE_PHP => [ |
||
87 | 'resources/views/index/index.html.php', |
||
88 | ], |
||
89 | self::TPL_ENGINE_TWIG => [ |
||
90 | 'resources/views/index/base.html.twig', |
||
91 | 'resources/views/index/index.html.twig', |
||
92 | ], |
||
93 | self::TPL_ENGINE_SMARTY => [ |
||
94 | 'resources/views/index/base.html.smarty', |
||
95 | 'resources/views/index/index.html.smarty', |
||
96 | ], |
||
97 | ]; |
||
98 | |||
99 | protected $routingEngineFilesMap = [ |
||
100 | self::ROUTING_ENGINE_SYMFONY => [ |
||
101 | 'src/Controller/Index.php', |
||
102 | 'src/Controller/Shared.php', |
||
103 | 'resources/routes/symfony.yml' |
||
104 | ], |
||
105 | self::ROUTING_ENGINE_AURA => [ |
||
106 | 'src/Controller/Index.php', |
||
107 | 'src/Controller/Shared.php', |
||
108 | 'resources/routes/aura.php', |
||
109 | ], |
||
110 | self::ROUTING_ENGINE_LARAVEL => [ |
||
111 | 'src/Controller/Index.php', |
||
112 | 'src/Controller/Shared.php', |
||
113 | 'resources/routes/laravel.php', |
||
114 | ], |
||
115 | self::ROUTING_ENGINE_FASTROUTE => [ |
||
116 | 'src/Controller/IndexInvoke.php', |
||
117 | 'src/Controller/Shared.php', |
||
118 | 'resources/routes/fastroute.php', |
||
119 | ], |
||
120 | ]; |
||
121 | |||
122 | protected $routingEngineTokenMap = [ |
||
123 | self::ROUTING_ENGINE_AURA => [ |
||
124 | '[ROUTING_LOAD_METHOD]' => 'loadAuraRoutes', |
||
125 | '[ROUTING_DEF_FILE]' => 'aura.php', |
||
126 | '[ROUTING_GETROUTES_RETVAL]' => '\Aura\Router\Router', |
||
127 | ], |
||
128 | self::ROUTING_ENGINE_LARAVEL => [ |
||
129 | '[ROUTING_LOAD_METHOD]' => 'loadLaravelRoutes', |
||
130 | '[ROUTING_DEF_FILE]' => 'laravel.php', |
||
131 | '[ROUTING_GETROUTES_RETVAL]' => '\Illuminate\Routing\Router', |
||
132 | ], |
||
133 | self::ROUTING_ENGINE_FASTROUTE => [ |
||
134 | '[ROUTING_LOAD_METHOD]' => 'loadFastRouteRoutes', |
||
135 | '[ROUTING_DEF_FILE]' => 'fastroute.php', |
||
136 | '[ROUTING_GETROUTES_RETVAL]' => '\PPI\FastRoute\Wrapper\FastRouteWrapper', |
||
137 | ], |
||
138 | ]; |
||
139 | |||
140 | /** |
||
141 | * @param string $moduleDir |
||
142 | */ |
||
143 | public function setSkeletonModuleDir($moduleDir) |
||
147 | |||
148 | /** |
||
149 | * @param string $moduleDir |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | public function setTargetModuleDir($moduleDir) |
||
157 | |||
158 | /** |
||
159 | * @param array $tplEngines |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | public function setEnabledTemplatingEngines(array $tplEngines) |
||
167 | |||
168 | /** |
||
169 | * @return void |
||
170 | */ |
||
171 | protected function configure() |
||
180 | |||
181 | /** |
||
182 | * @param InputInterface $input |
||
183 | * @param OutputInterface $output |
||
184 | * @throws \Exception |
||
185 | * @return void |
||
186 | */ |
||
187 | protected function execute(InputInterface $input, OutputInterface $output) |
||
227 | |||
228 | protected function isValidTemplatingEngine($tplEngine) |
||
238 | |||
239 | protected function isValidRoutingEngine($routingEngine) |
||
249 | |||
250 | /** |
||
251 | * @param string $moduleDir |
||
252 | * @param array $files |
||
253 | * @param array $tokens |
||
254 | */ |
||
255 | protected function replaceTokensInFiles($moduleDir, $files, $tokens) |
||
265 | |||
266 | /** |
||
267 | * @param string $skeletonDir |
||
268 | * @param string $moduleDir |
||
269 | * @param array $files |
||
270 | * |
||
271 | * @throws \InvalidArgumentException When a file path being created already exists |
||
272 | */ |
||
273 | protected function copyFiles($skeletonDir, $moduleDir, $files) |
||
287 | |||
288 | /** |
||
289 | * @param string $moduleDir |
||
290 | * @param string $moduleName |
||
291 | * |
||
292 | * @throws \InvalidArgumentException When a dir path being created already exists |
||
293 | */ |
||
294 | protected function createModuleStructure($moduleDir, $moduleName) |
||
308 | |||
309 | /** |
||
310 | * @param InputInterface $input |
||
311 | * @param OutputInterface $output |
||
312 | */ |
||
313 | protected function askQuestions(InputInterface $input, OutputInterface $output) |
||
334 | |||
335 | /** |
||
336 | * @param InputInterface $input |
||
337 | * @param OutputInterface $output |
||
338 | */ |
||
339 | private function checkEnabledRouters(InputInterface $input, OutputInterface $output) |
||
355 | |||
356 | /** |
||
357 | * @param InputInterface $input |
||
358 | * @param OutputInterface $output |
||
359 | */ |
||
360 | private function checkEnabledTemplatingEngines(InputInterface $input, OutputInterface $output) |
||
409 | |||
410 | /** |
||
411 | * @param InputInterface $input |
||
412 | * @param OutputInterface $output |
||
413 | * @return boolean |
||
414 | */ |
||
415 | View Code Duplication | private function askForTemplating(InputInterface $input, OutputInterface $output) |
|
422 | |||
423 | View Code Duplication | private function askForRouting(InputInterface $input, OutputInterface $output) |
|
430 | |||
431 | private function chooseTemplatingEngine($input, $output) |
||
448 | |||
449 | /** |
||
450 | * @param InputInterface $input |
||
451 | * @param OutputInterface $output |
||
452 | * |
||
453 | * @return void |
||
454 | */ |
||
455 | private function chooseRouter(InputInterface $input, OutputInterface $output) |
||
475 | |||
476 | /** |
||
477 | * @param $tplEngine |
||
478 | * |
||
479 | * @return void |
||
480 | */ |
||
481 | private function getTemplatingFilesFromEngine($tplEngine) |
||
487 | |||
488 | /** |
||
489 | * @return void |
||
490 | */ |
||
491 | private function processTemplatingFiles() |
||
516 | |||
517 | /** |
||
518 | * @throws \Exception |
||
519 | * |
||
520 | * @return void |
||
521 | */ |
||
522 | private function processRoutingFiles($tokenizedFiles, $tokens) |
||
568 | |||
569 | /** |
||
570 | * @return array |
||
571 | */ |
||
572 | protected function getTokenizedCoreFiles() |
||
580 | |||
581 | /** |
||
582 | * @param $routingEngine |
||
583 | * @return array |
||
584 | * @throws \Exception |
||
585 | */ |
||
586 | private function getRoutingTokenMap($routingEngine) { |
||
606 | |||
607 | |||
608 | } |
||
609 |
If you suppress an error, we recommend checking for the error condition explicitly: