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 SetupCommand 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 SetupCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class SetupCommand extends Command |
||
17 | { |
||
18 | /** |
||
19 | * @var EngineInterface |
||
20 | */ |
||
21 | private $templating; |
||
22 | |||
23 | /** |
||
24 | * @var string|null |
||
25 | */ |
||
26 | private $rootDir = null; |
||
27 | |||
28 | /** |
||
29 | * @param string|null $name |
||
30 | */ |
||
31 | 14 | public function __construct($name = null) |
|
40 | |||
41 | /** |
||
42 | * @param string $path |
||
43 | */ |
||
44 | 14 | public function setRootDir($path) |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 14 | protected function configure() |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 2 | protected function interact(InputInterface $input, OutputInterface $output) |
|
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | 14 | protected function execute(InputInterface $input, OutputInterface $output) |
|
188 | |||
189 | /** |
||
190 | * @param InputInterface $input |
||
191 | * @param OutputInterface $output |
||
192 | */ |
||
193 | 11 | private function createSourceTree(InputInterface $input, OutputInterface $output) |
|
216 | |||
217 | /** |
||
218 | * @param InputInterface $input |
||
219 | * @param OutputInterface $output |
||
220 | */ |
||
221 | 11 | View Code Duplication | private function createBuildFile(InputInterface $input, OutputInterface $output) |
229 | |||
230 | /** |
||
231 | * @param InputInterface $input |
||
232 | * @param OutputInterface $output |
||
233 | */ |
||
234 | 11 | View Code Duplication | private function createPackageJson(InputInterface $input, OutputInterface $output) |
242 | |||
243 | /** |
||
244 | * @param InputInterface $input |
||
245 | * @param OutputInterface $output |
||
246 | */ |
||
247 | 11 | private function createBowerJson(InputInterface $input, OutputInterface $output) |
|
251 | |||
252 | /** |
||
253 | * @param InputInterface $input |
||
254 | * @param OutputInterface $output |
||
255 | * @param string $blueprintDir |
||
256 | * @param string $targetDir |
||
257 | */ |
||
258 | 11 | private function createDirFromBlueprint(InputInterface $input, OutputInterface $output, $blueprintDir, $targetDir) |
|
297 | |||
298 | /** |
||
299 | * @param InputInterface $input |
||
300 | * @param OutputInterface $output |
||
301 | * @param string $file |
||
302 | */ |
||
303 | 11 | private function createFileFromTemplate(InputInterface $input, OutputInterface $output, $file) |
|
323 | |||
324 | /** |
||
325 | * @param InputInterface $input |
||
326 | * @param OutputInterface $output |
||
327 | * @param string $file |
||
328 | * @param string $target |
||
329 | */ |
||
330 | 6 | private function renderTemplate(InputInterface $input, OutputInterface $output, $file, $target) |
|
331 | { |
||
332 | 6 | if (file_exists($target) && !$input->getOption('force')) { |
|
333 | 1 | $output->writeln( |
|
334 | 1 | "<error>$target already exists. Run this command with --force to overwrite</error>" |
|
335 | ); |
||
336 | } |
||
337 | |||
338 | 6 | switch ($input->getOption('csspre')) { |
|
339 | case 'sass': |
||
340 | 5 | $stylesheetExtension = 'scss'; |
|
341 | 5 | break; |
|
342 | case 'less': |
||
343 | 1 | $stylesheetExtension = 'less'; |
|
344 | 1 | break; |
|
345 | default: |
||
346 | $stylesheetExtension = 'css'; |
||
347 | break; |
||
348 | } |
||
349 | |||
350 | 6 | file_put_contents($target, $this->templating->render("$file.php", [ |
|
351 | 6 | 'projectName' => basename(getcwd()), |
|
352 | 6 | 'srcDir' => $input->getOption('src-dir'), |
|
353 | 6 | 'destDir' => $input->getOption('dest-dir'), |
|
354 | 6 | 'prefix' => str_replace('web/', '', $input->getOption('dest-dir')), |
|
355 | 6 | 'coffee' => $input->getOption('coffee'), |
|
356 | 6 | 'cssPre' => $input->getOption('csspre'), |
|
357 | 6 | 'stylesheetExtension' => $stylesheetExtension, |
|
358 | ])); |
||
359 | 6 | } |
|
360 | |||
361 | /** |
||
362 | * @param InputInterface $input |
||
363 | */ |
||
364 | 14 | private function processOptions(InputInterface $input) |
|
380 | |||
381 | /** |
||
382 | * @param string $name |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | 14 | private function getDefaultOption($name) |
|
398 | } |
||
399 |