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:
Complex classes like Ftp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Ftp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Ftp extends AbstractFtpAdapter |
||
13 | { |
||
14 | /** |
||
15 | * Copy a file. |
||
16 | * |
||
17 | * @param string $path |
||
18 | * @param string $newpath |
||
19 | * |
||
20 | * @return bool |
||
21 | */ |
||
22 | View Code Duplication | public function copy($path, $newpath) |
|
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $transferMode = FTP_BINARY; |
||
43 | |||
44 | /** |
||
45 | * @var null|bool |
||
46 | */ |
||
47 | protected $ignorePassiveAddress = null; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $recurseManually = false; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $configurable = array( |
||
58 | 'host', |
||
59 | 'port', |
||
60 | 'username', |
||
61 | 'password', |
||
62 | 'ssl', |
||
63 | 'timeout', |
||
64 | 'root', |
||
65 | 'permPrivate', |
||
66 | 'permPublic', |
||
67 | 'passive', |
||
68 | 'transferMode', |
||
69 | 'systemType', |
||
70 | 'ignorePassiveAddress', |
||
71 | 'recurseManually', |
||
72 | ); |
||
73 | |||
74 | /** |
||
75 | * Set the transfer mode. |
||
76 | * |
||
77 | * @param int $mode |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | 3 | public function setTransferMode($mode) |
|
87 | |||
88 | /** |
||
89 | * Set if Ssl is enabled. |
||
90 | * |
||
91 | * @param bool $ssl |
||
92 | * |
||
93 | * @return $this |
||
94 | */ |
||
95 | 96 | public function setSsl($ssl) |
|
101 | |||
102 | /** |
||
103 | * Set if passive mode should be used. |
||
104 | * |
||
105 | * @param bool $passive |
||
106 | */ |
||
107 | 78 | public function setPassive($passive = true) |
|
111 | |||
112 | /** |
||
113 | * @param bool $ignorePassiveAddress |
||
114 | */ |
||
115 | 3 | public function setIgnorePassiveAddress($ignorePassiveAddress) |
|
119 | |||
120 | /** |
||
121 | * @param bool $recurseManually |
||
122 | */ |
||
123 | 66 | public function setRecurseManually($recurseManually) |
|
127 | |||
128 | /** |
||
129 | * Connect to the FTP server. |
||
130 | */ |
||
131 | 90 | public function connect() |
|
147 | |||
148 | /** |
||
149 | * Set the connections to passive mode. |
||
150 | * |
||
151 | * @throws RuntimeException |
||
152 | */ |
||
153 | 81 | protected function setConnectionPassiveMode() |
|
165 | |||
166 | /** |
||
167 | * Set the connection root. |
||
168 | */ |
||
169 | 78 | protected function setConnectionRoot() |
|
184 | |||
185 | /** |
||
186 | * Login. |
||
187 | * |
||
188 | * @throws RuntimeException |
||
189 | */ |
||
190 | 84 | protected function login() |
|
207 | |||
208 | /** |
||
209 | * Disconnect from the FTP server. |
||
210 | */ |
||
211 | 96 | public function disconnect() |
|
219 | |||
220 | /** |
||
221 | * @inheritdoc |
||
222 | */ |
||
223 | 9 | public function write($path, $contents, Config $config) |
|
240 | |||
241 | /** |
||
242 | * @inheritdoc |
||
243 | */ |
||
244 | 9 | public function writeStream($path, $resource, Config $config) |
|
258 | |||
259 | /** |
||
260 | * @inheritdoc |
||
261 | */ |
||
262 | 6 | public function update($path, $contents, Config $config) |
|
266 | |||
267 | /** |
||
268 | * @inheritdoc |
||
269 | */ |
||
270 | 3 | public function updateStream($path, $resource, Config $config) |
|
274 | |||
275 | /** |
||
276 | * @inheritdoc |
||
277 | */ |
||
278 | 3 | public function rename($path, $newpath) |
|
282 | |||
283 | /** |
||
284 | * @inheritdoc |
||
285 | */ |
||
286 | 3 | public function delete($path) |
|
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | 3 | public function deleteDir($dirname) |
|
311 | |||
312 | /** |
||
313 | * @inheritdoc |
||
314 | */ |
||
315 | 6 | public function createDir($dirname, Config $config) |
|
334 | |||
335 | /** |
||
336 | * Create a directory. |
||
337 | * |
||
338 | * @param string $directory |
||
339 | * @param resource $connection |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | 6 | protected function createActualDirectory($directory, $connection) |
|
360 | |||
361 | /** |
||
362 | * @inheritdoc |
||
363 | */ |
||
364 | 30 | public function getMetadata($path) |
|
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | 9 | public function getMimetype($path) |
|
408 | |||
409 | /** |
||
410 | * @inheritdoc |
||
411 | */ |
||
412 | 12 | public function getTimestamp($path) |
|
418 | |||
419 | /** |
||
420 | * @inheritdoc |
||
421 | */ |
||
422 | 6 | public function read($path) |
|
434 | |||
435 | /** |
||
436 | * @inheritdoc |
||
437 | */ |
||
438 | 6 | public function readStream($path) |
|
452 | |||
453 | /** |
||
454 | * @inheritdoc |
||
455 | */ |
||
456 | 9 | public function setVisibility($path, $visibility) |
|
466 | |||
467 | /** |
||
468 | * @inheritdoc |
||
469 | * |
||
470 | * @param string $directory |
||
471 | */ |
||
472 | 21 | protected function listDirectoryContents($directory, $recursive = true) |
|
485 | |||
486 | /** |
||
487 | * @inheritdoc |
||
488 | * |
||
489 | * @param string $directory |
||
490 | */ |
||
491 | 3 | protected function listDirectoryContentsRecursive($directory) |
|
505 | |||
506 | /** |
||
507 | * Check if the connection is open. |
||
508 | * |
||
509 | * @return bool |
||
510 | * @throws ErrorException |
||
511 | */ |
||
512 | 96 | public function isConnected() |
|
527 | } |
||
528 |
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.