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 |
||
| 23 | class AzureBlob implements Simulator |
||
| 24 | { |
||
| 25 | use Cleanable; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Azure Blob client. |
||
| 29 | * |
||
| 30 | * @var BlobRestProxy; |
||
| 31 | */ |
||
| 32 | private $client; |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Azure Blob Connection String |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $connectionString; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Azure Blob Container Name |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $containerName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Azure Blob remote path |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $path; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Azure Blob remote raw path |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $pathRaw; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Unix timestamp of generating path from placeholder. |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $time; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Configure the sync. |
||
| 72 | * |
||
| 73 | * @see \phpbu\App\Backup\Sync::setup() |
||
| 74 | * @param array $config |
||
| 75 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 76 | * @throws \phpbu\App\Exception |
||
| 77 | */ |
||
| 78 | 10 | public function setup(array $config) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Make sure all mandatory keys are present in given config. |
||
| 99 | * |
||
| 100 | * @param array $config |
||
| 101 | * @param array $keys |
||
| 102 | * @throws Exception |
||
| 103 | */ |
||
| 104 | 10 | protected function validateConfig(array $config, array $keys) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Execute the sync. |
||
| 115 | * |
||
| 116 | * @see \phpbu\App\Backup\Sync::sync() |
||
| 117 | * @param \phpbu\App\Backup\Target $target |
||
| 118 | * @param \phpbu\App\Result $result |
||
| 119 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 120 | */ |
||
| 121 | 3 | View Code Duplication | public function sync(Target $target, Result $result) |
| 140 | |||
| 141 | /** |
||
| 142 | * Create the Azure Blob client. |
||
| 143 | * |
||
| 144 | * @return \MicrosoftAzure\Storage\Blob\BlobRestProxy |
||
| 145 | */ |
||
| 146 | protected function createClient() : BlobRestProxy |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Creates collector for Azure Blob Storage |
||
| 153 | * |
||
| 154 | * @param \phpbu\App\Backup\Target $target |
||
| 155 | * @return \phpbu\App\Backup\Collector |
||
| 156 | */ |
||
| 157 | 1 | protected function createCollector(Target $target) : Collector |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Simulate the sync execution. |
||
| 165 | * |
||
| 166 | * @param \phpbu\App\Backup\Target $target |
||
| 167 | * @param \phpbu\App\Result $result |
||
| 168 | */ |
||
| 169 | 1 | View Code Duplication | public function simulate(Target $target, Result $result) |
| 179 | |||
| 180 | /** |
||
| 181 | * Check if an Azure Blob Storage Container exists |
||
| 182 | * |
||
| 183 | * @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $blobRestProxy |
||
| 184 | * @param string $containerName |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 3 | private function doesContainerExist(BlobRestProxy $blobRestProxy, string $containerName): bool |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Create an Azure Storage Container |
||
| 200 | * |
||
| 201 | * @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $blobRestProxy |
||
| 202 | */ |
||
| 203 | 3 | private function createContainer(BlobRestProxy $blobRestProxy) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Upload backup to Azure Blob Storage |
||
| 210 | * |
||
| 211 | * @param \phpbu\App\Backup\Target $target |
||
| 212 | * @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $blobRestProxy |
||
| 213 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 214 | * @throws \phpbu\App\Exception |
||
| 215 | */ |
||
| 216 | 3 | private function upload(Target $target, BlobRestProxy $blobRestProxy) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Open stream and validate it. |
||
| 228 | * |
||
| 229 | * @param string $path |
||
| 230 | * @param string $mode |
||
| 231 | * @return resource |
||
| 232 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 233 | */ |
||
| 234 | View Code Duplication | protected function getFileHandle($path, $mode) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Get the azure blob upload path |
||
| 245 | * |
||
| 246 | * @param \phpbu\App\Backup\Target $target |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | 5 | public function getUploadPath(Target $target) |
|
| 253 | } |
||
| 254 |