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 /** MicroFTP */ |
||
17 | final class Ftp |
||
18 | { |
||
19 | /** |
||
20 | * Last error |
||
21 | * |
||
22 | * @var string $error |
||
23 | */ |
||
24 | public $error; |
||
25 | /** |
||
26 | * FTP passive mode flag |
||
27 | * |
||
28 | * @var bool $passive |
||
29 | */ |
||
30 | public $passive = false; |
||
31 | /** |
||
32 | * SSL-FTP connection flag |
||
33 | * |
||
34 | * @var bool $ssl |
||
35 | */ |
||
36 | public $ssl = false; |
||
37 | /** |
||
38 | * System type of FTP server |
||
39 | * |
||
40 | * @var string $system_type |
||
41 | */ |
||
42 | public $system_type; |
||
43 | /** |
||
44 | * FTP host |
||
45 | * |
||
46 | * @var string $_host |
||
47 | */ |
||
48 | private $_host; |
||
49 | /** |
||
50 | * FTP port |
||
51 | * |
||
52 | * @var int $_port |
||
53 | */ |
||
54 | private $_port = 21; |
||
55 | /** |
||
56 | * FTP password |
||
57 | * |
||
58 | * @var string $_pwd |
||
59 | */ |
||
60 | private $_pwd; |
||
61 | /** |
||
62 | * FTP stream |
||
63 | * |
||
64 | * @var resource $_id |
||
65 | */ |
||
66 | private $_stream; |
||
67 | /** |
||
68 | * FTP timeout |
||
69 | * |
||
70 | * @var int $_timeout |
||
71 | */ |
||
72 | private $_timeout = 90; |
||
73 | /** |
||
74 | * FTP user |
||
75 | * |
||
76 | * @var string $_user |
||
77 | */ |
||
78 | private $_user; |
||
79 | |||
80 | /** |
||
81 | * Initialize connection params |
||
82 | * |
||
83 | * @access public |
||
84 | * |
||
85 | * @param array $params |
||
86 | * |
||
87 | * @result void |
||
88 | */ |
||
89 | public function __construct(array $params = []) |
||
97 | |||
98 | /** |
||
99 | * Auto close connection |
||
100 | */ |
||
101 | public function __destruct() |
||
105 | |||
106 | /** |
||
107 | * Close FTP connection |
||
108 | */ |
||
109 | public function close() |
||
120 | |||
121 | /** |
||
122 | * Change current directory on FTP server |
||
123 | * |
||
124 | * @param string $directory |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function cd($directory = null) |
||
138 | |||
139 | /** |
||
140 | * Set file permissions |
||
141 | * |
||
142 | * @param int $permissions (ex: 0644) |
||
143 | * @param string $remote_file |
||
144 | * |
||
145 | * @return false |
||
146 | */ |
||
147 | public function chmod($permissions = 0, $remote_file = null) |
||
157 | |||
158 | /** |
||
159 | * Connect to FTP server |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function connect() |
||
186 | |||
187 | /** |
||
188 | * Delete file on FTP server |
||
189 | * |
||
190 | * @param string $remote_file |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function delete($remote_file = null) |
||
204 | |||
205 | /** |
||
206 | * Download file from server |
||
207 | * |
||
208 | * @param string $remote_file |
||
209 | * @param string $local_file |
||
210 | * @param int $mode |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | View Code Duplication | public function get($remote_file = null, $local_file = null, $mode = FTP_ASCII) |
|
224 | |||
225 | /** |
||
226 | * Get list of files/directories in directory |
||
227 | * |
||
228 | * @param string $directory |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function ls($directory = null) |
||
242 | |||
243 | /** |
||
244 | * Create directory on FTP server |
||
245 | * |
||
246 | * @param string $directory |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | View Code Duplication | public function mkdir($directory = null) |
|
260 | |||
261 | /** |
||
262 | * Upload file to server |
||
263 | * |
||
264 | * @param string $local_file |
||
265 | * @param string $remote_file |
||
266 | * @param int $mode |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | View Code Duplication | public function put($local_file = null, $remote_file = null, $mode = FTP_ASCII) |
|
280 | |||
281 | /** |
||
282 | * Get current directory |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function pwd() |
||
290 | |||
291 | /** |
||
292 | * Rename file on FTP server |
||
293 | * |
||
294 | * @param string $old_name |
||
295 | * @param string $new_name |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function rename($old_name = null, $new_name = null) |
||
309 | |||
310 | /** |
||
311 | * Remove directory on FTP server |
||
312 | * |
||
313 | * @param string $directory |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | View Code Duplication | public function rmdir($directory = null) |
|
327 | } |
||
328 |
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.