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 |
||
26 | class ComposerApiService implements ComposerServiceInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var EccubeConfig |
||
30 | */ |
||
31 | protected $eccubeConfig; |
||
32 | |||
33 | /** |
||
34 | * @var Application |
||
35 | */ |
||
36 | private $consoleApplication; |
||
37 | |||
38 | private $workingDir; |
||
39 | |||
40 | public function __construct(EccubeConfig $eccubeConfig) |
||
44 | |||
45 | /** |
||
46 | * Run get info command |
||
47 | * |
||
48 | * @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
49 | * @param string|null $version |
||
50 | * @return array |
||
51 | * |
||
52 | * @throws PluginException |
||
53 | */ |
||
54 | public function execInfo($pluginName, $version) |
||
65 | |||
66 | /** |
||
67 | * Run execute command |
||
68 | * |
||
69 | * @param string $packageName format "foo/bar foo/bar:1.0.0" |
||
70 | * @param null|OutputInterface $output |
||
71 | * |
||
72 | * @return string |
||
73 | * @throws PluginException |
||
74 | */ |
||
75 | public function execRequire($packageName, $output = null) |
||
89 | |||
90 | /** |
||
91 | * Run remove command |
||
92 | * |
||
93 | * @param string $packageName format "foo/bar foo/bar:1.0.0" |
||
94 | * @param null|OutputInterface $output |
||
95 | * |
||
96 | * @return string |
||
97 | * @throws PluginException |
||
98 | */ |
||
99 | public function execRemove($packageName, $output = null) |
||
111 | |||
112 | /** |
||
113 | * Run update command |
||
114 | * |
||
115 | * @param boolean $dryRun |
||
116 | * @param null|OutputInterface $output |
||
117 | * |
||
118 | * @throws PluginException |
||
119 | */ |
||
120 | View Code Duplication | public function execUpdate($dryRun, $output = null) |
|
130 | |||
131 | /** |
||
132 | * Run install command |
||
133 | * |
||
134 | * @param boolean $dryRun |
||
135 | * @param null|OutputInterface $output |
||
136 | * |
||
137 | * @throws PluginException |
||
138 | */ |
||
139 | View Code Duplication | public function execInstall($dryRun, $output = null) |
|
149 | |||
150 | /** |
||
151 | * Get require |
||
152 | * |
||
153 | * @param string $packageName |
||
154 | * @param string|null $version |
||
155 | * @param string $callback |
||
156 | * @param null $typeFilter |
||
157 | * |
||
158 | * @param int $level |
||
159 | * @throws PluginException |
||
160 | */ |
||
161 | public function foreachRequires($packageName, $version, $callback, $typeFilter = null, $level = 0) |
||
181 | |||
182 | /** |
||
183 | * Run get config information |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param null $value |
||
187 | * |
||
188 | * @return array|mixed |
||
189 | * |
||
190 | * @throws PluginException |
||
191 | */ |
||
192 | public function execConfig($key, $value = null) |
||
206 | |||
207 | /** |
||
208 | * Get config list |
||
209 | * |
||
210 | * @return array |
||
211 | * |
||
212 | * @throws PluginException |
||
213 | */ |
||
214 | public function getConfig() |
||
223 | |||
224 | /** |
||
225 | * Set work dir |
||
226 | * |
||
227 | * @param string $workingDir |
||
228 | */ |
||
229 | public function setWorkingDir($workingDir) |
||
233 | |||
234 | /** |
||
235 | * Run composer command |
||
236 | * |
||
237 | * @param array $commands |
||
238 | * @param null|OutputInterface $output |
||
239 | * |
||
240 | * @return string |
||
241 | * |
||
242 | * @throws PluginException |
||
243 | */ |
||
244 | public function runCommand($commands, $output = null) |
||
267 | |||
268 | /** |
||
269 | * Init composer console application |
||
270 | */ |
||
271 | private function init() |
||
283 | |||
284 | /** |
||
285 | * Get version of composer |
||
286 | * |
||
287 | * @return null|string |
||
288 | * |
||
289 | * @throws PluginException |
||
290 | */ |
||
291 | public function composerVersion() |
||
300 | |||
301 | /** |
||
302 | * Get mode |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getMode() |
||
310 | } |
||
311 |
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.