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) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function exists($key) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | public function keys() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function mtime($key) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function delete($key) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function rename($sourceKey, $targetKey) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | public function isDirectory($key) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | View Code Duplication | public function setMetadata($key, $content) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function getMetadata($key) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Lazy initialization, automatically called when some method is called after construction. |
||
| 344 | */ |
||
| 345 | protected function init() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
| 354 | * |
||
| 355 | * @param ServiceException $exception |
||
| 356 | * @param string $action |
||
| 357 | * |
||
| 358 | * @throws \RuntimeException |
||
| 359 | */ |
||
| 360 | protected function failIfContainerNotFound(ServiceException $exception, $action) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Extracts the error code from a service exception. |
||
| 375 | * |
||
| 376 | * @param ServiceException $exception |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
| 390 | } |
||
| 391 |