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 |
||
25 | class Simulate extends Compression |
||
26 | { |
||
27 | /** |
||
28 | * Execute backups. |
||
29 | * |
||
30 | * @param \phpbu\App\Configuration $configuration |
||
31 | * @return \phpbu\App\Result |
||
32 | * @throws \Exception |
||
33 | */ |
||
34 | 5 | public function run(Configuration $configuration) : Result |
|
35 | { |
||
36 | 5 | $this->configuration = $configuration; |
|
37 | 5 | $this->result->phpbuStart($configuration); |
|
38 | |||
39 | // create backups |
||
40 | /** @var \phpbu\App\Configuration\Backup $backup */ |
||
41 | 5 | foreach ($configuration->getBackups() as $backup) { |
|
42 | // make sure the backup should be executed and is not excluded via the --limit option |
||
43 | 5 | View Code Duplication | if (!$configuration->isBackupActive($backup->getName())) { |
44 | 1 | $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL); |
|
45 | 1 | continue; |
|
46 | } |
||
47 | // setup target and collector |
||
48 | 5 | $target = $this->factory->createTarget($backup->getTarget()); |
|
49 | 5 | $collector = new Collector($target); |
|
50 | |||
51 | 5 | $this->simulateSource($backup, $target); |
|
52 | 5 | $this->simulateChecks($backup, $target, $collector); |
|
53 | 5 | $this->simulateCrypt($backup, $target); |
|
54 | 5 | $this->simulateSyncs($backup, $target); |
|
55 | 5 | $this->simulateCleanup($backup, $target, $collector); |
|
56 | } |
||
57 | 5 | $this->result->phpbuEnd(); |
|
58 | |||
59 | 5 | return $this->result; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Simulate the backup. |
||
64 | * |
||
65 | * @param \phpbu\App\Configuration\Backup $conf |
||
66 | * @param \phpbu\App\Backup\Target $target |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | 5 | View Code Duplication | protected function simulateSource(Configuration\Backup $conf, Target $target) |
70 | { |
||
71 | 5 | $this->result->backupStart($conf); |
|
72 | 5 | $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options); |
|
73 | |||
74 | 5 | if ($source instanceof Source\Simulator) { |
|
75 | 5 | $status = $source->simulate($target, $this->result); |
|
76 | 5 | $this->compress($status, $target, $this->result); |
|
77 | } |
||
78 | 5 | $this->result->backupEnd($conf); |
|
79 | 5 | } |
|
80 | |||
81 | /** |
||
82 | * Simulate checks. |
||
83 | * |
||
84 | * @param \phpbu\App\Configuration\Backup $backup |
||
85 | * @param \phpbu\App\Backup\Target $target |
||
86 | * @param \phpbu\App\Backup\Collector $collector |
||
87 | * @throws \Exception |
||
88 | */ |
||
89 | 5 | View Code Duplication | protected function simulateChecks(Configuration\Backup $backup, Target $target, Collector $collector) |
90 | { |
||
91 | 5 | foreach ($backup->getChecks() as $config) { |
|
92 | 5 | $this->result->checkStart($config); |
|
93 | 5 | $check = $this->factory->createCheck($config->type); |
|
94 | 5 | if ($check instanceof Check\Simulator) { |
|
95 | 5 | $check->simulate($target, $config->value, $collector, $this->result); |
|
96 | } |
||
97 | 5 | $this->result->checkEnd($config); |
|
98 | } |
||
99 | 5 | } |
|
100 | |||
101 | /** |
||
102 | * Simulate encryption. |
||
103 | * |
||
104 | * @param \phpbu\App\Configuration\Backup $backup |
||
105 | * @param \phpbu\App\Backup\Target $target |
||
106 | * @throws \phpbu\App\Exception |
||
107 | */ |
||
108 | 5 | View Code Duplication | protected function simulateCrypt(Configuration\Backup $backup, Target $target) |
109 | { |
||
110 | 5 | if ($backup->hasCrypt()) { |
|
111 | 5 | $crypt = $backup->getCrypt(); |
|
112 | 5 | $this->result->cryptStart($crypt); |
|
113 | 5 | $crypter = $this->factory->createCrypter($crypt->type, $crypt->options); |
|
114 | 5 | if ($crypter instanceof Crypter\Simulator) { |
|
115 | 5 | $crypter->simulate($target, $this->result); |
|
116 | } |
||
117 | 5 | $target->setCrypter($crypter); |
|
118 | 5 | $this->result->cryptEnd($crypt); |
|
119 | } |
||
120 | 5 | } |
|
121 | |||
122 | /** |
||
123 | * Simulate all syncs. |
||
124 | * |
||
125 | * @param \phpbu\App\Configuration\Backup $backup |
||
126 | * @param \phpbu\App\Backup\Target $target |
||
127 | * @throws \Exception |
||
128 | */ |
||
129 | 5 | protected function simulateSyncs(Configuration\Backup $backup, Target $target) |
|
139 | |||
140 | /** |
||
141 | * Simulate the cleanup. |
||
142 | * |
||
143 | * @param \phpbu\App\Configuration\Backup $backup |
||
144 | * @param \phpbu\App\Backup\Target $target |
||
145 | * @param \phpbu\App\Backup\Collector $collector |
||
146 | * @throws \phpbu\App\Exception |
||
147 | */ |
||
148 | 5 | View Code Duplication | protected function simulateCleanup(Configuration\Backup $backup, Target $target, Collector $collector) |
161 | |||
162 | /** |
||
163 | * Execute the compressor. |
||
164 | * Returns the path to the created archive file. |
||
165 | * |
||
166 | * @param \phpbu\App\Backup\Compressor\Executable $compressor |
||
167 | * @param \phpbu\App\Backup\Target $target |
||
168 | * @param \phpbu\App\Result $result |
||
169 | * @return string |
||
170 | */ |
||
171 | 3 | protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string |
|
176 | } |
||
177 |