Complex classes like SftpAdapter 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 SftpAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class SftpAdapter extends AbstractFtpAdapter |
||
| 18 | { |
||
| 19 | use StreamedCopyTrait; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | protected $port = 22; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $hostFingerprint; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $privatekey; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $useAgent = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Agent |
||
| 43 | */ |
||
| 44 | private $agent; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $configurable = ['host', 'hostFingerprint', 'port', 'username', 'password', 'useAgent', 'agent', 'timeout', 'root', 'privateKey', 'permPrivate', 'permPublic', 'directoryPerm', 'NetSftpConnection', 'url']; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $statMap = ['mtime' => 'timestamp', 'size' => 'size']; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $directoryPerm = 0744; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Prefix a path. |
||
| 63 | * |
||
| 64 | * @param string $path |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 6 | protected function prefix($path) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Set the finger print of the public key of the host you are connecting to. |
||
| 75 | * |
||
| 76 | * If the key does not match the server identification, the connection will |
||
| 77 | * be aborted. |
||
| 78 | * |
||
| 79 | * @param string $fingerprint Example: '88:76:75:96:c1:26:7c:dd:9f:87:50:db:ac:c4:a8:7c'. |
||
| 80 | * |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | 6 | public function setHostFingerprint($fingerprint) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Set the private key (string or path to local file). |
||
| 92 | * |
||
| 93 | * @param string $key |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | 9 | public function setPrivateKey($key) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param boolean $useAgent |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function setUseAgent($useAgent) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param Agent $agent |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function setAgent(Agent $agent) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set permissions for new directory |
||
| 130 | * |
||
| 131 | * @param int $directoryPerm |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | 6 | public function setDirectoryPerm($directoryPerm) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Get permissions for new directory |
||
| 144 | * |
||
| 145 | * @return int |
||
| 146 | */ |
||
| 147 | 3 | public function getDirectoryPerm() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Inject the SFTP instance. |
||
| 154 | * |
||
| 155 | * @param SFTP $connection |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | 30 | public function setNetSftpConnection(SFTP $connection) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Set the url. |
||
| 168 | * |
||
| 169 | * @param string $url |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | public function setUrl($url) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * get full url from path if url is set. |
||
| 182 | * |
||
| 183 | * @param string path |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getUrl($path) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Connect. |
||
| 198 | */ |
||
| 199 | 21 | public function connect() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Login. |
||
| 208 | * |
||
| 209 | * @throws LogicException |
||
| 210 | */ |
||
| 211 | 21 | protected function login() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Convert the SSH RSA public key into a hex formatted fingerprint. |
||
| 234 | * |
||
| 235 | * @param string $publickey |
||
| 236 | * @return string Hex formatted fingerprint, e.g. '88:76:75:96:c1:26:7c:dd:9f:87:50:db:ac:c4:a8:7c'. |
||
| 237 | */ |
||
| 238 | 6 | private function getHexFingerprintFromSshPublicKey ($publickey) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Set the connection root. |
||
| 246 | */ |
||
| 247 | 15 | protected function setConnectionRoot() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get the password, either the private key or a plain text password. |
||
| 263 | * |
||
| 264 | * @return Agent|RSA|string |
||
| 265 | */ |
||
| 266 | 21 | public function getAuthentication() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Get the private get with the password or private key contents. |
||
| 281 | * |
||
| 282 | * @return RSA |
||
| 283 | */ |
||
| 284 | 9 | public function getPrivateKey() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @return Agent|bool |
||
| 303 | */ |
||
| 304 | public function getAgent() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * List the contents of a directory. |
||
| 315 | * |
||
| 316 | * @param string $directory |
||
| 317 | * @param bool $recursive |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | 6 | protected function listDirectoryContents($directory, $recursive = true) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Normalize a listing response. |
||
| 350 | * |
||
| 351 | * @param string $path |
||
| 352 | * @param array $object |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | 6 | protected function normalizeListingObject($path, array $object) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Disconnect. |
||
| 375 | */ |
||
| 376 | 15 | public function disconnect() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @inheritdoc |
||
| 383 | */ |
||
| 384 | 6 | public function write($path, $contents, Config $config) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @inheritdoc |
||
| 395 | */ |
||
| 396 | 6 | public function writeStream($path, $resource, Config $config) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Upload a file. |
||
| 407 | * |
||
| 408 | * @param string $path |
||
| 409 | * @param string|resource $contents |
||
| 410 | * @param Config $config |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | 12 | public function upload($path, $contents, Config $config) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @inheritdoc |
||
| 432 | */ |
||
| 433 | 6 | public function read($path) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @inheritdoc |
||
| 446 | */ |
||
| 447 | 3 | public function readStream($path) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @inheritdoc |
||
| 464 | */ |
||
| 465 | 3 | public function update($path, $contents, Config $config) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @inheritdoc |
||
| 472 | */ |
||
| 473 | 3 | public function updateStream($path, $contents, Config $config) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @inheritdoc |
||
| 480 | */ |
||
| 481 | 3 | public function delete($path) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @inheritdoc |
||
| 490 | */ |
||
| 491 | 3 | public function rename($path, $newpath) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @inheritdoc |
||
| 500 | */ |
||
| 501 | 3 | public function deleteDir($dirname) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * @inheritdoc |
||
| 510 | */ |
||
| 511 | 39 | public function has($path) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @inheritdoc |
||
| 518 | */ |
||
| 519 | 48 | public function getMetadata($path) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @inheritdoc |
||
| 537 | */ |
||
| 538 | 6 | public function getTimestamp($path) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @inheritdoc |
||
| 545 | */ |
||
| 546 | 3 | public function getMimetype($path) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * @inheritdoc |
||
| 559 | */ |
||
| 560 | 3 | public function createDir($dirname, Config $config) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * @inheritdoc |
||
| 573 | */ |
||
| 574 | 6 | public function getVisibility($path) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * @inheritdoc |
||
| 581 | */ |
||
| 582 | 12 | public function setVisibility($path, $visibility) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * @inheritdoc |
||
| 597 | */ |
||
| 598 | 6 | public function isConnected() |
|
| 606 | } |
||
| 607 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: