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 | * @var bool |
||
52 | */ |
||
53 | protected $multiContainerMode = false; |
||
54 | |||
55 | /** |
||
56 | * @var CreateContainerOptions |
||
57 | */ |
||
58 | protected $createContainerOptions; |
||
59 | |||
60 | /** |
||
61 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
62 | * @param string|null $containerName |
||
63 | * @param bool $create |
||
64 | * @param bool $detectContentType |
||
65 | * |
||
66 | * @throws \RuntimeException |
||
67 | */ |
||
68 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
79 | |||
80 | /** |
||
81 | * @return CreateContainerOptions |
||
82 | */ |
||
83 | public function getCreateContainerOptions() |
||
87 | |||
88 | /** |
||
89 | * @param CreateContainerOptions $options |
||
90 | */ |
||
91 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
95 | |||
96 | /** |
||
97 | * Creates a new container. |
||
98 | * |
||
99 | * @param string $containerName |
||
100 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
101 | * |
||
102 | * @throws \RuntimeException if cannot create the container |
||
103 | */ |
||
104 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
127 | |||
128 | /** |
||
129 | * Deletes a container. |
||
130 | * |
||
131 | * @param string $containerName |
||
132 | * @param DeleteContainerOptions $options |
||
133 | * |
||
134 | * @throws \RuntimeException if cannot delete the container |
||
135 | */ |
||
136 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | * @throws \RuntimeException |
||
159 | * @throws \InvalidArgumentException |
||
160 | */ |
||
161 | View Code Duplication | public function read($key) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | * @throws \RuntimeException |
||
180 | * @throws \InvalidArgumentException |
||
181 | */ |
||
182 | public function write($key, $content) |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | * @throws \RuntimeException |
||
214 | * @throws \InvalidArgumentException |
||
215 | */ |
||
216 | public function exists($key) |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | * @throws \RuntimeException |
||
254 | */ |
||
255 | public function keys() |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | * @throws \RuntimeException |
||
285 | * @throws \InvalidArgumentException |
||
286 | */ |
||
287 | View Code Duplication | public function mtime($key) |
|
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | * @throws \RuntimeException |
||
306 | * @throws \InvalidArgumentException |
||
307 | */ |
||
308 | View Code Duplication | public function delete($key) |
|
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | * @throws \RuntimeException |
||
327 | * @throws \InvalidArgumentException |
||
328 | */ |
||
329 | public function rename($sourceKey, $targetKey) |
||
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | public function isDirectory($key) |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | * @throws \RuntimeException |
||
361 | * @throws \InvalidArgumentException |
||
362 | */ |
||
363 | View Code Duplication | public function setMetadata($key, $content) |
|
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | * @throws \RuntimeException |
||
386 | * @throws \InvalidArgumentException |
||
387 | */ |
||
388 | View Code Duplication | public function getMetadata($key) |
|
409 | |||
410 | /** |
||
411 | * Lazy initialization, automatically called when some method is called after construction. |
||
412 | */ |
||
413 | protected function init() |
||
419 | |||
420 | /** |
||
421 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
422 | * |
||
423 | * @param ServiceException $exception |
||
424 | * @param string $action |
||
425 | * @param string $containerName |
||
426 | * |
||
427 | * @throws \RuntimeException |
||
428 | */ |
||
429 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
441 | |||
442 | /** |
||
443 | * Extracts the error code from a service exception. |
||
444 | * |
||
445 | * @param ServiceException $exception |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
459 | |||
460 | /** |
||
461 | * @param string|resource $content |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | View Code Duplication | private function guessContentType($content) |
|
475 | |||
476 | /** |
||
477 | * @param string $key |
||
478 | * |
||
479 | * @return array |
||
480 | * @throws \InvalidArgumentException |
||
481 | */ |
||
482 | private function tokenizeKey($key) |
||
495 | } |
||
496 |