Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
22 | class Rsync extends Cli implements Simulator |
||
23 | { |
||
24 | use RsyncTrait; |
||
25 | |||
26 | /** |
||
27 | * Setup the rsync sync. |
||
28 | * |
||
29 | * @see \phpbu\App\Backup\Sync::setup() |
||
30 | * @param array $options |
||
31 | * @throws \phpbu\App\Backup\Sync\Exception |
||
32 | */ |
||
33 | 13 | public function setup(array $options) |
|
34 | { |
||
35 | try { |
||
36 | 13 | $this->setupRsync($options); |
|
37 | 1 | } catch (\Exception $e) { |
|
38 | 1 | throw new Exception($e->getMessage()); |
|
39 | } |
||
40 | 12 | } |
|
41 | |||
42 | /** |
||
43 | * Execute the sync. |
||
44 | * |
||
45 | * @see \phpbu\App\Backup\Sync::sync() |
||
46 | * @param \phpbu\App\Backup\Target $target |
||
47 | * @param \phpbu\App\Result $result |
||
48 | * @throws \phpbu\App\Backup\Sync\Exception |
||
49 | */ |
||
50 | 2 | View Code Duplication | public function sync(Target $target, Result $result) |
51 | { |
||
52 | 2 | if ($this->args) { |
|
53 | // pro mode define all arguments yourself |
||
54 | // WARNING! no escaping is done by phpbu |
||
55 | 1 | $result->debug('WARNING: phpbu uses your rsync args without escaping'); |
|
56 | } |
||
57 | 2 | $rsync = $this->execute($target); |
|
58 | |||
59 | 2 | $result->debug($rsync->getCmd()); |
|
60 | |||
61 | 2 | if (!$rsync->isSuccessful()) { |
|
62 | 1 | throw new Exception('rsync failed: ' . $rsync->getStdErr()); |
|
63 | } |
||
64 | 1 | } |
|
65 | |||
66 | /** |
||
67 | * Simulate the sync execution. |
||
68 | * |
||
69 | * @param \phpbu\App\Backup\Target $target |
||
70 | * @param \phpbu\App\Result $result |
||
71 | */ |
||
72 | 1 | public function simulate(Target $target, Result $result) |
|
73 | { |
||
74 | 1 | $result->debug( |
|
75 | 1 | 'sync backup with rsync' . PHP_EOL |
|
76 | 1 | . $this->getExecutable($target)->getCommandPrintable() |
|
77 | ); |
||
78 | 1 | } |
|
79 | |||
80 | /** |
||
81 | * Setup the Executable to run the 'rsync' command. |
||
82 | * |
||
83 | * @param \phpbu\App\Backup\Target |
||
84 | * @return \phpbu\App\Cli\Executable |
||
85 | */ |
||
86 | 9 | protected function createExecutable(Target $target) : Executable |
|
109 | |||
110 | /** |
||
111 | * Execute the remote clean up if needed |
||
112 | * |
||
113 | * @param \phpbu\App\Backup\Target $target |
||
114 | * @param \phpbu\App\Result $result |
||
115 | */ |
||
116 | public function cleanup(Target $target, Result $result) |
||
120 | } |
||
121 |