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 |
||
15 | class ApiListCommand extends Command |
||
16 | { |
||
17 | /** |
||
18 | * @var FilesystemCache |
||
19 | */ |
||
20 | private $cache; |
||
21 | |||
22 | /** |
||
23 | * Configures the current command. |
||
24 | * |
||
25 | * @return void |
||
26 | */ |
||
27 | protected function configure() |
||
33 | |||
34 | /** |
||
35 | * Executes the current command. |
||
36 | * |
||
37 | * @param InputInterface $input |
||
38 | * @param OutputInterface $output |
||
39 | * @return void |
||
40 | */ |
||
41 | protected function execute(InputInterface $input, OutputInterface $output) : void |
||
74 | |||
75 | /** |
||
76 | * Ask for URL API. |
||
77 | * |
||
78 | * @param InputInterface $input |
||
79 | * @param OutputInterface $output |
||
80 | * @return string |
||
81 | */ |
||
82 | protected function askUrlApi(InputInterface $input, OutputInterface $output) : string |
||
91 | |||
92 | /** |
||
93 | * Ask for API key. |
||
94 | * |
||
95 | * @param InputInterface $input |
||
96 | * @param OutputInterface $output |
||
97 | * @return string |
||
98 | */ |
||
99 | View Code Duplication | protected function askApiKey(InputInterface $input, OutputInterface $output) : string |
|
113 | |||
114 | /** |
||
115 | * Ask for secret key. |
||
116 | * |
||
117 | * @param InputInterface $input |
||
118 | * @param OutputInterface $output |
||
119 | * @return string |
||
120 | */ |
||
121 | View Code Duplication | protected function askSecretKey(InputInterface $input, OutputInterface $output) : string |
|
135 | |||
136 | /** |
||
137 | * Dump cache file of APIs list. |
||
138 | * |
||
139 | * @param OutputInterface $output |
||
140 | * @param array $list |
||
141 | * @return void |
||
142 | */ |
||
143 | protected function processList(OutputInterface $output, array $list = []) : void |
||
164 | |||
165 | /** |
||
166 | * Parse command data into expected structure |
||
167 | * @param array $command |
||
168 | * @return array |
||
169 | */ |
||
170 | protected function parseCommand(array $command) : array |
||
190 | |||
191 | /** |
||
192 | * Get cache driver instance |
||
193 | * @return FilesystemCache |
||
194 | */ |
||
195 | View Code Duplication | private function cache() : FilesystemCache |
|
203 | } |
||
204 |
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.