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 |
||
25 | class ModuleCreateCommand extends AbstractCommand |
||
26 | { |
||
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 | |||
39 | protected $skeletonModuleDir; |
||
40 | protected $modulesDir; |
||
41 | protected $tplEngine; |
||
42 | protected $routingEngine; |
||
43 | protected $configEnabledTemplatingEngines = []; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $coreDirs = [ |
||
49 | 'src', |
||
50 | 'src/Controller', |
||
51 | 'tests', |
||
52 | 'resources', |
||
53 | 'resources/routes', |
||
54 | 'resources/config', |
||
55 | 'resources/views', |
||
56 | 'resources/views/index' |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $coreFiles = [ |
||
63 | 'Module.php', |
||
64 | 'resources/config/config.php' |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $tplEngineFilesMap = [ |
||
71 | self::TPL_ENGINE_LATTE => [ |
||
72 | 'resources/views/index/index.html.latte' |
||
73 | ], |
||
74 | self::TPL_ENGINE_PLATES => [ |
||
75 | 'resources/views/index/index.html.plates' |
||
76 | ], |
||
77 | self::TPL_ENGINE_PHP => [ |
||
78 | 'resources/views/index/index.html.php' |
||
79 | ], |
||
80 | self::TPL_ENGINE_TWIG => [ |
||
81 | 'resources/views/index/base.html.twig', |
||
82 | 'resources/views/index/index.html.twig' |
||
83 | ], |
||
84 | self::TPL_ENGINE_SMARTY => [ |
||
85 | 'resources/views/index/base.html.smarty', |
||
86 | 'resources/views/index/index.html.smarty' |
||
87 | ] |
||
88 | ]; |
||
89 | |||
90 | protected $routingEngineFilesMap = [ |
||
91 | self::ROUTING_ENGINE_SYMFONY => [ |
||
92 | 'src/Controller/Index.php', |
||
93 | 'src/Controller/Shared.php', |
||
94 | 'resources/routes/symfony.yml' |
||
95 | ], |
||
96 | self::ROUTING_ENGINE_AURA => [ |
||
97 | 'src/Controller/Index.php', |
||
98 | 'src/Controller/Shared.php', |
||
99 | 'resources/routes/aura.php' |
||
100 | ], |
||
101 | self::ROUTING_ENGINE_LARAVEL => [ |
||
102 | 'src/Controller/Index.php', |
||
103 | 'src/Controller/Shared.php', |
||
104 | 'resources/routes/laravel.php' |
||
105 | ], |
||
106 | self::ROUTING_ENGINE_FASTROUTE => [ |
||
107 | 'src/Controller/IndexInvoke.php', |
||
108 | 'src/Controller/Shared.php', |
||
109 | 'resources/routes/fastroute.php' |
||
110 | ], |
||
111 | ]; |
||
112 | |||
113 | protected $routingEngineTokenMap = [ |
||
114 | self::ROUTING_ENGINE_SYMFONY => [ |
||
115 | '[ROUTING_LOAD_METHOD]' => 'loadSymfonyRoutes', |
||
116 | '[ROUTING_DEF_FILE]' => 'symfony.yml', |
||
117 | '[ROUTING_GETROUTES_RETVAL]' => '\Symfony\Component\Routing\RouteCollection' |
||
118 | ], |
||
119 | self::ROUTING_ENGINE_AURA => [ |
||
120 | '[ROUTING_LOAD_METHOD]' => 'loadAuraRoutes', |
||
121 | '[ROUTING_DEF_FILE]' => 'aura.php', |
||
122 | '[ROUTING_GETROUTES_RETVAL]' => '\Aura\Router\Router' |
||
123 | ], |
||
124 | self::ROUTING_ENGINE_LARAVEL => [ |
||
125 | '[ROUTING_LOAD_METHOD]' => 'loadLaravelRoutes', |
||
126 | '[ROUTING_DEF_FILE]' => 'laravel.php', |
||
127 | '[ROUTING_GETROUTES_RETVAL]' => '\Illuminate\Routing\Router' |
||
128 | ], |
||
129 | self::ROUTING_ENGINE_FASTROUTE => [ |
||
130 | '[ROUTING_LOAD_METHOD]' => 'loadFastRouteRoutes', |
||
131 | '[ROUTING_DEF_FILE]' => 'fastroute.php', |
||
132 | '[ROUTING_GETROUTES_RETVAL]' => '\PPI\FastRoute\Wrapper\FastRouteWrapper' |
||
133 | ] |
||
134 | ]; |
||
135 | |||
136 | /** |
||
137 | * @param string $moduleDir |
||
138 | */ |
||
139 | public function setSkeletonModuleDir($moduleDir) |
||
143 | |||
144 | /** |
||
145 | * @param string $moduleDir |
||
146 | */ |
||
147 | public function setTargetModuleDir($moduleDir) |
||
151 | |||
152 | /** |
||
153 | * @param array $tplEngines |
||
154 | */ |
||
155 | public function setEnabledTemplatingEngines(array $tplEngines) |
||
159 | |||
160 | |||
161 | /** |
||
162 | * @return void |
||
163 | */ |
||
164 | protected function configure() |
||
173 | |||
174 | /** |
||
175 | * @param InputInterface $input |
||
176 | * @param OutputInterface $output |
||
177 | * @throws \Exception |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | protected function execute(InputInterface $input, OutputInterface $output) |
||
260 | |||
261 | protected function isValidTemplatingEngine($tplEngine) |
||
271 | |||
272 | protected function isValidRoutingEngine($routingEngine) |
||
281 | |||
282 | /** |
||
283 | * @param string $moduleDir |
||
284 | * @param array $files |
||
285 | * @param array $tokens |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | protected function replaceTokensInFiles($moduleDir, $files, $tokens) |
||
299 | |||
300 | /** |
||
301 | * @param string $skeletonDir |
||
302 | * @param string $moduleDir |
||
303 | * @param array $files |
||
304 | * |
||
305 | * @throws \InvalidArgumentException When a file path being created already exists |
||
306 | */ |
||
307 | protected function copyFiles($skeletonDir, $moduleDir, $files) |
||
321 | |||
322 | /** |
||
323 | * @param string $moduleDir |
||
324 | * @param string $moduleName |
||
325 | * |
||
326 | * @throws \InvalidArgumentException When a dir path being created already exists |
||
327 | */ |
||
328 | protected function createModuleStructure($moduleDir, $moduleName) |
||
342 | |||
343 | /** |
||
344 | * @param InputInterface $input |
||
345 | * @param OutputInterface $output |
||
346 | */ |
||
347 | protected function askQuestions(InputInterface $input, OutputInterface $output) |
||
380 | |||
381 | /** |
||
382 | * @param InputInterface $input |
||
383 | * @param OutputInterface $output |
||
384 | */ |
||
385 | private function checkRouters(InputInterface $input, OutputInterface $output) |
||
402 | |||
403 | /** |
||
404 | * @param InputInterface $input |
||
405 | * @param OutputInterface $output |
||
406 | */ |
||
407 | private function checkTemplatingEngines(InputInterface $input, OutputInterface $output) |
||
457 | } |
||
458 |
If you suppress an error, we recommend checking for the error condition explicitly: