Complex classes like FtpClient 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 FtpClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class FtpClient extends AbstractFtpClient { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor. |
||
| 27 | * |
||
| 28 | * @param Authenticator $authenticator The authenticator. |
||
| 29 | */ |
||
| 30 | public function __construct(Authenticator $authenticator) { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Changes to the parent directory. |
||
| 41 | * |
||
| 42 | * @return FtpClient Returns this FTP client. |
||
| 43 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 44 | */ |
||
| 45 | public function cdup() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Changes the current directory on a FTP server. |
||
| 56 | * |
||
| 57 | * @param string $directory The directory. |
||
| 58 | * @return FtpClient Returns this FTP client. |
||
| 59 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 60 | */ |
||
| 61 | public function chdir($directory) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set permissions on a file via FTP. |
||
| 72 | * |
||
| 73 | * @param int $mode The mode. |
||
| 74 | * @param string $filename The filename. |
||
| 75 | * @return FtpClient Returns this FTP client. |
||
| 76 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 77 | */ |
||
| 78 | public function chmod($mode, $filename) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Closes an FTP connection. |
||
| 89 | * |
||
| 90 | * @return FtpClient Returns this FTP client. |
||
| 91 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 92 | */ |
||
| 93 | public function close() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Opens an FTP connection. |
||
| 104 | * |
||
| 105 | * @param int $timeout The timeout. |
||
| 106 | * @return FtpClient Returns this FTP client. |
||
| 107 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 108 | */ |
||
| 109 | public function connect($timeout = 90) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Deletes a file on the FTP server. |
||
| 124 | * |
||
| 125 | * @param string $path The path. |
||
| 126 | * @return FtpClient Returns this FTP client. |
||
| 127 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 128 | */ |
||
| 129 | public function delete($path) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Downloads a file from the FTP server and saves to an open file. |
||
| 140 | * |
||
| 141 | * @param resource $localStream The local stream. |
||
| 142 | * @param string $remoteFile The remote file. |
||
| 143 | * @param int $mode The mode. |
||
| 144 | * @param int $resumePos The resume position. |
||
| 145 | * @return FtpClient Returns this FTP client. |
||
| 146 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 147 | */ |
||
| 148 | public function fget($localStream, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Uploads from an open file to the FTP server. |
||
| 159 | * |
||
| 160 | * @param string $remoteFile The remote file. |
||
| 161 | * @param resource $localStream The local stream. |
||
| 162 | * @param int $mode The mode. |
||
| 163 | * @param int $startPos The start position. |
||
| 164 | * @return FtpClient Returns this FTP client. |
||
| 165 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 166 | */ |
||
| 167 | public function fput($remoteFile, $localStream, $mode = FTP_BINARY, $startPos = 0) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Downloads a file from the FTP server. |
||
| 178 | * |
||
| 179 | * @param string $localFile The local file. |
||
| 180 | * @param string $remoteFile The remote file. |
||
| 181 | * @param int $mode The mode. |
||
| 182 | * @param int $resumePos The resume position. |
||
| 183 | * @return FtpClient Returns this FTP client. |
||
| 184 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 185 | */ |
||
| 186 | public function get($localFile, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Logs in to an FTP connection. |
||
| 197 | * |
||
| 198 | * @return FtpClient Returns this FTP client. |
||
| 199 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 200 | */ |
||
| 201 | public function login() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns the last modified time of the given file. |
||
| 215 | * |
||
| 216 | * @param string $remoteFile The remote file. |
||
| 217 | * @return int Returns the last modified time of the given file. |
||
| 218 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 219 | */ |
||
| 220 | public function mdtm($remoteFile) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Creates a directory. |
||
| 232 | * |
||
| 233 | * @param string $directory The directory. |
||
| 234 | * @return FtpClient Returns this FTP client. |
||
| 235 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 236 | */ |
||
| 237 | public function mkdir($directory) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns a list of files in the given directory. |
||
| 248 | * |
||
| 249 | * @param string $directory The directory. |
||
| 250 | * @return array Returns a list of files in the given directory. |
||
| 251 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 252 | */ |
||
| 253 | public function mlsd($directory) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Retrieves a file from the FTP server and writes it to an open file (non-blocking). |
||
| 265 | * |
||
| 266 | * @param resource $localStream The local stream. |
||
| 267 | * @param string $remoteFile The remote file. |
||
| 268 | * @param int $mode The mode. |
||
| 269 | * @param int $resumePos The resume position. |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function nbFget($localStream, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Stores a file from an open file to the FTP server (non-blocking). |
||
| 278 | * |
||
| 279 | * @param string $remoteFile The remote file. |
||
| 280 | * @param resource $localStream The local stream. |
||
| 281 | * @param int $mode The mode. |
||
| 282 | * @param int $startPos The start position. |
||
| 283 | * @return int |
||
| 284 | */ |
||
| 285 | public function nbFput($remoteFile, $localStream, $mode = FTP_BINARY, $startPos = 0) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Retrieves a file from the FTP server and writes it to a local file (non-blocking). |
||
| 291 | * |
||
| 292 | * @param string $localFile The local file. |
||
| 293 | * @param string $remoteFile The remote file. |
||
| 294 | * @param int $mode The mode. |
||
| 295 | * @param int $resumePos The resume position. |
||
| 296 | * @return int |
||
| 297 | */ |
||
| 298 | public function nbGet($localFile, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Stores a file from an open file to the FTP server (non-blocking). |
||
| 304 | * |
||
| 305 | * @param string $remoteFile The remote file. |
||
| 306 | * @param string $localFile The local file. |
||
| 307 | * @param int $mode The mode. |
||
| 308 | * @param int $startPos The start position. |
||
| 309 | * @return int |
||
| 310 | */ |
||
| 311 | public function nbPut($remoteFile, $localFile, $mode = FTP_BINARY, $startPos = 0) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns a list of files in the given directory. |
||
| 317 | * |
||
| 318 | * @param string $directory The directory. |
||
| 319 | * @return string[] Returns a list of files in the given directory. |
||
| 320 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 321 | */ |
||
| 322 | public function nlist($directory) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Turns passive mode on or off. |
||
| 334 | * |
||
| 335 | * @param bool $pasv The passive mode. |
||
| 336 | * @return FtpClient Returns this FTP client. |
||
| 337 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 338 | */ |
||
| 339 | public function pasv($pasv) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Uploads a file to the FTP server. |
||
| 350 | * |
||
| 351 | * @param string $localFile The local file. |
||
| 352 | * @param string $remoteFile The remote file. |
||
| 353 | * @param int $mode The mode. |
||
| 354 | * @param int $startPos The start position. |
||
| 355 | * @return FtpClient Returns this FTP client. |
||
| 356 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 357 | */ |
||
| 358 | public function put($localFile, $remoteFile, $mode = FTP_IMAGE, $startPos = 0) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns the current directory name. |
||
| 369 | * |
||
| 370 | * @return FtpClient Returns this FTP client. |
||
| 371 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 372 | */ |
||
| 373 | public function pwd() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns a detailed list of files in the given directory. |
||
| 384 | * |
||
| 385 | * @param string $directory The directory. |
||
| 386 | * @param bool $recursive Recursive ? |
||
| 387 | * @return array Returns a detailed list of files in the given directory. |
||
| 388 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 389 | */ |
||
| 390 | public function rawList($directory, $recursive = false) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Renames a file or a directory on the FTP server. |
||
| 402 | * |
||
| 403 | * @param string $oldName The old name. |
||
| 404 | * @param string $newName The new name. |
||
| 405 | * @return FtpClient Returns this FTP client. |
||
| 406 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 407 | */ |
||
| 408 | public function rename($oldName, $newName) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Removes a directory. |
||
| 419 | * |
||
| 420 | * @param string $directory The directory. |
||
| 421 | * @return FtpClient Returns this FTP client. |
||
| 422 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 423 | */ |
||
| 424 | public function rmdir($directory) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns the size of the given file. |
||
| 435 | * |
||
| 436 | * @param string $remoteFile The remote file. |
||
| 437 | * @return int Returns the size of the given file. |
||
| 438 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 439 | */ |
||
| 440 | public function size($remoteFile) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Returns the system type identifier of the remote FTP server. |
||
| 452 | * |
||
| 453 | * @return string Returns the system type identifier of the remote FTP server. |
||
| 454 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
| 455 | */ |
||
| 456 | public function systype() { |
||
| 465 | } |
||
| 466 |