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:
Complex classes like AzureBlobStorage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AzureBlobStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class AzureBlobStorage implements Adapter, |
||
|
|
|||
| 23 | MetadataSupporter |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Error constants. |
||
| 27 | */ |
||
| 28 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
| 29 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
| 33 | */ |
||
| 34 | protected $blobProxyFactory; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $containerName; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $detectContentType; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
| 48 | */ |
||
| 49 | protected $blobProxy; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $multiContainerMode = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var CreateContainerOptions |
||
| 58 | */ |
||
| 59 | protected $createContainerOptions; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
| 63 | * @param string|null $containerName |
||
| 64 | * @param bool $create |
||
| 65 | * @param bool $detectContentType |
||
| 66 | * |
||
| 67 | * @throws \RuntimeException |
||
| 68 | */ |
||
| 69 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return CreateContainerOptions |
||
| 83 | */ |
||
| 84 | public function getCreateContainerOptions() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param CreateContainerOptions $options |
||
| 91 | */ |
||
| 92 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Creates a new container. |
||
| 99 | * |
||
| 100 | * @param string $containerName |
||
| 101 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
| 102 | * |
||
| 103 | * @throws \RuntimeException if cannot create the container |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Deletes a container. |
||
| 131 | * |
||
| 132 | * @param string $containerName |
||
| 133 | * @param DeleteContainerOptions $options |
||
| 134 | * |
||
| 135 | * @throws \RuntimeException if cannot delete the container |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | * @throws \RuntimeException |
||
| 160 | * @throws \InvalidArgumentException |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function read($key) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | * @throws \RuntimeException |
||
| 181 | * @throws \InvalidArgumentException |
||
| 182 | */ |
||
| 183 | public function write($key, $content) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | * @throws \RuntimeException |
||
| 215 | * @throws \InvalidArgumentException |
||
| 216 | */ |
||
| 217 | public function exists($key) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | * @throws \RuntimeException |
||
| 255 | */ |
||
| 256 | public function keys() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | * @throws \RuntimeException |
||
| 294 | * @throws \InvalidArgumentException |
||
| 295 | */ |
||
| 296 | View Code Duplication | public function mtime($key) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | * @throws \RuntimeException |
||
| 315 | * @throws \InvalidArgumentException |
||
| 316 | */ |
||
| 317 | View Code Duplication | public function delete($key) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | * @throws \RuntimeException |
||
| 336 | * @throws \InvalidArgumentException |
||
| 337 | */ |
||
| 338 | public function rename($sourceKey, $targetKey) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | public function isDirectory($key) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | * @throws \RuntimeException |
||
| 370 | * @throws \InvalidArgumentException |
||
| 371 | */ |
||
| 372 | View Code Duplication | public function setMetadata($key, $content) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | * @throws \RuntimeException |
||
| 395 | * @throws \InvalidArgumentException |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function getMetadata($key) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Lazy initialization, automatically called when some method is called after construction. |
||
| 421 | */ |
||
| 422 | protected function init() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
| 431 | * |
||
| 432 | * @param ServiceException $exception |
||
| 433 | * @param string $action |
||
| 434 | * @param string $containerName |
||
| 435 | * |
||
| 436 | * @throws \RuntimeException |
||
| 437 | */ |
||
| 438 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Extracts the error code from a service exception. |
||
| 453 | * |
||
| 454 | * @param ServiceException $exception |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string|resource $content |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | View Code Duplication | private function guessContentType($content) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $key |
||
| 487 | * |
||
| 488 | * @return array |
||
| 489 | * @throws \InvalidArgumentException |
||
| 490 | */ |
||
| 491 | private function tokenizeKey($key) |
||
| 506 | } |
||
| 507 |