Complex classes like Rsync 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 Rsync, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Rsync extends AbstractProtocol |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $executable = "/usr/bin/rsync"; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $archive = true; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $skipNewerFiles = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $followSymLinks = true; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $dryRun = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $optionalParameters = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $verbose = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $deleteFromTarget = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $deleteExcluded = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $exclude = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $excludeFrom = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | protected $recursive = true; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $times = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $showOutput = true; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $compression = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | protected $remoteOrigin = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | protected $removeSource = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $info = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | protected $compareDest = false; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | protected $pruneEmptyDirs = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var SSH |
||
| 124 | */ |
||
| 125 | protected $ssh; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Injects and validates config |
||
| 129 | * |
||
| 130 | * @param array $options |
||
| 131 | */ |
||
| 132 | public function __construct(Array $options = array()) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $options |
||
| 159 | */ |
||
| 160 | public function setSshOptions($options) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Sync $origin directory with $target one. |
||
| 167 | * If SSH was configured, you must use absolute path |
||
| 168 | * in the target directory |
||
| 169 | * |
||
| 170 | * @param $origin |
||
| 171 | * @param $target |
||
| 172 | * |
||
| 173 | * @throws \InvalidArgumentException If the command failed |
||
| 174 | */ |
||
| 175 | public function sync($origin, $target) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getExecutable() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function getArchive() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $archive |
||
| 200 | */ |
||
| 201 | public function setArchive($archive) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function getPruneEmptyDirs() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param $pruneEmptyDirs |
||
| 216 | */ |
||
| 217 | public function setPruneEmptyDirs($pruneEmptyDirs) |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $skipNewerFiles |
||
| 225 | */ |
||
| 226 | public function setSkipNewerFiles($skipNewerFiles) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public function getSkipNewerFiles() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $followSymLinks |
||
| 241 | */ |
||
| 242 | public function setFollowSymLinks($followSymLinks) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function getFollowSymLinks() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $dryRun |
||
| 257 | */ |
||
| 258 | public function setDryRun($dryRun) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function getDryRun() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param $optionalParameters |
||
| 273 | */ |
||
| 274 | public function setOptionalParameters($optionalParameters) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getOptionalParameters() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param $verbose |
||
| 289 | */ |
||
| 290 | public function setVerbose($verbose) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function getVerbose() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param $deleteExcluded |
||
| 305 | */ |
||
| 306 | public function setDeleteExcluded($deleteExcluded) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function getDeleteExcluded() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $deleteFromTarget |
||
| 321 | */ |
||
| 322 | public function setDeleteFromTarget($deleteFromTarget) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function getDeleteFromTarget() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param $exclude |
||
| 337 | */ |
||
| 338 | public function setExclude($exclude) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function getExclude() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param $exclude |
||
| 353 | */ |
||
| 354 | public function setExcludeFrom($excludeFrom) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getExcludeFrom() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param $recursive |
||
| 369 | */ |
||
| 370 | public function setRecursive($recursive) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public function getRecursive() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param bool $times |
||
| 385 | */ |
||
| 386 | public function setTimes($times) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | public function getTimes() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param $showOutput |
||
| 401 | */ |
||
| 402 | public function setShowOutput($showOutput) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | public function getShowOutput() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $compression |
||
| 417 | */ |
||
| 418 | public function setCompression($compression) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return bool |
||
| 425 | */ |
||
| 426 | public function getCompression() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param $remoteOrigin |
||
| 433 | */ |
||
| 434 | public function setRemoteOrigin($remoteOrigin) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function getRemoteOrigin() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param $removeSource |
||
| 449 | */ |
||
| 450 | public function setRemoveSource($removeSource) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | public function getRemoveSource() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param $info |
||
| 465 | */ |
||
| 466 | public function setInfo($info) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | public function getInfo() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param $dest |
||
| 481 | */ |
||
| 482 | public function setCompareDest($dest) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | public function getCompareDest() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Gets command generated for this current |
||
| 497 | * rsync configuration. You can use it to test |
||
| 498 | * or execute it later without using the sync method |
||
| 499 | * |
||
| 500 | * @param $origin |
||
| 501 | * @param $target |
||
| 502 | * |
||
| 503 | * @return Command |
||
| 504 | */ |
||
| 505 | public function getCommand($origin, $target) |
||
| 609 | } |
||
| 610 |