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 |
||
| 31 | class GlSyncFtp |
||
| 32 | { |
||
| 33 | const DELETE_FILE = 0; |
||
| 34 | const DELETE_DIR = 1; |
||
| 35 | const CREATE_DIR = 2; |
||
| 36 | const NEW_FILE = 3; |
||
| 37 | const UPDATE_FILE = 4; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var SFTP |
||
| 41 | */ |
||
| 42 | private $sftp; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $server; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private $port; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $user; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $password; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $ftpserver |
||
| 66 | * @param int $port |
||
| 67 | * @param string $user |
||
| 68 | * @param string $password |
||
| 69 | */ |
||
| 70 | function __construct($ftpserver, $port, $user, $password) |
||
| 77 | |||
| 78 | function __destruct() |
||
| 82 | |||
| 83 | function disconnect() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $root |
||
| 92 | * @param array $listfiles |
||
| 93 | * @param array $listdirs |
||
| 94 | * |
||
| 95 | * @throws GlSyncFtpException |
||
| 96 | */ |
||
| 97 | public function getAllFiles($root, &$listfiles, &$listdirs) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @throws GlSyncFtpException |
||
| 105 | */ |
||
| 106 | private function login() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $root |
||
| 120 | * @param string $relative |
||
| 121 | * @param array $listfiles |
||
| 122 | * @param array $listdirs |
||
| 123 | */ |
||
| 124 | private function getFiles($root, $relative, &$listfiles, &$listdirs) |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * sync list of local directories with list of ftp directories |
||
| 145 | * |
||
| 146 | * @param array $list |
||
| 147 | * @param callable $syncdir = null |
||
| 148 | * @param callable $syncop = null |
||
| 149 | */ |
||
| 150 | public function syncDirectories($list, callable $syncdir = null, callable $syncop = null) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * delete files unknowns on ftp server |
||
| 166 | * |
||
| 167 | * @param string $src |
||
| 168 | * @param string $dst |
||
| 169 | * @param callable|null $syncop |
||
| 170 | */ |
||
| 171 | private function syncDelete($src, $dst,callable $syncop = null) { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * sync local directory with ftp directory |
||
| 203 | * |
||
| 204 | * @param string $src |
||
| 205 | * @param string $dst |
||
| 206 | * @param callable|null $syncop |
||
| 207 | * |
||
| 208 | * @throws GlSyncFtpException |
||
| 209 | */ |
||
| 210 | public function syncDirectory($src, $dst, callable $syncop = null) |
||
| 261 | } |
||
| 262 | |||
| 263 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.