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 |
||
| 26 | class Backup extends Compression |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Backup failed |
||
| 30 | * |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $failure; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Execute backups. |
||
| 37 | * |
||
| 38 | * @param \phpbu\App\Configuration $configuration |
||
| 39 | * @return \phpbu\App\Result |
||
| 40 | * @throws \phpbu\App\Exception |
||
| 41 | */ |
||
| 42 | 8 | public function run(Configuration $configuration) : Result |
|
| 43 | { |
||
| 44 | 8 | $this->configuration = $configuration; |
|
| 45 | 8 | $stop = false; |
|
| 46 | |||
| 47 | 8 | $this->result->phpbuStart($configuration); |
|
| 48 | |||
| 49 | // create backups |
||
| 50 | /** @var \phpbu\App\Configuration\Backup $backup */ |
||
| 51 | 8 | foreach ($configuration->getBackups() as $backup) { |
|
| 52 | 8 | if ($stop) { |
|
| 53 | 2 | break; |
|
| 54 | } |
||
| 55 | // make sure the backup should be executed and is not excluded via the --limit option |
||
| 56 | 8 | View Code Duplication | if (!$configuration->isBackupActive($backup->getName())) { |
| 57 | $this->result->debug('skipping backup: ' . $backup->getName() . PHP_EOL); |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | // setup target and collector, reset failure state |
||
| 61 | 8 | $target = $this->factory->createTarget($backup->getTarget()); |
|
| 62 | 8 | $collector = new Local($target); |
|
| 63 | 8 | $this->failure = false; |
|
| 64 | |||
| 65 | try { |
||
| 66 | /* ___ ___ _______ ____ _____ |
||
| 67 | * / _ )/ _ |/ ___/ //_/ / / / _ \ |
||
| 68 | * / _ / __ / /__/ ,< / /_/ / ___/ |
||
| 69 | * /____/_/ |_\___/_/|_|\____/_/ |
||
| 70 | */ |
||
| 71 | 8 | $this->executeSource($backup, $target); |
|
| 72 | |||
| 73 | /* _______ _____________ ______ |
||
| 74 | * / ___/ // / __/ ___/ //_/ __/ |
||
| 75 | * / /__/ _ / _// /__/ ,< _\ \ |
||
| 76 | * \___/_//_/___/\___/_/|_/___/ |
||
| 77 | */ |
||
| 78 | 7 | $this->executeChecks($backup, $target, $collector); |
|
| 79 | |||
| 80 | /* __________ _____ ______ |
||
| 81 | * / ___/ _ \ \/ / _ \/_ __/ |
||
| 82 | * / /__/ , _/\ / ___/ / / |
||
| 83 | * \___/_/|_| /_/_/ /_/ |
||
| 84 | */ |
||
| 85 | 7 | $this->executeCrypt($backup, $target); |
|
| 86 | |||
| 87 | /* ______ ___ ___________ |
||
| 88 | * / __/\ \/ / |/ / ___/ __/ |
||
| 89 | * _\ \ \ / / /___\ \ |
||
| 90 | * /___/ /_/_/|_/\___/___/ |
||
| 91 | */ |
||
| 92 | 7 | $this->executeSyncs($backup, $target); |
|
| 93 | |||
| 94 | /* _______ _______ _ ____ _____ |
||
| 95 | * / ___/ / / __/ _ | / |/ / / / / _ \ |
||
| 96 | * / /__/ /__/ _// __ |/ / /_/ / ___/ |
||
| 97 | * \___/____/___/_/ |_/_/|_/\____/_/ |
||
| 98 | */ |
||
| 99 | 7 | $this->executeCleanup($backup, $target, $collector); |
|
| 100 | 3 | } catch (\Exception $e) { |
|
| 101 | 3 | $this->result->debug('exception: ' . $e->getMessage()); |
|
| 102 | 3 | $this->result->addError($e); |
|
| 103 | 3 | $this->result->backupFailed($backup); |
|
| 104 | 3 | if ($backup->stopOnFailure()) { |
|
| 105 | 8 | $stop = true; |
|
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | 8 | $this->result->phpbuEnd(); |
|
| 110 | |||
| 111 | 8 | return $this->result; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Execute the backup. |
||
| 116 | * |
||
| 117 | * @param \phpbu\App\Configuration\Backup $conf |
||
| 118 | * @param \phpbu\App\Backup\Target $target |
||
| 119 | * @throws \Exception |
||
| 120 | */ |
||
| 121 | 8 | View Code Duplication | protected function executeSource(Configuration\Backup $conf, Target $target) |
| 129 | |||
| 130 | /** |
||
| 131 | * Execute checks. |
||
| 132 | * |
||
| 133 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 134 | * @param \phpbu\App\Backup\Target $target |
||
| 135 | * @param \phpbu\App\Backup\Collector\Local $collector |
||
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | 7 | protected function executeChecks(Configuration\Backup $backup, Target $target, Local $collector) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Execute encryption. |
||
| 160 | * |
||
| 161 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 162 | * @param \phpbu\App\Backup\Target $target |
||
| 163 | * @throws \phpbu\App\Exception |
||
| 164 | */ |
||
| 165 | 7 | protected function executeCrypt(Configuration\Backup $backup, Target $target) |
|
| 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 | protected function executeSyncs(Configuration\Backup $backup, Target $target) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Execute the cleanup. |
||
| 217 | * |
||
| 218 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 219 | * @param \phpbu\App\Backup\Target $target |
||
| 220 | * @param \phpbu\App\Backup\Collector\Local $collector |
||
| 221 | * @throws \phpbu\App\Exception |
||
| 222 | */ |
||
| 223 | 7 | protected function executeCleanup(Configuration\Backup $backup, Target $target, Local $collector) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Execute the compressor. |
||
| 259 | * Returns the path to the created archive file. |
||
| 260 | * |
||
| 261 | * @param \phpbu\App\Backup\Compressor\Compressible $compressor |
||
| 262 | * @param \phpbu\App\Backup\Target $target |
||
| 263 | * @param \phpbu\App\Result $result |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | protected function executeCompressor(Compressor\Compressible $compressor, Target $target, Result $result) : string |
||
| 270 | } |
||
| 271 |