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 |
||
| 20 | class AzureBlobStorage implements Adapter, |
||
|
|
|||
| 21 | MetadataSupporter |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Error constants. |
||
| 25 | */ |
||
| 26 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
| 27 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
| 31 | */ |
||
| 32 | protected $blobProxyFactory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $containerName; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $detectContentType; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \WindowsAzure\Blob\Internal\IBlob |
||
| 46 | */ |
||
| 47 | protected $blobProxy; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
| 51 | * @param string $containerName |
||
| 52 | * @param bool $create |
||
| 53 | * @param bool $detectContentType |
||
| 54 | */ |
||
| 55 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName, $create = false, $detectContentType = true) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Creates a new container. |
||
| 67 | * |
||
| 68 | * @param string $containerName |
||
| 69 | * @param \WindowsAzure\Blob\Models\CreateContainerOptions $options |
||
| 70 | * |
||
| 71 | * @throws \RuntimeException if cannot create the container |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Deletes a container. |
||
| 95 | * |
||
| 96 | * @param string $containerName |
||
| 97 | * @param DeleteContainerOptions $options |
||
| 98 | * |
||
| 99 | * @throws \RuntimeException if cannot delete the container |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function read($key) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function write($key, $content) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | public function exists($key) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function keys() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function mtime($key) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function delete($key) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function rename($sourceKey, $targetKey) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | public function isDirectory($key) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | View Code Duplication | public function setMetadata($key, $content) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function getMetadata($key) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Lazy initialization, automatically called when some method is called after construction. |
||
| 348 | */ |
||
| 349 | protected function init() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
| 358 | * |
||
| 359 | * @param ServiceException $exception |
||
| 360 | * @param string $action |
||
| 361 | * |
||
| 362 | * @throws \RuntimeException |
||
| 363 | */ |
||
| 364 | protected function failIfContainerNotFound(ServiceException $exception, $action) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Extracts the error code from a service exception. |
||
| 379 | * |
||
| 380 | * @param ServiceException $exception |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
| 394 | } |
||
| 395 |