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 | private $templating; |
||
| 19 | private $rootDir = null; |
||
| 20 | |||
| 21 | 15 | public function __construct($name = null) |
|
| 30 | |||
| 31 | 15 | public function setRootDir($path) |
|
| 35 | |||
| 36 | 15 | protected function configure() |
|
| 85 | |||
| 86 | 2 | protected function interact(InputInterface $input, OutputInterface $output) |
|
| 136 | |||
| 137 | 15 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 161 | |||
| 162 | 12 | private function runInstallCommand($input, $output) |
|
| 171 | |||
| 172 | 12 | private function createSourceTree($input, $output) |
|
| 195 | |||
| 196 | 12 | View Code Duplication | private function createBuildFile($input, $output) |
| 204 | |||
| 205 | 12 | View Code Duplication | private function createPackageJson($input, $output) |
| 213 | |||
| 214 | 12 | private function createBowerJson($input, $output) |
|
| 218 | |||
| 219 | 12 | private function createDirFromBlueprint($input, $output, $blueprintDir, $targetDir) |
|
| 220 | { |
||
| 221 | 12 | $dryRun = $input->getOption('dry-run'); |
|
| 222 | |||
| 223 | 12 | if (!$dryRun && !file_exists($targetDir)) { |
|
| 224 | 6 | mkdir($targetDir, 0777, true); |
|
| 225 | } |
||
| 226 | |||
| 227 | 12 | foreach (preg_grep('/^\.?\w+/', scandir($blueprintDir)) as $entry) { |
|
| 228 | 12 | $target = $entry; |
|
| 229 | |||
| 230 | 12 | $isPhpTemplate = substr($entry, strrpos($entry, '.')) === '.php'; |
|
| 231 | 12 | if ($isPhpTemplate) { |
|
| 232 | 12 | $entry = str_replace('.php', '', $entry); |
|
| 233 | 12 | $target = str_replace('.php', '', $target); |
|
| 234 | } |
||
| 235 | |||
| 236 | 12 | $entry = $blueprintDir.'/'.$entry; |
|
| 237 | 12 | $target = $targetDir.'/'.$target; |
|
| 238 | |||
| 239 | 12 | if (!$dryRun) { |
|
| 240 | 6 | if ($isPhpTemplate) { |
|
| 241 | 6 | $this->renderTemplate($input, $output, $entry, $target); |
|
| 242 | } else { |
||
| 243 | 6 | if (file_exists($target) && !$input->getOption('force')) { |
|
| 244 | $output->writeln( |
||
| 245 | "<error>$target already exists. Run this command with --force to overwrite</error> |
||
| 246 | "); |
||
| 247 | |||
| 248 | continue; |
||
| 249 | } |
||
| 250 | |||
| 251 | 6 | copy($entry, $target); |
|
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | 12 | $output->writeln($target); |
|
| 256 | } |
||
| 257 | 12 | } |
|
| 258 | |||
| 259 | 12 | private function createFileFromTemplate($input, $output, $file) |
|
| 279 | |||
| 280 | 6 | private function renderTemplate($input, $output, $file, $target) |
|
| 281 | { |
||
| 282 | 6 | if (file_exists($target) && !$input->getOption('force')) { |
|
| 283 | 1 | return $output->writeln( |
|
| 284 | 1 | "<error>$target already exists. Run this command with --force to overwrite</error> |
|
| 285 | "); |
||
| 286 | } |
||
| 287 | |||
| 288 | 6 | switch ($input->getOption('csspre')) { |
|
| 289 | 6 | case 'sass': |
|
| 290 | 5 | $stylesheetExtension = 'scss'; |
|
| 291 | 5 | break; |
|
| 292 | 1 | case 'less': |
|
| 293 | 1 | $stylesheetExtension = 'less'; |
|
| 294 | 1 | break; |
|
| 295 | default: |
||
| 296 | $stylesheetExtension = 'css'; |
||
| 297 | break; |
||
| 298 | } |
||
| 299 | |||
| 300 | 6 | file_put_contents($target, $this->templating->render("$file.php", array( |
|
| 301 | 6 | 'projectName' => basename(getcwd()), |
|
| 302 | 6 | 'srcDir' => $input->getOption('src-dir'), |
|
| 303 | 6 | 'destDir' => $input->getOption('dest-dir'), |
|
| 304 | 6 | 'prefix' => str_replace('web/', '', $input->getOption('dest-dir')), |
|
| 305 | 6 | 'coffee' => $input->getOption('coffee'), |
|
| 306 | 6 | 'cssPre' => $input->getOption('csspre'), |
|
| 307 | 6 | 'stylesheetExtension' => $stylesheetExtension, |
|
| 308 | ))); |
||
| 309 | 6 | } |
|
| 310 | |||
| 311 | 15 | private function processOptions($input) |
|
| 327 | |||
| 328 | 15 | private function getDefaultOption($name) |
|
| 340 | } |
||
| 341 |