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:
1 | <?php |
||
54 | class ConvertCommand extends Command |
||
55 | { |
||
56 | /** |
||
57 | * @var BrowscapCacheInterface |
||
58 | */ |
||
59 | private $cache = null; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $defaultIniFile; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $defaultCacheFolder; |
||
70 | |||
71 | /** |
||
72 | * @param string $defaultCacheFolder |
||
73 | * @param string $defaultIniFile |
||
74 | */ |
||
75 | 2 | public function __construct($defaultCacheFolder, $defaultIniFile) |
|
82 | |||
83 | /** |
||
84 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | 2 | public function setCache(BrowscapCacheInterface $cache) |
|
94 | |||
95 | /** |
||
96 | * Configures the current command. |
||
97 | */ |
||
98 | 2 | View Code Duplication | protected function configure() |
|
|||
99 | { |
||
100 | $this |
||
101 | 2 | ->setName('browscap:convert') |
|
102 | 2 | ->setDescription('Converts an existing browscap.ini file to a cache.php file.') |
|
103 | 2 | ->addArgument( |
|
104 | 2 | 'file', |
|
105 | 2 | InputArgument::OPTIONAL, |
|
106 | 2 | 'Path to the browscap.ini file', |
|
107 | 2 | $this->defaultIniFile |
|
108 | ) |
||
109 | 2 | ->addOption( |
|
110 | 2 | 'debug', |
|
111 | 2 | 'd', |
|
112 | 2 | InputOption::VALUE_NONE, |
|
113 | 2 | 'Should the debug mode entered?' |
|
114 | ) |
||
115 | 2 | ->addOption( |
|
116 | 2 | 'cache', |
|
117 | 2 | 'c', |
|
118 | 2 | InputOption::VALUE_OPTIONAL, |
|
119 | 2 | 'Where the cache files are located', |
|
120 | 2 | $this->defaultCacheFolder |
|
121 | ); |
||
122 | 2 | } |
|
123 | |||
124 | /** |
||
125 | * @param InputInterface $input |
||
126 | * @param OutputInterface $output |
||
127 | * |
||
128 | * @return int|null|void |
||
129 | */ |
||
130 | 1 | protected function execute(InputInterface $input, OutputInterface $output) |
|
150 | |||
151 | /** |
||
152 | * @param InputInterface $input |
||
153 | * |
||
154 | * @return BrowscapCacheInterface |
||
155 | */ |
||
156 | 1 | View Code Duplication | private function getCache(InputInterface $input) |
165 | } |
||
166 |
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.