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 |
||
| 29 | class OpenStack implements Simulator |
||
| 30 | { |
||
| 31 | use Cleanable; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * OpenStack identify url |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $authUrl; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * OpenStack region |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $region; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $username; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $password; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Object Store container name |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $containerName; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * OpenStack service name |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $serviceName; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Max stream upload size, files over this size have to be uploaded as Dynamic Large Objects |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | protected $maxStreamUploadSize = 5368709120; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Path where to copy the backup without leading or trailing slashes. |
||
| 80 | * |
||
| 81 | * @var Path |
||
| 82 | */ |
||
| 83 | protected $path; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Unix timestamp of generating path from placeholder. |
||
| 87 | * |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | protected $time; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Path where to copy the backup still containing possible date placeholders. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $pathRaw = ''; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var Container |
||
| 101 | */ |
||
| 102 | protected $container; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getPath(): string |
||
| 111 | |||
| 112 | /** |
||
| 113 | * (non-PHPDoc) |
||
| 114 | * |
||
| 115 | * @see \phpbu\App\Backup\Sync::setup() |
||
| 116 | * @param array $config |
||
| 117 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 118 | * @throws \phpbu\App\Exception |
||
| 119 | */ |
||
| 120 | 9 | public function setup(array $config) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Make sure all mandatory keys are present in given config. |
||
| 143 | * |
||
| 144 | * @param array $config |
||
| 145 | * @param array $keys |
||
| 146 | * @throws Exception |
||
| 147 | */ |
||
| 148 | 9 | protected function validateConfig(array $config, array $keys) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Execute the sync. |
||
| 159 | * |
||
| 160 | * @see \phpbu\App\Backup\Sync::sync() |
||
| 161 | * @param \phpbu\App\Backup\Target $target |
||
| 162 | * @param \phpbu\App\Result $result |
||
| 163 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 164 | * @throws \OpenStack\Common\Error\BadResponseError |
||
| 165 | */ |
||
| 166 | public function sync(Target $target, Result $result) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Simulate the sync execution. |
||
| 198 | * |
||
| 199 | * @param \phpbu\App\Backup\Target $target |
||
| 200 | * @param \phpbu\App\Result $result |
||
| 201 | */ |
||
| 202 | 1 | View Code Duplication | public function simulate(Target $target, Result $result) |
| 215 | |||
| 216 | /** |
||
| 217 | * Creates collector for OpenStack. |
||
| 218 | * |
||
| 219 | * @param \phpbu\App\Backup\Target $target |
||
| 220 | * @return \phpbu\App\Backup\Collector |
||
| 221 | */ |
||
| 222 | protected function createCollector(Target $target): Collector |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param \OpenStack\ObjectStore\v1\Service $service |
||
| 229 | * @param \phpbu\App\Result $result |
||
| 230 | * @return \OpenStack\ObjectStore\v1\Models\Container |
||
| 231 | * @throws \OpenStack\Common\Error\BadResponseError |
||
| 232 | */ |
||
| 233 | protected function getOrCreateContainer(ObjectStoreService $service, Result $result) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the upload path |
||
| 244 | * |
||
| 245 | * @param \phpbu\App\Backup\Target $target |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | 2 | public function getUploadPath(Target $target) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param \phpbu\App\Result $result |
||
| 255 | * @return void |
||
| 256 | * @throws \OpenStack\Common\Error\BadResponseError |
||
| 257 | */ |
||
| 258 | protected function connect(Result $result) |
||
| 277 | } |
||
| 278 |