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 |
||
12 | class FtpImplicitSSL extends FtpAbstract implements Logger\LoggerInterface |
||
13 | { |
||
14 | use Logger\LoggerTrait; |
||
15 | |||
16 | const CURL_OPTIONS = 'curl_options'; |
||
17 | const PASSIVE_MODE = 'passive_mode'; |
||
18 | |||
19 | /** @var string $url */ |
||
20 | private $url; |
||
21 | |||
22 | /** |
||
23 | * @return array |
||
24 | */ |
||
25 | 1 | protected function getCurlOptions() |
|
29 | |||
30 | /** |
||
31 | * @return bool |
||
32 | */ |
||
33 | 1 | protected function getPassiveMode() |
|
37 | |||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | 1 | protected function getUrl() |
|
45 | |||
46 | /** |
||
47 | * @param string $url |
||
48 | * @return $this |
||
49 | */ |
||
50 | 1 | protected function setUrl($url) |
|
55 | |||
56 | /** |
||
57 | * FtpImplicitSSL constructor. |
||
58 | * @param array $params |
||
59 | */ |
||
60 | 12 | public function __construct($params = array()) |
|
73 | |||
74 | /** |
||
75 | * @param string $host |
||
76 | * @param int $port |
||
77 | * @throws FtpException |
||
78 | * @return $this |
||
79 | */ |
||
80 | 3 | public function connect($host, $port = 990) |
|
94 | |||
95 | /** |
||
96 | * @param string $user |
||
97 | * @param string $pass |
||
98 | * @throws FtpException |
||
99 | * @return bool |
||
100 | */ |
||
101 | 2 | public function login($user, $pass) |
|
113 | |||
114 | /** |
||
115 | * @param string $localFile |
||
116 | * @param string $remoteFile |
||
117 | * @throws FtpException |
||
118 | * @return bool |
||
119 | */ |
||
120 | 2 | public function get($localFile, $remoteFile) |
|
136 | |||
137 | /** |
||
138 | * Put file on ftp server |
||
139 | * @param string $remoteFile |
||
140 | * @param string $localFile |
||
141 | * @return bool |
||
142 | * @throws FtpException |
||
143 | */ |
||
144 | 2 | public function put($remoteFile, $localFile) |
|
158 | |||
159 | |||
160 | /** |
||
161 | * @param string $file |
||
162 | * @return bool |
||
163 | */ |
||
164 | 1 | public function delete($file) |
|
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | 1 | public function close() |
|
177 | |||
178 | /** |
||
179 | * @param string|null $url |
||
180 | * @return resource |
||
181 | * @codeCoverageIgnore |
||
182 | */ |
||
183 | protected function phpCurlInit($url = null) |
||
187 | |||
188 | /** |
||
189 | * @param resource $ch |
||
190 | * @return mixed |
||
191 | * @codeCoverageIgnore |
||
192 | */ |
||
193 | protected function phpCurlExec($ch) |
||
197 | |||
198 | /** |
||
199 | * @param resource $ch |
||
200 | * @param int $option |
||
201 | * @param mixed $value |
||
202 | * @return bool |
||
203 | * @codeCoverageIgnore |
||
204 | */ |
||
205 | protected function phpCurlSetOpt($ch, $option, $value) |
||
209 | |||
210 | /** |
||
211 | * @param resource $ch |
||
212 | * @return string |
||
213 | * @codeCoverageIgnore |
||
214 | */ |
||
215 | protected function phpCurlError($ch) |
||
219 | |||
220 | /** |
||
221 | * @param resource $ch |
||
222 | * @codeCoverageIgnore |
||
223 | */ |
||
224 | protected function phpCurlClose($ch) |
||
228 | |||
229 | /** |
||
230 | * @param string $filename |
||
231 | * @param string $mode |
||
232 | * @return resource |
||
233 | * @codeCoverageIgnore |
||
234 | */ |
||
235 | protected function phpFopen($filename, $mode) |
||
239 | |||
240 | /** |
||
241 | * @param resource $handle |
||
242 | * @return bool |
||
243 | * @codeCoverageIgnore |
||
244 | */ |
||
245 | protected function phpFclose($handle) |
||
249 | } |
||
250 |
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.