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 SSH |
||
119 | */ |
||
120 | protected $ssh; |
||
121 | |||
122 | /** |
||
123 | * Injects and validates config |
||
124 | * |
||
125 | * @param array $options |
||
126 | */ |
||
127 | public function __construct(Array $options = array()) |
||
150 | |||
151 | /** |
||
152 | * @param $options |
||
153 | */ |
||
154 | public function setSshOptions($options) |
||
159 | |||
160 | /** |
||
161 | * Sync $origin directory with $target one. |
||
162 | * If SSH was configured, you must use absolute path |
||
163 | * in the target directory |
||
164 | * |
||
165 | * @param $origin |
||
166 | * @param $target |
||
167 | * |
||
168 | * @throws \InvalidArgumentException If the command failed |
||
169 | */ |
||
170 | public function sync($origin, $target) |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getExecutable() |
||
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function getArchive() |
||
192 | |||
193 | /** |
||
194 | * @param $archive |
||
195 | */ |
||
196 | public function setArchive($archive) |
||
200 | |||
201 | /** |
||
202 | * @param $skipNewerFiles |
||
203 | */ |
||
204 | public function setSkipNewerFiles($skipNewerFiles) |
||
208 | |||
209 | /** |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function getSkipNewerFiles() |
||
216 | |||
217 | /** |
||
218 | * @param $followSymLinks |
||
219 | */ |
||
220 | public function setFollowSymLinks($followSymLinks) |
||
224 | |||
225 | /** |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function getFollowSymLinks() |
||
232 | |||
233 | /** |
||
234 | * @param $dryRun |
||
235 | */ |
||
236 | public function setDryRun($dryRun) |
||
240 | |||
241 | /** |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function getDryRun() |
||
248 | |||
249 | /** |
||
250 | * @param $optionalParameters |
||
251 | */ |
||
252 | public function setOptionalParameters($optionalParameters) |
||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | public function getOptionalParameters() |
||
264 | |||
265 | /** |
||
266 | * @param $verbose |
||
267 | */ |
||
268 | public function setVerbose($verbose) |
||
272 | |||
273 | /** |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function getVerbose() |
||
280 | |||
281 | /** |
||
282 | * @param $deleteExcluded |
||
283 | */ |
||
284 | public function setDeleteExcluded($deleteExcluded) |
||
288 | |||
289 | /** |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function getDeleteExcluded() |
||
296 | |||
297 | /** |
||
298 | * @param $deleteFromTarget |
||
299 | */ |
||
300 | public function setDeleteFromTarget($deleteFromTarget) |
||
304 | |||
305 | /** |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function getDeleteFromTarget() |
||
312 | |||
313 | /** |
||
314 | * @param $exclude |
||
315 | */ |
||
316 | public function setExclude($exclude) |
||
320 | |||
321 | /** |
||
322 | * @return array |
||
323 | */ |
||
324 | public function getExclude() |
||
328 | |||
329 | /** |
||
330 | * @param $exclude |
||
331 | */ |
||
332 | public function setExcludeFrom($excludeFrom) |
||
336 | |||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | public function getExcludeFrom() |
||
344 | |||
345 | /** |
||
346 | * @param $recursive |
||
347 | */ |
||
348 | public function setRecursive($recursive) |
||
352 | |||
353 | /** |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function getRecursive() |
||
360 | |||
361 | /** |
||
362 | * @param bool $times |
||
363 | */ |
||
364 | public function setTimes($times) |
||
368 | |||
369 | /** |
||
370 | * @return bool |
||
371 | */ |
||
372 | public function getTimes() |
||
376 | |||
377 | /** |
||
378 | * @param $showOutput |
||
379 | */ |
||
380 | public function setShowOutput($showOutput) |
||
384 | |||
385 | /** |
||
386 | * @return bool |
||
387 | */ |
||
388 | public function getShowOutput() |
||
392 | |||
393 | /** |
||
394 | * @param $compression |
||
395 | */ |
||
396 | public function setCompression($compression) |
||
400 | |||
401 | /** |
||
402 | * @return bool |
||
403 | */ |
||
404 | public function getCompression() |
||
408 | |||
409 | /** |
||
410 | * @param $remoteOrigin |
||
411 | */ |
||
412 | public function setRemoteOrigin($remoteOrigin) |
||
416 | |||
417 | /** |
||
418 | * @return bool |
||
419 | */ |
||
420 | public function getRemoteOrigin() |
||
424 | |||
425 | /** |
||
426 | * @param $removeSource |
||
427 | */ |
||
428 | public function setRemoveSource($removeSource) |
||
432 | |||
433 | /** |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function getRemoveSource() |
||
440 | |||
441 | /** |
||
442 | * @param $info |
||
443 | */ |
||
444 | public function setInfo($info) |
||
448 | |||
449 | /** |
||
450 | * @return bool |
||
451 | */ |
||
452 | public function getInfo() |
||
456 | |||
457 | /** |
||
458 | * @param $dest |
||
459 | */ |
||
460 | public function setCompareDest($dest) |
||
464 | |||
465 | /** |
||
466 | * @return string |
||
467 | */ |
||
468 | public function getCompareDest() |
||
472 | |||
473 | /** |
||
474 | * Gets command generated for this current |
||
475 | * rsync configuration. You can use it to test |
||
476 | * or execute it later without using the sync method |
||
477 | * |
||
478 | * @param $origin |
||
479 | * @param $target |
||
480 | * |
||
481 | * @return Command |
||
482 | */ |
||
483 | public function getCommand($origin, $target) |
||
581 | } |
||
582 |