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 |
||
23 | class AzureBlobStorage implements Adapter, |
||
24 | MetadataSupporter, |
||
25 | SizeCalculator, |
||
26 | ChecksumCalculator |
||
27 | |||
28 | { |
||
29 | /** |
||
30 | * Error constants. |
||
31 | */ |
||
32 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
33 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
34 | |||
35 | /** |
||
36 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
37 | */ |
||
38 | protected $blobProxyFactory; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $containerName; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $detectContentType; |
||
49 | |||
50 | /** |
||
51 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
52 | */ |
||
53 | protected $blobProxy; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $multiContainerMode = false; |
||
59 | |||
60 | /** |
||
61 | * @var CreateContainerOptions |
||
62 | */ |
||
63 | protected $createContainerOptions; |
||
64 | |||
65 | /** |
||
66 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
67 | * @param string|null $containerName |
||
68 | * @param bool $create |
||
69 | * @param bool $detectContentType |
||
70 | * |
||
71 | * @throws \RuntimeException |
||
72 | */ |
||
73 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
84 | |||
85 | /** |
||
86 | * @return CreateContainerOptions |
||
87 | */ |
||
88 | public function getCreateContainerOptions() |
||
92 | |||
93 | /** |
||
94 | * @param CreateContainerOptions $options |
||
95 | */ |
||
96 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
100 | |||
101 | /** |
||
102 | * Creates a new container. |
||
103 | * |
||
104 | * @param string $containerName |
||
105 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
106 | * |
||
107 | * @throws \RuntimeException if cannot create the container |
||
108 | */ |
||
109 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
132 | |||
133 | /** |
||
134 | * Deletes a container. |
||
135 | * |
||
136 | * @param string $containerName |
||
137 | * @param DeleteContainerOptions $options |
||
138 | * |
||
139 | * @throws \RuntimeException if cannot delete the container |
||
140 | */ |
||
141 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | * @throws \RuntimeException |
||
164 | * @throws \InvalidArgumentException |
||
165 | */ |
||
166 | View Code Duplication | public function read($key) |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | * @throws \RuntimeException |
||
185 | * @throws \InvalidArgumentException |
||
186 | */ |
||
187 | public function write($key, $content) |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | * @throws \RuntimeException |
||
226 | * @throws \InvalidArgumentException |
||
227 | */ |
||
228 | public function exists($key) |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | * @throws \RuntimeException |
||
266 | */ |
||
267 | public function keys() |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | * @throws \RuntimeException |
||
300 | * @throws \InvalidArgumentException |
||
301 | */ |
||
302 | View Code Duplication | public function mtime($key) |
|
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | View Code Duplication | public function size($key) |
|
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | public function checksum($key) |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | * @throws \RuntimeException |
||
361 | * @throws \InvalidArgumentException |
||
362 | */ |
||
363 | View Code Duplication | public function delete($key) |
|
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | * @throws \RuntimeException |
||
382 | * @throws \InvalidArgumentException |
||
383 | */ |
||
384 | public function rename($sourceKey, $targetKey) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | public function isDirectory($key) |
||
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | * @throws \RuntimeException |
||
418 | * @throws \InvalidArgumentException |
||
419 | */ |
||
420 | View Code Duplication | public function setMetadata($key, $content) |
|
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | * @throws \RuntimeException |
||
443 | * @throws \InvalidArgumentException |
||
444 | */ |
||
445 | View Code Duplication | public function getMetadata($key) |
|
466 | |||
467 | /** |
||
468 | * Lazy initialization, automatically called when some method is called after construction. |
||
469 | */ |
||
470 | protected function init() |
||
476 | |||
477 | /** |
||
478 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
479 | * |
||
480 | * @param ServiceException $exception |
||
481 | * @param string $action |
||
482 | * @param string $containerName |
||
483 | * |
||
484 | * @throws \RuntimeException |
||
485 | */ |
||
486 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
498 | |||
499 | /** |
||
500 | * Extracts the error code from a service exception. |
||
501 | * |
||
502 | * @param ServiceException $exception |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
516 | |||
517 | /** |
||
518 | * @param string|resource $content |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | View Code Duplication | private function guessContentType($content) |
|
532 | |||
533 | /** |
||
534 | * @param string $key |
||
535 | * |
||
536 | * @return array |
||
537 | * @throws \InvalidArgumentException |
||
538 | */ |
||
539 | private function tokenizeKey($key) |
||
557 | |||
558 | /** |
||
559 | * @param string $containerName |
||
560 | * @param null $prefix |
||
561 | * |
||
562 | * @return array |
||
563 | */ |
||
564 | private function fetchBlobs($containerName, $prefix = null) |
||
578 | } |
||
579 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.