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 |
||
36 | class ComposerApiService implements ComposerServiceInterface |
||
37 | { |
||
38 | /** |
||
39 | * @Inject("config") |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $appConfig; |
||
43 | |||
44 | /** |
||
45 | * @var Application $consoleApplication |
||
46 | */ |
||
47 | private $consoleApplication; |
||
48 | |||
49 | private $workingDir; |
||
50 | |||
51 | /** |
||
52 | * Run get info command |
||
53 | * |
||
54 | * @param string $pluginName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
55 | * @return array |
||
56 | */ |
||
57 | public function execInfo($pluginName) |
||
66 | |||
67 | /** |
||
68 | * Run execute command |
||
69 | * |
||
70 | * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
71 | * @return array |
||
72 | */ |
||
73 | View Code Duplication | public function execRequire($packageName) |
|
86 | |||
87 | /** |
||
88 | * Run remove command |
||
89 | * |
||
90 | * @param string $packageName format foo/bar or foo/bar:1.0.0 or "foo/bar 1.0.0" |
||
91 | * @return bool |
||
92 | */ |
||
93 | View Code Duplication | public function execRemove($packageName) |
|
105 | |||
106 | /** |
||
107 | * Get require |
||
108 | * |
||
109 | * @param string $packageName |
||
110 | * @param string $callback |
||
111 | * @param null $typeFilter |
||
112 | */ |
||
113 | public function foreachRequires($packageName, $callback, $typeFilter = null) |
||
125 | |||
126 | /** |
||
127 | * Run get config information |
||
128 | * |
||
129 | * @param string $key |
||
130 | * @param null $value |
||
131 | * @return array|mixed |
||
132 | */ |
||
133 | public function execConfig($key, $value = null) |
||
147 | |||
148 | /** |
||
149 | * Get config list |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public function getConfig() |
||
162 | |||
163 | /** |
||
164 | * Set work dir |
||
165 | * |
||
166 | * @param string $workingDir |
||
167 | */ |
||
168 | public function setWorkingDir($workingDir) |
||
172 | |||
173 | /** |
||
174 | * Run composer command |
||
175 | * |
||
176 | * @param array $commands |
||
177 | * @return string |
||
178 | */ |
||
179 | public function runCommand($commands) |
||
198 | |||
199 | /** |
||
200 | * Init composer console application |
||
201 | */ |
||
202 | private function init() |
||
214 | } |
||
215 |
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.