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 |
||
| 24 | class Backup extends Compression |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Backup failed |
||
| 28 | * |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | protected $failure; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Execute backups. |
||
| 35 | * |
||
| 36 | * @param \phpbu\App\Configuration $configuration |
||
| 37 | * @return \phpbu\App\Result |
||
| 38 | * @throws \phpbu\App\Exception |
||
| 39 | */ |
||
| 40 | 8 | public function run(Configuration $configuration) : Result |
|
| 41 | { |
||
| 42 | 8 | $this->configuration = $configuration; |
|
| 43 | 8 | $stop = false; |
|
| 44 | |||
| 45 | 8 | $this->result->phpbuStart($configuration); |
|
| 46 | |||
| 47 | // create backups |
||
| 48 | /** @var \phpbu\App\Configuration\Backup $backup */ |
||
| 49 | 8 | foreach ($configuration->getBackups() as $backup) { |
|
| 50 | 8 | if ($stop) { |
|
| 51 | 1 | break; |
|
| 52 | } |
||
| 53 | // make sure the backup should be executed and is not excluded via the --limit option |
||
| 54 | 8 | View Code Duplication | if (!$configuration->isBackupActive($backup->getName())) { |
| 55 | 1 | $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL); |
|
| 56 | 1 | continue; |
|
| 57 | } |
||
| 58 | // setup target and collector, reset failure state |
||
| 59 | 8 | $target = $this->factory->createTarget($backup->getTarget()); |
|
| 60 | 8 | $collector = new Collector($target); |
|
| 61 | 8 | $this->failure = false; |
|
| 62 | |||
| 63 | try { |
||
| 64 | /* ___ ___ _______ ____ _____ |
||
| 65 | * / _ )/ _ |/ ___/ //_/ / / / _ \ |
||
| 66 | * / _ / __ / /__/ ,< / /_/ / ___/ |
||
| 67 | * /____/_/ |_\___/_/|_|\____/_/ |
||
| 68 | */ |
||
| 69 | 8 | $this->executeSource($backup, $target); |
|
| 70 | |||
| 71 | /* _______ _____________ ______ |
||
| 72 | * / ___/ // / __/ ___/ //_/ __/ |
||
| 73 | * / /__/ _ / _// /__/ ,< _\ \ |
||
| 74 | * \___/_//_/___/\___/_/|_/___/ |
||
| 75 | */ |
||
| 76 | 7 | $this->executeChecks($backup, $target, $collector); |
|
| 77 | |||
| 78 | /* __________ _____ ______ |
||
| 79 | * / ___/ _ \ \/ / _ \/_ __/ |
||
| 80 | * / /__/ , _/\ / ___/ / / |
||
| 81 | * \___/_/|_| /_/_/ /_/ |
||
| 82 | */ |
||
| 83 | 7 | $this->executeCrypt($backup, $target); |
|
| 84 | |||
| 85 | /* ______ ___ ___________ |
||
| 86 | * / __/\ \/ / |/ / ___/ __/ |
||
| 87 | * _\ \ \ / / /___\ \ |
||
| 88 | * /___/ /_/_/|_/\___/___/ |
||
| 89 | */ |
||
| 90 | 7 | $this->executeSyncs($backup, $target); |
|
| 91 | |||
| 92 | /* _______ _______ _ ____ _____ |
||
| 93 | * / ___/ / / __/ _ | / |/ / / / / _ \ |
||
| 94 | * / /__/ /__/ _// __ |/ / /_/ / ___/ |
||
| 95 | * \___/____/___/_/ |_/_/|_/\____/_/ |
||
| 96 | */ |
||
| 97 | 7 | $this->executeCleanup($backup, $target, $collector); |
|
| 98 | |||
| 99 | 1 | } catch (\Exception $e) { |
|
| 100 | 1 | $this->result->debug('exception: ' . $e->getMessage()); |
|
| 101 | 1 | $this->result->addError($e); |
|
| 102 | 1 | $this->result->backupFailed($backup); |
|
| 103 | 1 | if ($backup->stopOnFailure()) { |
|
| 104 | 8 | $stop = true; |
|
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | 8 | $this->result->phpbuEnd(); |
|
| 109 | |||
| 110 | 8 | return $this->result; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Execute the backup. |
||
| 115 | * |
||
| 116 | * @param \phpbu\App\Configuration\Backup $conf |
||
| 117 | * @param \phpbu\App\Backup\Target $target |
||
| 118 | * @throws \Exception |
||
| 119 | */ |
||
| 120 | 8 | View Code Duplication | protected function executeSource(Configuration\Backup $conf, Target $target) |
| 121 | { |
||
| 122 | 8 | $this->result->backupStart($conf); |
|
| 123 | 8 | $source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options); |
|
| 124 | 8 | $status = $source->backup($target, $this->result); |
|
| 125 | 7 | $this->compress($status, $target, $this->result); |
|
| 126 | 7 | $this->result->backupEnd($conf); |
|
| 127 | 7 | } |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Execute checks. |
||
| 131 | * |
||
| 132 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 133 | * @param \phpbu\App\Backup\Target $target |
||
| 134 | * @param \phpbu\App\Backup\Collector $collector |
||
| 135 | * @throws \Exception |
||
| 136 | */ |
||
| 137 | 7 | protected function executeChecks(Configuration\Backup $backup, Target $target, Collector $collector) |
|
| 138 | { |
||
| 139 | 7 | foreach ($backup->getChecks() as $config) { |
|
| 140 | try { |
||
| 141 | 7 | $this->result->checkStart($config); |
|
| 142 | 7 | $check = $this->factory->createCheck($config->type); |
|
| 143 | 7 | if ($check->pass($target, $config->value, $collector, $this->result)) { |
|
| 144 | 5 | $this->result->checkEnd($config); |
|
| 145 | } else { |
||
| 146 | 1 | $this->failure = true; |
|
| 147 | 6 | $this->result->checkFailed($config); |
|
| 148 | } |
||
| 149 | 1 | } catch (Exception $e) { |
|
| 150 | 1 | $this->failure = true; |
|
| 151 | 1 | $this->result->addError($e); |
|
| 152 | 7 | $this->result->checkFailed($config); |
|
| 153 | } |
||
| 154 | } |
||
| 155 | 7 | } |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Execute encryption. |
||
| 159 | * |
||
| 160 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 161 | * @param \phpbu\App\Backup\Target $target |
||
| 162 | * @throws \phpbu\App\Exception |
||
| 163 | */ |
||
| 164 | 7 | View Code Duplication | protected function executeCrypt(Configuration\Backup $backup, Target $target) |
| 165 | { |
||
| 166 | 7 | if ($backup->hasCrypt()) { |
|
| 167 | 7 | $config = $backup->getCrypt(); |
|
| 168 | try { |
||
| 169 | 7 | $this->result->cryptStart($config); |
|
| 170 | 7 | if ($this->failure && $config->skipOnFailure) { |
|
| 171 | 2 | $this->result->cryptSkipped($config); |
|
| 172 | 2 | return; |
|
| 173 | } |
||
| 174 | 5 | $crypter = $this->factory->createCrypter($config->type, $config->options); |
|
| 175 | 5 | $crypter->crypt($target, $this->result); |
|
| 176 | 4 | $target->setCrypter($crypter); |
|
| 177 | 4 | $this->result->cryptEnd($config); |
|
| 178 | |||
| 179 | 1 | } catch (Crypter\Exception $e) { |
|
| 180 | 1 | $this->failure = true; |
|
| 181 | 1 | $this->result->addError($e); |
|
| 182 | 1 | $this->result->cryptFailed($config); |
|
| 183 | } |
||
| 184 | } |
||
| 185 | 5 | } |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Execute all syncs. |
||
| 189 | * |
||
| 190 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 191 | * @param \phpbu\App\Backup\Target $target |
||
| 192 | * @throws \Exception |
||
| 193 | */ |
||
| 194 | 7 | View Code Duplication | protected function executeSyncs(Configuration\Backup $backup, Target $target) |
| 215 | |||
| 216 | /** |
||
| 217 | * Execute the cleanup. |
||
| 218 | * |
||
| 219 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 220 | * @param \phpbu\App\Backup\Target $target |
||
| 221 | * @param \phpbu\App\Backup\Collector $collector |
||
| 222 | * @throws \phpbu\App\Exception |
||
| 223 | */ |
||
| 224 | 7 | View Code Duplication | protected function executeCleanup(Configuration\Backup $backup, Target $target, Collector $collector) |
| 246 | |||
| 247 | /** |
||
| 248 | * Execute the compressor. |
||
| 249 | * Returns the path to the created archive file. |
||
| 250 | * |
||
| 251 | * @param \phpbu\App\Backup\Compressor\Executable $compressor |
||
| 252 | * @param \phpbu\App\Backup\Target $target |
||
| 253 | * @param \phpbu\App\Result $result |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | protected function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) : string |
||
| 260 | } |
||
| 261 |