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 |
||
28 | class ComposerApiService implements ComposerServiceInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var EccubeConfig |
||
32 | */ |
||
33 | protected $eccubeConfig; |
||
34 | |||
35 | /** |
||
36 | * @var Application |
||
37 | */ |
||
38 | private $consoleApplication; |
||
39 | |||
40 | private $workingDir; |
||
41 | /** |
||
42 | * @var BaseInfoRepository |
||
43 | */ |
||
44 | private $baseInfoRepository; |
||
45 | |||
46 | public function __construct(EccubeConfig $eccubeConfig, BaseInfoRepository $baseInfoRepository) |
||
51 | |||
52 | /** |
||
53 | * Run get info command |
||
54 | * |
||
55 | * @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
56 | * @param string|null $version |
||
57 | * |
||
58 | * @return array |
||
59 | * |
||
60 | * @throws PluginException |
||
61 | * @throws \Doctrine\ORM\NoResultException |
||
62 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
63 | */ |
||
64 | public function execInfo($pluginName, $version) |
||
75 | |||
76 | /** |
||
77 | * Run execute command |
||
78 | * |
||
79 | * @param string $packageName format "foo/bar foo/bar:1.0.0" |
||
80 | * @param null|OutputInterface $output |
||
81 | * |
||
82 | * @return string |
||
83 | * |
||
84 | * @throws PluginException |
||
85 | * @throws \Doctrine\ORM\NoResultException |
||
86 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
87 | */ |
||
88 | public function execRequire($packageName, $output = null) |
||
103 | |||
104 | /** |
||
105 | * Run remove command |
||
106 | * |
||
107 | * @param string $packageName format "foo/bar foo/bar:1.0.0" |
||
108 | * @param null|OutputInterface $output |
||
109 | * |
||
110 | * @return string |
||
111 | * |
||
112 | * @throws PluginException |
||
113 | * @throws \Doctrine\ORM\NoResultException |
||
114 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
115 | */ |
||
116 | public function execRemove($packageName, $output = null) |
||
129 | |||
130 | /** |
||
131 | * Run update command |
||
132 | * |
||
133 | * @param boolean $dryRun |
||
134 | * @param null|OutputInterface $output |
||
135 | * |
||
136 | * @throws PluginException |
||
137 | * @throws \Doctrine\ORM\NoResultException |
||
138 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
139 | */ |
||
140 | View Code Duplication | public function execUpdate($dryRun, $output = null) |
|
150 | |||
151 | /** |
||
152 | * Run install command |
||
153 | * |
||
154 | * @param boolean $dryRun |
||
155 | * @param null|OutputInterface $output |
||
156 | * |
||
157 | * @throws PluginException |
||
158 | * @throws \Doctrine\ORM\NoResultException |
||
159 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
160 | */ |
||
161 | View Code Duplication | public function execInstall($dryRun, $output = null) |
|
171 | |||
172 | /** |
||
173 | * Get require |
||
174 | * |
||
175 | * @param string $packageName |
||
176 | * @param string|null $version |
||
177 | * @param string $callback |
||
178 | * @param null $typeFilter |
||
179 | * @param int $level |
||
180 | * |
||
181 | * @throws PluginException |
||
182 | * @throws \Doctrine\ORM\NoResultException |
||
183 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
184 | */ |
||
185 | public function foreachRequires($packageName, $version, $callback, $typeFilter = null, $level = 0) |
||
205 | |||
206 | /** |
||
207 | * Run get config information |
||
208 | * |
||
209 | * @param string $key |
||
210 | * @param null $value |
||
211 | * |
||
212 | * @return array|mixed |
||
213 | * |
||
214 | * @throws PluginException |
||
215 | * @throws \Doctrine\ORM\NoResultException |
||
216 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
217 | */ |
||
218 | public function execConfig($key, $value = null) |
||
232 | |||
233 | /** |
||
234 | * Get config list |
||
235 | * |
||
236 | * @return array |
||
237 | * |
||
238 | * @throws PluginException |
||
239 | * @throws \Doctrine\ORM\NoResultException |
||
240 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
241 | */ |
||
242 | public function getConfig() |
||
251 | |||
252 | /** |
||
253 | * Set work dir |
||
254 | * |
||
255 | * @param string $workingDir |
||
256 | */ |
||
257 | public function setWorkingDir($workingDir) |
||
261 | |||
262 | /** |
||
263 | * Run composer command |
||
264 | * |
||
265 | * @param array $commands |
||
266 | * @param null|OutputInterface $output |
||
267 | * @param bool $init |
||
268 | * |
||
269 | * @return string |
||
270 | * |
||
271 | * @throws PluginException |
||
272 | * @throws \Doctrine\ORM\NoResultException |
||
273 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
274 | * @throws \Exception |
||
275 | */ |
||
276 | public function runCommand($commands, $output = null, $init = true) |
||
313 | |||
314 | /** |
||
315 | * Init composer console application |
||
316 | * |
||
317 | * @param BaseInfo|null $BaseInfo |
||
318 | * |
||
319 | * @throws PluginException |
||
320 | * @throws \Doctrine\ORM\NoResultException |
||
321 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
322 | */ |
||
323 | private function init($BaseInfo = null) |
||
352 | |||
353 | private function initConsole() |
||
360 | |||
361 | /** |
||
362 | * @param BaseInfo $BaseInfo |
||
363 | * |
||
364 | * @throws PluginException |
||
365 | * @throws \Doctrine\ORM\NoResultException |
||
366 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
367 | */ |
||
368 | public function configureRepository(BaseInfo $BaseInfo) |
||
372 | } |
||
373 |
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.