Complex classes like AbstractFtpAdapter 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 AbstractFtpAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | abstract class AbstractFtpAdapter |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @const VISIBILITY_PUBLIC public visibility |
||
| 9 | */ |
||
| 10 | const VISIBILITY_PUBLIC = 'public'; |
||
| 11 | /** |
||
| 12 | * @const VISIBILITY_PRIVATE private visibility |
||
| 13 | */ |
||
| 14 | const VISIBILITY_PRIVATE = 'private'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $configurable = [ |
||
| 20 | 'host', |
||
| 21 | 'port', |
||
| 22 | 'username', |
||
| 23 | 'password', |
||
| 24 | 'ssl', |
||
| 25 | 'timeout', |
||
| 26 | 'root', |
||
| 27 | 'permPrivate', |
||
| 28 | 'permPublic', |
||
| 29 | 'passive', |
||
| 30 | 'transferMode', |
||
| 31 | 'systemType', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var mixed |
||
| 36 | */ |
||
| 37 | protected $connection; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $host; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $port = 21; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string|null |
||
| 51 | */ |
||
| 52 | protected $username; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string|null |
||
| 56 | */ |
||
| 57 | protected $password; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $ssl = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | protected $timeout = 90; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected $passive = true; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $transferMode = FTP_BINARY; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $separator = '/'; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string|null |
||
| 86 | */ |
||
| 87 | protected $root; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $permPublic = 0744; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var int |
||
| 96 | */ |
||
| 97 | protected $permPrivate = 0700; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $systemType; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Constructor. |
||
| 106 | * |
||
| 107 | * @param array $config |
||
| 108 | */ |
||
| 109 | public function __construct(array $config) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Disconnect on destruction. |
||
| 116 | */ |
||
| 117 | public function __destruct() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set the config. |
||
| 124 | * |
||
| 125 | * @param array $config |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function setConfig(array $config) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Connect to the FTP server. |
||
| 147 | */ |
||
| 148 | public function connect() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the connections to passive mode. |
||
| 167 | * |
||
| 168 | * @throws \RuntimeException |
||
| 169 | */ |
||
| 170 | protected function setConnectionPassiveMode() |
||
| 178 | /** |
||
| 179 | * Set the connection root. |
||
| 180 | */ |
||
| 181 | protected function setConnectionRoot() |
||
| 194 | /** |
||
| 195 | * Login. |
||
| 196 | * |
||
| 197 | * @throws \RuntimeException |
||
| 198 | */ |
||
| 199 | protected function login() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Disconnect from the FTP server. |
||
| 218 | */ |
||
| 219 | public function disconnect() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritdoc |
||
| 229 | */ |
||
| 230 | public function read($path) |
||
| 240 | /** |
||
| 241 | * @inheritdoc |
||
| 242 | */ |
||
| 243 | public function readStream($path) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @inheritdoc |
||
| 257 | * |
||
| 258 | * @param string $directory |
||
| 259 | */ |
||
| 260 | protected function listDirectoryContents($directory, $recursive = true) |
||
| 265 | /** |
||
| 266 | * Check if the connection is open. |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function isConnected() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return mixed |
||
| 277 | */ |
||
| 278 | public function getConnection() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set the host. |
||
| 289 | * |
||
| 290 | * @param string $host |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function setHost($host) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the host. |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getHost() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set the ftp port. |
||
| 312 | * |
||
| 313 | * @param int|string $port |
||
| 314 | * |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function setPort($port) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the ftp port. |
||
| 325 | * |
||
| 326 | * @return int |
||
| 327 | */ |
||
| 328 | public function getPort() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set ftp username. |
||
| 335 | * |
||
| 336 | * @param string $username |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function setUsername($username) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns the ftp username. |
||
| 348 | * |
||
| 349 | * @return string username |
||
| 350 | */ |
||
| 351 | public function getUsername() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set the ftp password. |
||
| 358 | * |
||
| 359 | * @param string $password |
||
| 360 | * |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function setPassword($password) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns the password. |
||
| 371 | * |
||
| 372 | * @return string password |
||
| 373 | */ |
||
| 374 | public function getPassword() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Set if Ssl is enabled. |
||
| 381 | * |
||
| 382 | * @param bool $ssl |
||
| 383 | * |
||
| 384 | * @return $this |
||
| 385 | */ |
||
| 386 | public function setSsl($ssl) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get if ssl is enabled |
||
| 394 | * |
||
| 395 | * @return boolean |
||
| 396 | */ |
||
| 397 | public function isSsl() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set the amount of seconds before the connection should timeout. |
||
| 404 | * |
||
| 405 | * @param int $timeout |
||
| 406 | * |
||
| 407 | * @return $this |
||
| 408 | */ |
||
| 409 | public function setTimeout($timeout) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns the amount of seconds before the connection will timeout. |
||
| 417 | * |
||
| 418 | * @return int |
||
| 419 | */ |
||
| 420 | public function getTimeout() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set if passive is enabled |
||
| 427 | * |
||
| 428 | * @param boolean $passive |
||
| 429 | */ |
||
| 430 | public function setPassive($passive) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get if passive is enabled |
||
| 437 | * |
||
| 438 | * @return boolean |
||
| 439 | */ |
||
| 440 | public function isPassive() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set the transfer mode |
||
| 447 | * |
||
| 448 | * @param int $transferMode |
||
| 449 | */ |
||
| 450 | public function setTransferMode($transferMode) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the transfer mode |
||
| 457 | * |
||
| 458 | * @return int |
||
| 459 | */ |
||
| 460 | public function getTransferMode() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Set the separator used |
||
| 467 | * |
||
| 468 | * @param string $separator |
||
| 469 | */ |
||
| 470 | public function setSeparator($separator) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Returns the separator used |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function getSeparator() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Set the root folder to work from. |
||
| 487 | * |
||
| 488 | * @param string $root |
||
| 489 | * |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | public function setRoot($root) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Returns the root folder to work from. |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public function getRoot() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Set the public permission value. |
||
| 510 | * |
||
| 511 | * @param int $permPublic |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function setPermPublic($permPublic) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get the public permission value. |
||
| 523 | * |
||
| 524 | * @return int |
||
| 525 | */ |
||
| 526 | public function getPermPublic() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Set the private permission value. |
||
| 533 | * |
||
| 534 | * @param int $permPrivate |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | public function setPermPrivate($permPrivate) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get the private permission value. |
||
| 546 | * |
||
| 547 | * @return int |
||
| 548 | */ |
||
| 549 | public function getPermPrivate() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Set the FTP system type (windows or unix). |
||
| 556 | * |
||
| 557 | * @param string $systemType |
||
| 558 | * |
||
| 559 | * @return $this |
||
| 560 | */ |
||
| 561 | public function setSystemType($systemType) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Return the FTP system type. |
||
| 569 | * |
||
| 570 | * @return string |
||
| 571 | */ |
||
| 572 | public function getSystemType() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Normalize a directory listing. |
||
| 579 | * |
||
| 580 | * @param array $listing |
||
| 581 | * @param string $prefix |
||
| 582 | * |
||
| 583 | * @return array directory listing |
||
| 584 | */ |
||
| 585 | protected function normalizeListing(array $listing, $prefix = '') |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Sort a directory listing. |
||
| 602 | * |
||
| 603 | * @param array $result |
||
| 604 | * |
||
| 605 | * @return array sorted listing |
||
| 606 | */ |
||
| 607 | protected function sortListing(array $result) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Normalize a file entry. |
||
| 618 | * |
||
| 619 | * @param string $item |
||
| 620 | * @param string $base |
||
| 621 | * @return array normalized file array |
||
| 622 | * |
||
| 623 | * @throws \Exception |
||
| 624 | */ |
||
| 625 | protected function normalizeObject($item, $base) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Normalize a Unix file entry. |
||
| 638 | * |
||
| 639 | * @param string $item |
||
| 640 | * @param string $base |
||
| 641 | * |
||
| 642 | * @return array normalized file array |
||
| 643 | */ |
||
| 644 | protected function normalizeUnixObject($item, $base) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Normalize a Windows/DOS file entry. |
||
| 679 | * |
||
| 680 | * @param string $item |
||
| 681 | * @param string $base |
||
| 682 | * |
||
| 683 | * @return array normalized file array |
||
| 684 | */ |
||
| 685 | protected function normalizeWindowsObject($item, $base) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get the system type from a listing item. |
||
| 705 | * |
||
| 706 | * @param string $item |
||
| 707 | * |
||
| 708 | * @return string the system type |
||
| 709 | */ |
||
| 710 | protected function detectSystemType($item) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Get the file type from the permissions. |
||
| 720 | * |
||
| 721 | * @param string $permissions |
||
| 722 | * |
||
| 723 | * @return string file type |
||
| 724 | */ |
||
| 725 | protected function detectType($permissions) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Normalize a permissions string. |
||
| 732 | * |
||
| 733 | * @param string $permissions |
||
| 734 | * |
||
| 735 | * @return int |
||
| 736 | */ |
||
| 737 | protected function normalizePermissions($permissions) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Filter out dot-directories. |
||
| 756 | * |
||
| 757 | * @param array $list |
||
| 758 | * |
||
| 759 | * @return array |
||
| 760 | */ |
||
| 761 | public function removeDotDirectories(array $list) |
||
| 771 | } |
||
| 772 |