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 LogfileCommand 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 LogfileCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
70 | class LogfileCommand extends Command |
||
71 | { |
||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $undefinedClients = array(); |
||
76 | |||
77 | private $uas = array(); |
||
78 | private $uasWithType = array(); |
||
79 | |||
80 | private $countOk = 0; |
||
81 | private $countNok = 0; |
||
82 | private $totalCount = 0; |
||
83 | |||
84 | /** |
||
85 | * @var BrowscapCacheInterface |
||
86 | */ |
||
87 | private $cache = null; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | */ |
||
92 | private $defaultCacheFolder; |
||
93 | |||
94 | /** |
||
95 | * @param string $defaultCacheFolder |
||
96 | */ |
||
97 | 1 | public function __construct($defaultCacheFolder) |
|
98 | { |
||
99 | 1 | $this->defaultCacheFolder = $defaultCacheFolder; |
|
100 | |||
101 | 1 | parent::__construct(); |
|
102 | 1 | } |
|
103 | |||
104 | /** |
||
105 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | 1 | public function setCache(BrowscapCacheInterface $cache) |
|
115 | |||
116 | /** |
||
117 | * Configures the current command. |
||
118 | */ |
||
119 | 1 | protected function configure() |
|
171 | |||
172 | /** |
||
173 | * @param InputInterface $input |
||
174 | * @param OutputInterface $output |
||
175 | * |
||
176 | * @throws \UnexpectedValueException |
||
177 | * @throws \BrowscapPHP\Exception\InvalidArgumentException |
||
178 | * @return int|null|void |
||
179 | */ |
||
180 | protected function execute(InputInterface $input, OutputInterface $output) |
||
314 | |||
315 | private function createAmountContent() |
||
339 | |||
340 | private function createAmountTypeContent() |
||
359 | |||
360 | /** |
||
361 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
362 | * @param \BrowscapPHP\Util\Logfile\ReaderCollection $collection |
||
363 | * @param \BrowscapPHP\Browscap $browscap |
||
364 | * @param integer $line |
||
365 | * |
||
366 | * @throws UnknownBrowserException |
||
367 | * @throws UnknownBrowserTypeException |
||
368 | * @throws UnknownDeviceException |
||
369 | * @throws UnknownEngineException |
||
370 | * @throws UnknownPlatformException |
||
371 | * @throws \Exception |
||
372 | */ |
||
373 | private function handleLine(OutputInterface $output, ReaderCollection $collection, Browscap $browscap, $line) |
||
435 | |||
436 | /** |
||
437 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
438 | * @param string $result |
||
439 | * @param bool $end |
||
440 | * |
||
441 | * @return int |
||
442 | */ |
||
443 | private function outputProgress(OutputInterface $output, $result, $end = false) |
||
444 | { |
||
445 | if (($this->totalCount % 70) === 0 || $end) { |
||
446 | $formatString = ' %'.strlen($this->countOk).'d OK, %'.strlen($this->countNok).'d NOK, Summary %' |
||
447 | .strlen($this->totalCount).'d'; |
||
448 | |||
449 | if ($end) { |
||
450 | $result = str_pad($result, 70 - ($this->totalCount % 70), ' ', STR_PAD_RIGHT); |
||
451 | } |
||
452 | |||
453 | $endString = sprintf($formatString, $this->countOk, $this->countNok, $this->totalCount); |
||
454 | |||
455 | $output->writeln($result.$endString); |
||
456 | |||
457 | return; |
||
458 | } |
||
459 | |||
460 | $output->write($result); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param \stdClass $result |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | private function getResult(\stdClass $result) |
||
469 | { |
||
470 | if ('Default Browser' === $result->browser) { |
||
471 | throw new UnknownBrowserException('unknwon browser found'); |
||
472 | } |
||
473 | |||
474 | if ('unknown' === $result->browser_type) { |
||
475 | throw new UnknownBrowserTypeException('unknwon browser type found'); |
||
476 | } |
||
477 | |||
478 | if (in_array($result->browser_type, array('Bot/Crawler', 'Library'))) { |
||
479 | return '.'; |
||
480 | } |
||
481 | |||
482 | if ('unknown' === $result->platform) { |
||
483 | throw new UnknownPlatformException('unknown platform found'); |
||
484 | } |
||
485 | |||
486 | if ('unknown' === $result->device_type) { |
||
487 | throw new UnknownDeviceException('unknwon device type found'); |
||
488 | } |
||
489 | |||
490 | if ('unknown' === $result->renderingengine_name) { |
||
491 | throw new UnknownEngineException('unknown rendering engine found'); |
||
492 | } |
||
493 | |||
494 | return '.'; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
499 | * |
||
500 | * @return \Symfony\Component\Finder\Finder |
||
501 | */ |
||
502 | private function getFiles(InputInterface $input) |
||
503 | { |
||
504 | $finder = Finder::create(); |
||
505 | |||
506 | if ($input->getOption('log-file')) { |
||
507 | $file = $input->getOption('log-file'); |
||
508 | $finder->append(Finder::create()->in(dirname($file))->name(basename($file))); |
||
509 | } |
||
510 | |||
511 | if ($input->getOption('log-dir')) { |
||
512 | $dirFinder = Finder::create() |
||
513 | ->in($input->getOption('log-dir')); |
||
514 | array_map(array($dirFinder, 'name'), $input->getOption('include')); |
||
515 | array_map(array($dirFinder, 'notName'), $input->getOption('exclude')); |
||
516 | |||
517 | $finder->append($dirFinder); |
||
518 | } |
||
519 | |||
520 | return $finder; |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * @param \Symfony\Component\Finder\SplFileInfo $file |
||
525 | * |
||
526 | * @return string |
||
527 | */ |
||
528 | private function getPath(SplFileInfo $file) |
||
529 | { |
||
530 | switch ($file->getExtension()) { |
||
531 | case 'gz': |
||
532 | $path = 'compress.zlib://'.$file->getPathname(); |
||
533 | break; |
||
534 | case 'bz2': |
||
535 | $path = 'compress.bzip2://'.$file->getPathname(); |
||
536 | break; |
||
537 | default: |
||
538 | $path = $file->getPathname(); |
||
539 | break; |
||
540 | } |
||
541 | |||
542 | return $path; |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * @param InputInterface $input |
||
547 | * |
||
548 | * @return BrowscapCacheInterface |
||
549 | */ |
||
550 | View Code Duplication | private function getCache(InputInterface $input) |
|
559 | } |
||
560 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.