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) |
||
165 | |||
166 | /** |
||
167 | * Sync $origin directory with $target one. |
||
168 | * If SSH was configured, you must use absolute path |
||
169 | * in the target directory |
||
170 | * |
||
171 | * @param $origin |
||
172 | * @param $target |
||
173 | * |
||
174 | * @throws \InvalidArgumentException If the command failed |
||
175 | */ |
||
176 | public function sync($origin, $target) |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getExecutable() |
||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function getArchive() |
||
198 | |||
199 | /** |
||
200 | * @param $archive |
||
201 | */ |
||
202 | public function setArchive($archive) |
||
206 | |||
207 | /** |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function getPruneEmptyDirs() |
||
214 | |||
215 | /** |
||
216 | * @param $pruneEmptyDirs |
||
217 | */ |
||
218 | public function setPruneEmptyDirs($pruneEmptyDirs) |
||
222 | |||
223 | |||
224 | /** |
||
225 | * @param $skipNewerFiles |
||
226 | */ |
||
227 | public function setSkipNewerFiles($skipNewerFiles) |
||
231 | |||
232 | /** |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function getSkipNewerFiles() |
||
239 | |||
240 | /** |
||
241 | * @param $followSymLinks |
||
242 | */ |
||
243 | public function setFollowSymLinks($followSymLinks) |
||
247 | |||
248 | /** |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function getFollowSymLinks() |
||
255 | |||
256 | /** |
||
257 | * @param $dryRun |
||
258 | */ |
||
259 | public function setDryRun($dryRun) |
||
263 | |||
264 | /** |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function getDryRun() |
||
271 | |||
272 | /** |
||
273 | * @param $optionalParameters |
||
274 | */ |
||
275 | public function setOptionalParameters($optionalParameters) |
||
279 | |||
280 | /** |
||
281 | * @return array |
||
282 | */ |
||
283 | public function getOptionalParameters() |
||
287 | |||
288 | /** |
||
289 | * @param $verbose |
||
290 | */ |
||
291 | public function setVerbose($verbose) |
||
295 | |||
296 | /** |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function getVerbose() |
||
303 | |||
304 | /** |
||
305 | * @param $deleteExcluded |
||
306 | */ |
||
307 | public function setDeleteExcluded($deleteExcluded) |
||
311 | |||
312 | /** |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function getDeleteExcluded() |
||
319 | |||
320 | /** |
||
321 | * @param $deleteFromTarget |
||
322 | */ |
||
323 | public function setDeleteFromTarget($deleteFromTarget) |
||
327 | |||
328 | /** |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function getDeleteFromTarget() |
||
335 | |||
336 | /** |
||
337 | * @param $exclude |
||
338 | */ |
||
339 | public function setExclude($exclude) |
||
343 | |||
344 | /** |
||
345 | * @return array |
||
346 | */ |
||
347 | public function getExclude() |
||
351 | |||
352 | /** |
||
353 | * @param $exclude |
||
354 | */ |
||
355 | public function setExcludeFrom($excludeFrom) |
||
359 | |||
360 | /** |
||
361 | * @return string |
||
362 | */ |
||
363 | public function getExcludeFrom() |
||
367 | |||
368 | /** |
||
369 | * @param $recursive |
||
370 | */ |
||
371 | public function setRecursive($recursive) |
||
375 | |||
376 | /** |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function getRecursive() |
||
383 | |||
384 | /** |
||
385 | * @param bool $times |
||
386 | */ |
||
387 | public function setTimes($times) |
||
391 | |||
392 | /** |
||
393 | * @return bool |
||
394 | */ |
||
395 | public function getTimes() |
||
399 | |||
400 | /** |
||
401 | * @param $showOutput |
||
402 | */ |
||
403 | public function setShowOutput($showOutput) |
||
407 | |||
408 | /** |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function getShowOutput() |
||
415 | |||
416 | /** |
||
417 | * @param $compression |
||
418 | */ |
||
419 | public function setCompression($compression) |
||
423 | |||
424 | /** |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function getCompression() |
||
431 | |||
432 | /** |
||
433 | * @param $remoteOrigin |
||
434 | */ |
||
435 | public function setRemoteOrigin($remoteOrigin) |
||
439 | |||
440 | /** |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function getRemoteOrigin() |
||
447 | |||
448 | /** |
||
449 | * @param $removeSource |
||
450 | */ |
||
451 | public function setRemoveSource($removeSource) |
||
455 | |||
456 | /** |
||
457 | * @return bool |
||
458 | */ |
||
459 | public function getRemoveSource() |
||
463 | |||
464 | /** |
||
465 | * @param $info |
||
466 | */ |
||
467 | public function setInfo($info) |
||
471 | |||
472 | /** |
||
473 | * @return bool |
||
474 | */ |
||
475 | public function getInfo() |
||
479 | |||
480 | /** |
||
481 | * @param $dest |
||
482 | */ |
||
483 | public function setCompareDest($dest) |
||
487 | |||
488 | /** |
||
489 | * @return string |
||
490 | */ |
||
491 | public function getCompareDest() |
||
495 | |||
496 | /** |
||
497 | * Gets command generated for this current |
||
498 | * rsync configuration. You can use it to test |
||
499 | * or execute it later without using the sync method |
||
500 | * |
||
501 | * @param $origin |
||
502 | * @param $target |
||
503 | * |
||
504 | * @return Command |
||
505 | */ |
||
506 | public function getCommand($origin, $target) |
||
607 | } |
||
608 |