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 |
||
48 | class Rsync extends BaseTask implements CommandInterface |
||
49 | { |
||
50 | use \Robo\Common\ExecOneCommand; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $command; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $fromUser; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $fromHost; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $fromPath; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $toUser; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $toHost; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $toPath; |
||
86 | |||
87 | /** |
||
88 | * @return static |
||
89 | */ |
||
90 | public static function init() |
||
94 | |||
95 | public function __construct() |
||
99 | |||
100 | /** |
||
101 | * This can either be a full rsync path spec (user@host:path) or just a path. |
||
102 | * In case of the former do not specify host and user. |
||
103 | * |
||
104 | * @param string|array $path |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function fromPath($path) |
||
114 | |||
115 | /** |
||
116 | * This can either be a full rsync path spec (user@host:path) or just a path. |
||
117 | * In case of the former do not specify host and user. |
||
118 | * |
||
119 | * @param string $path |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function toPath($path) |
||
129 | |||
130 | /** |
||
131 | * @param string $fromUser |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function fromUser($fromUser) |
||
140 | |||
141 | /** |
||
142 | * @param string $fromHost |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function fromHost($fromHost) |
||
151 | |||
152 | /** |
||
153 | * @param string $toUser |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function toUser($toUser) |
||
162 | |||
163 | /** |
||
164 | * @param string $toHost |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function toHost($toHost) |
||
173 | |||
174 | /** |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function progress() |
||
183 | |||
184 | /** |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function stats() |
||
193 | |||
194 | /** |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function recursive() |
||
203 | |||
204 | /** |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function verbose() |
||
213 | |||
214 | /** |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function checksum() |
||
223 | |||
224 | /** |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function archive() |
||
233 | |||
234 | /** |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function compress() |
||
243 | |||
244 | /** |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function owner() |
||
253 | |||
254 | /** |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function group() |
||
263 | |||
264 | /** |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function times() |
||
273 | |||
274 | /** |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function delete() |
||
283 | |||
284 | /** |
||
285 | * @param int $seconds |
||
286 | * |
||
287 | * @return $this |
||
288 | */ |
||
289 | public function timeout($seconds) |
||
295 | |||
296 | /** |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function humanReadable() |
||
305 | |||
306 | /** |
||
307 | * @return $this |
||
308 | */ |
||
309 | public function wholeFile() |
||
315 | |||
316 | /** |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function dryRun() |
||
325 | |||
326 | /** |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function itemizeChanges() |
||
335 | |||
336 | /** |
||
337 | * Excludes .git, .svn and .hg items at any depth. |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function excludeVcs() |
||
349 | |||
350 | /** |
||
351 | * @param array|string $pattern |
||
352 | * |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function exclude($pattern) |
||
359 | |||
360 | /** |
||
361 | * @param string $file |
||
362 | * |
||
363 | * @return $this |
||
364 | * |
||
365 | * @throws \Robo\Exception\TaskException |
||
366 | */ |
||
367 | public function excludeFrom($file) |
||
375 | |||
376 | /** |
||
377 | * @param array|string $pattern |
||
378 | * |
||
379 | * @return $this |
||
380 | */ |
||
381 | public function includeFilter($pattern) |
||
385 | |||
386 | /** |
||
387 | * @param array|string $pattern |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | public function filter($pattern) |
||
395 | |||
396 | /** |
||
397 | * @param string $file |
||
398 | * |
||
399 | * @return $this |
||
400 | * |
||
401 | * @throws \Robo\Exception\TaskException |
||
402 | */ |
||
403 | public function filesFrom($file) |
||
411 | |||
412 | /** |
||
413 | * @param string $command |
||
414 | * |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function remoteShell($command) |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function run() |
||
433 | |||
434 | /** |
||
435 | * Returns command that can be executed. |
||
436 | * This method is used to pass generated command from one task to another. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getCommand() |
||
449 | |||
450 | /** |
||
451 | * @param string $from |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | protected function getFromPathSpec($from) |
||
459 | |||
460 | /** |
||
461 | * @return string |
||
462 | */ |
||
463 | protected function getToPathSpec() |
||
467 | |||
468 | /** |
||
469 | * @param string $host |
||
470 | * @param string $user |
||
471 | * @param string $path |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | protected function getPathSpec($host, $user, $path) |
||
487 | } |
||
488 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.