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 |
||
| 14 | class Ftp extends AbstractFtpAdapter |
||
| 15 | { |
||
| 16 | use StreamedCopyTrait; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $transferMode = FTP_BINARY; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var null|bool |
||
| 25 | */ |
||
| 26 | protected $ignorePassiveAddress = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | protected $recurseManually = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected $utf8 = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $configurable = [ |
||
| 42 | 'host', |
||
| 43 | 'port', |
||
| 44 | 'username', |
||
| 45 | 'password', |
||
| 46 | 'ssl', |
||
| 47 | 'timeout', |
||
| 48 | 'root', |
||
| 49 | 'permPrivate', |
||
| 50 | 'permPublic', |
||
| 51 | 'passive', |
||
| 52 | 'transferMode', |
||
| 53 | 'systemType', |
||
| 54 | 'ignorePassiveAddress', |
||
| 55 | 'recurseManually', |
||
| 56 | 'utf8', |
||
| 57 | 'enableTimestampsOnUnixListings', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $isPureFtpd; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set the transfer mode. |
||
| 67 | * |
||
| 68 | * @param int $mode |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function setTransferMode($mode) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set if Ssl is enabled. |
||
| 81 | * |
||
| 82 | * @param bool $ssl |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function setSsl($ssl) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set if passive mode should be used. |
||
| 95 | * |
||
| 96 | * @param bool $passive |
||
| 97 | */ |
||
| 98 | public function setPassive($passive = true) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param bool $ignorePassiveAddress |
||
| 105 | */ |
||
| 106 | public function setIgnorePassiveAddress($ignorePassiveAddress) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param bool $recurseManually |
||
| 113 | */ |
||
| 114 | public function setRecurseManually($recurseManually) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param bool $utf8 |
||
| 121 | */ |
||
| 122 | public function setUtf8($utf8) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Connect to the FTP server. |
||
| 129 | */ |
||
| 130 | public function connect() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Set the connection to UTF-8 mode. |
||
| 158 | */ |
||
| 159 | protected function setUtf8Mode() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set the connections to passive mode. |
||
| 173 | * |
||
| 174 | * @throws ConnectionRuntimeException |
||
| 175 | */ |
||
| 176 | protected function setConnectionPassiveMode() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the connection root. |
||
| 191 | */ |
||
| 192 | protected function setConnectionRoot() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Login. |
||
| 210 | * |
||
| 211 | * @throws ConnectionRuntimeException |
||
| 212 | */ |
||
| 213 | protected function login() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Disconnect from the FTP server. |
||
| 235 | */ |
||
| 236 | public function disconnect() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @inheritdoc |
||
| 247 | */ |
||
| 248 | public function write($path, $contents, Config $config) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @inheritdoc |
||
| 268 | */ |
||
| 269 | public function writeStream($path, $resource, Config $config) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | */ |
||
| 289 | public function update($path, $contents, Config $config) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @inheritdoc |
||
| 296 | */ |
||
| 297 | public function updateStream($path, $resource, Config $config) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @inheritdoc |
||
| 304 | */ |
||
| 305 | public function rename($path, $newpath) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @inheritdoc |
||
| 312 | */ |
||
| 313 | public function delete($path) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @inheritdoc |
||
| 320 | */ |
||
| 321 | public function deleteDir($dirname) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @inheritdoc |
||
| 341 | */ |
||
| 342 | public function createDir($dirname, Config $config) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Create a directory. |
||
| 364 | * |
||
| 365 | * @param string $directory |
||
| 366 | * @param resource $connection |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | protected function createActualDirectory($directory, $connection) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @inheritdoc |
||
| 390 | */ |
||
| 391 | public function getMetadata($path) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @inheritdoc |
||
| 422 | */ |
||
| 423 | public function getMimetype($path) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @inheritdoc |
||
| 436 | */ |
||
| 437 | public function getTimestamp($path) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @inheritdoc |
||
| 446 | */ |
||
| 447 | public function read($path) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @inheritdoc |
||
| 462 | */ |
||
| 463 | public function readStream($path) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @inheritdoc |
||
| 480 | */ |
||
| 481 | public function setVisibility($path, $visibility) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @inheritdoc |
||
| 494 | * |
||
| 495 | * @param string $directory |
||
| 496 | */ |
||
| 497 | protected function listDirectoryContents($directory, $recursive = true) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @inheritdoc |
||
| 511 | * |
||
| 512 | * @param string $directory |
||
| 513 | */ |
||
| 514 | protected function listDirectoryContentsRecursive($directory) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Check if the connection is open. |
||
| 532 | * |
||
| 533 | * @return bool |
||
| 534 | * |
||
| 535 | * @throws ConnectionErrorException |
||
| 536 | */ |
||
| 537 | public function isConnected() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @return bool |
||
| 545 | */ |
||
| 546 | protected function isPureFtpdServer() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * The ftp_rawlist function with optional escaping. |
||
| 555 | * |
||
| 556 | * @param string $options |
||
| 557 | * @param string $path |
||
| 558 | * |
||
| 559 | * @return array |
||
| 560 | */ |
||
| 561 | protected function ftpRawlist($options, $path) |
||
| 572 | |||
| 573 | private function getRawExecResponseCode($command) |
||
| 579 | } |
||
| 580 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: