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 | */ |
||
160 | View Code Duplication | public function read($key) |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | * @throws \RuntimeException |
||
179 | */ |
||
180 | public function write($key, $content) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | * @throws \RuntimeException |
||
213 | */ |
||
214 | public function exists($key) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | * @throws \RuntimeException |
||
249 | */ |
||
250 | public function keys() |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | * @throws \RuntimeException |
||
280 | */ |
||
281 | View Code Duplication | public function mtime($key) |
|
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | * @throws \RuntimeException |
||
300 | */ |
||
301 | View Code Duplication | public function delete($key) |
|
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | * @throws \RuntimeException |
||
320 | */ |
||
321 | public function rename($sourceKey, $targetKey) |
||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function isDirectory($key) |
||
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | * @throws \RuntimeException |
||
352 | */ |
||
353 | View Code Duplication | public function setMetadata($key, $content) |
|
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | * @throws \RuntimeException |
||
376 | */ |
||
377 | View Code Duplication | public function getMetadata($key) |
|
398 | |||
399 | /** |
||
400 | * Lazy initialization, automatically called when some method is called after construction. |
||
401 | */ |
||
402 | protected function init() |
||
408 | |||
409 | /** |
||
410 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
411 | * |
||
412 | * @param ServiceException $exception |
||
413 | * @param string $action |
||
414 | * |
||
415 | * @throws \RuntimeException |
||
416 | */ |
||
417 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
429 | |||
430 | /** |
||
431 | * Extracts the error code from a service exception. |
||
432 | * |
||
433 | * @param ServiceException $exception |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
447 | |||
448 | /** |
||
449 | * @param string|resource $content |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | View Code Duplication | private function guessContentType($content) |
|
463 | |||
464 | /** |
||
465 | * @param string $key |
||
466 | * |
||
467 | * @return array |
||
468 | * @throws \InvalidArgumentException |
||
469 | */ |
||
470 | private function tokenizeKey($key) |
||
482 | } |
||
483 |