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 |
||
| 21 | class AzureBlobStorage implements Adapter, |
||
|
|
|||
| 22 | MetadataSupporter |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Error constants. |
||
| 26 | */ |
||
| 27 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
| 28 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
| 32 | */ |
||
| 33 | protected $blobProxyFactory; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $containerName; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $detectContentType; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
| 47 | */ |
||
| 48 | protected $blobProxy; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
| 52 | * @param string $containerName |
||
| 53 | * @param bool $create |
||
| 54 | * @param bool $detectContentType |
||
| 55 | */ |
||
| 56 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName, $create = false, $detectContentType = true) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a new container. |
||
| 68 | * |
||
| 69 | * @param string $containerName |
||
| 70 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
| 71 | * |
||
| 72 | * @throws \RuntimeException if cannot create the container |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Deletes a container. |
||
| 96 | * |
||
| 97 | * @param string $containerName |
||
| 98 | * @param DeleteContainerOptions $options |
||
| 99 | * |
||
| 100 | * @throws \RuntimeException if cannot delete the container |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function read($key) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function write($key, $content) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | public function exists($key) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function keys() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function mtime($key) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | View Code Duplication | public function delete($key) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function rename($sourceKey, $targetKey) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | */ |
||
| 291 | public function isDirectory($key) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | View Code Duplication | public function setMetadata($key, $content) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * {@inheritdoc} |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function getMetadata($key) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Lazy initialization, automatically called when some method is called after construction. |
||
| 345 | */ |
||
| 346 | protected function init() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
| 355 | * |
||
| 356 | * @param ServiceException $exception |
||
| 357 | * @param string $action |
||
| 358 | * |
||
| 359 | * @throws \RuntimeException |
||
| 360 | */ |
||
| 361 | protected function failIfContainerNotFound(ServiceException $exception, $action) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Extracts the error code from a service exception. |
||
| 376 | * |
||
| 377 | * @param ServiceException $exception |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $content |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | View Code Duplication | private function guessContentType($content) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Error message to be parsed. |
||
| 414 | * |
||
| 415 | * @param ResponseInterface $response The response with a response body. |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | protected static function parseErrorCode(ResponseInterface $response) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Converts a SimpleXML object to an Array recursively |
||
| 441 | * ensuring all sub-elements are arrays as well. |
||
| 442 | * |
||
| 443 | * @param string $sxml The SimpleXML object. |
||
| 444 | * @param array $arr The array into which to store results. |
||
| 445 | * |
||
| 446 | * @return array |
||
| 447 | */ |
||
| 448 | private static function _sxml2arr($sxml, array $arr = null) |
||
| 460 | } |
||
| 461 |