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 |
||
| 24 | class ServiceAdapter extends AbstractServiceAdapter |
||
| 25 | { |
||
| 26 | /** @var resource */ |
||
| 27 | private $connectionId = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Disconnected by garbage collection. |
||
| 31 | */ |
||
| 32 | public function __destruct() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Connect and login to remote host. |
||
| 39 | * |
||
| 40 | * @return ServiceInterface |
||
| 41 | */ |
||
| 42 | public function connect() : ServiceInterface |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Disconnect from remote host. |
||
| 68 | * |
||
| 69 | * @return ServiceInterface |
||
| 70 | */ |
||
| 71 | public function disconnect() : ServiceInterface |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Toggles connection security level. |
||
| 81 | * |
||
| 82 | * @param bool $state |
||
| 83 | * @return ServiceInterface |
||
| 84 | */ |
||
| 85 | public function setSecureConnection(bool $state) : ServiceInterface |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Toggles connection passive mode. |
||
| 100 | * |
||
| 101 | * @param bool $state |
||
| 102 | * @return ServiceInterface |
||
| 103 | */ |
||
| 104 | public function setPassiveMode(bool $state) : ServiceInterface |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Sets remote path. |
||
| 112 | * |
||
| 113 | * @param string $path |
||
| 114 | * @return ServiceInterface |
||
| 115 | */ |
||
| 116 | public function setRemotePath(string $path) : ServiceInterface |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Gets remote path. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getRemotePath() : string |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Lists remote path. |
||
| 147 | * |
||
| 148 | * @param null|string $path |
||
| 149 | * @param bool|null $changeToDirectory |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function getRemoteFileList(? string $path, ? bool $changeToDirectory) : array |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $fileData |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | private function getFileType(string $fileData) : string |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param array $fileData |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | private function getFileDate(array $fileData) : string |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Converts file rights string into octal value. |
||
| 237 | * |
||
| 238 | * @param string $permissions The UNIX-style permission string, e.g.: 'drwxr-xr-x' |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | private function getOctalChmod(string $permissions) : string |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Uploads file to remote host. |
||
| 281 | * |
||
| 282 | * @see self::setRemotePath |
||
| 283 | * @see self::setLocalPath |
||
| 284 | * |
||
| 285 | * @param string $sourceFileName |
||
| 286 | * @param string $destinationFileName |
||
| 287 | * @param int $fileMode |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | public function upload( |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Downloads file from remote host. |
||
| 318 | * |
||
| 319 | * @see self::setRemotePath |
||
| 320 | * @see self::setLocalPath |
||
| 321 | * |
||
| 322 | * @param string $remoteFileName |
||
| 323 | * @param string $localFileName |
||
| 324 | * @param int $fileMode |
||
| 325 | * @return mixed |
||
| 326 | */ |
||
| 327 | public function download( |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Checks local file, and generates new unique name if necessary. |
||
| 354 | * |
||
| 355 | * @param string $localFileName |
||
| 356 | * @param bool $forceUnique |
||
| 357 | * @throws RuntimeException |
||
| 358 | */ |
||
| 359 | private function checkLocalFile(string&$localFileName, bool $forceUnique = false) : void |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Check remote file name. |
||
| 397 | * |
||
| 398 | * @param string $remoteFileName |
||
| 399 | */ |
||
| 400 | private function checkRemoteFile(string&$remoteFileName) : void |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Moves file on remote host. |
||
| 412 | * |
||
| 413 | * @param string $currentPath |
||
| 414 | * @param string $newPath |
||
| 415 | * @return ServiceInterface |
||
| 416 | */ |
||
| 417 | public function moveRemoteFile(string $currentPath, string $newPath) : ServiceInterface |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Deletes file on remote host. |
||
| 433 | * |
||
| 434 | * @param string $path |
||
| 435 | * @return ServiceInterface |
||
| 436 | */ |
||
| 437 | public function deleteRemoteFile(string $path) : ServiceInterface |
||
| 447 | } |
||
| 448 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.