Complex classes like ServiceAdapter 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 ServiceAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ServiceAdapter implements ServiceInterface |
||
| 24 | { |
||
| 25 | /** @var resource */ |
||
| 26 | private $connectionId = null; |
||
| 27 | /** @var string */ |
||
| 28 | private $localPath = __DIR__; |
||
| 29 | /** @var array */ |
||
| 30 | protected $options = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * ServiceAdapter constructor. |
||
| 34 | * |
||
| 35 | * @param ConfigurationInterface $configuration |
||
| 36 | */ |
||
| 37 | public function __construct(ConfigurationInterface $configuration) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Disconnected by garbage collection. |
||
| 44 | */ |
||
| 45 | public function __destruct() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Connect and login to remote host. |
||
| 52 | * |
||
| 53 | * @return ServiceInterface |
||
| 54 | */ |
||
| 55 | public function connect() : ServiceInterface |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Disconnect from remote host. |
||
| 81 | * |
||
| 82 | * @return ServiceInterface |
||
| 83 | */ |
||
| 84 | public function disconnect() : ServiceInterface |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Sets an option data. |
||
| 94 | * |
||
| 95 | * @param string $key |
||
| 96 | * @param mixed $value |
||
| 97 | * @return ServiceInterface |
||
| 98 | */ |
||
| 99 | public function setOption(string $key, $value) : ServiceInterface |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Sets a group of options. |
||
| 107 | * |
||
| 108 | * @param array $options |
||
| 109 | * @return ServiceInterface |
||
| 110 | */ |
||
| 111 | public function setOptions(array $options) : ServiceInterface |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Gets a specific option data. |
||
| 119 | * |
||
| 120 | * @param string $key |
||
| 121 | * @param mixed $default |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | public function getOption(string $key, $default = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Toggles connection security level. |
||
| 131 | * |
||
| 132 | * @param bool $state |
||
| 133 | * @return ServiceInterface |
||
| 134 | */ |
||
| 135 | public function setSecureConnection(bool $state) : ServiceInterface |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Toggles connection passive mode. |
||
| 150 | * |
||
| 151 | * @param bool $state |
||
| 152 | * @return ServiceInterface |
||
| 153 | */ |
||
| 154 | public function setPassiveMode(bool $state) : ServiceInterface |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Sets remote path. |
||
| 162 | * |
||
| 163 | * @param string $path |
||
| 164 | * @return ServiceInterface |
||
| 165 | */ |
||
| 166 | public function setRemotePath(string $path) : ServiceInterface |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Gets remote path. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getRemotePath() : string |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets local path. |
||
| 197 | * |
||
| 198 | * @param string $path |
||
| 199 | * @return ServiceInterface |
||
| 200 | */ |
||
| 201 | public function setLocalPath(string $path) : ServiceInterface |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Gets local path. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getLocalPath() : string |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Lists remote path. |
||
| 240 | * |
||
| 241 | * @param null|string $path |
||
| 242 | * @param bool|null $changeToDirectory |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getRemoteFileList(? string $path, ? bool $changeToDirectory) : array |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Converts file rights string into octal value. |
||
| 298 | * |
||
| 299 | * @param string $permissions The UNIX-style permission string, e.g.: 'drwxr-xr-x' |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | private function getOctalChmod(string $permissions) : string |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Uploads file to remote host. |
||
| 342 | * |
||
| 343 | * @see self::setRemotePath |
||
| 344 | * @see self::setLocalPath |
||
| 345 | * |
||
| 346 | * @param string $sourceFileName |
||
| 347 | * @param string $destinationFileName |
||
| 348 | * @param int $fileMode |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | public function upload( |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Downloads file from remote host. |
||
| 379 | * |
||
| 380 | * @see self::setRemotePath |
||
| 381 | * @see self::setLocalPath |
||
| 382 | * |
||
| 383 | * @param string $remoteFileName |
||
| 384 | * @param string $localFileName |
||
| 385 | * @param int $fileMode |
||
| 386 | * @return mixed |
||
| 387 | */ |
||
| 388 | public function download( |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Checks local file, and generates new unique name if necessary. |
||
| 415 | * |
||
| 416 | * @param string $localFileName |
||
| 417 | * @param bool $forceUnique |
||
| 418 | */ |
||
| 419 | private function checkLocalFile(string&$localFileName, bool $forceUnique = false) : void |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Check remote file name. |
||
| 449 | * |
||
| 450 | * @param string $remoteFileName |
||
| 451 | */ |
||
| 452 | private function checkRemoteFile(string&$remoteFileName) : void |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Moves file on remote host. |
||
| 464 | * |
||
| 465 | * @param string $currentPath |
||
| 466 | * @param string $newPath |
||
| 467 | * @return ServiceInterface |
||
| 468 | */ |
||
| 469 | public function moveRemoteFile(string $currentPath, string $newPath) : ServiceInterface |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Deletes file on remote host. |
||
| 485 | * |
||
| 486 | * @param string $path |
||
| 487 | * @return ServiceInterface |
||
| 488 | */ |
||
| 489 | public function deleteRemoteFile(string $path) : ServiceInterface |
||
| 499 | } |
||
| 500 |