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 | SizeCalculator |
||
25 | |||
26 | { |
||
27 | /** |
||
28 | * Error constants. |
||
29 | */ |
||
30 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
31 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
32 | |||
33 | /** |
||
34 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
35 | */ |
||
36 | protected $blobProxyFactory; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $containerName; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $detectContentType; |
||
47 | |||
48 | /** |
||
49 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
50 | */ |
||
51 | protected $blobProxy; |
||
52 | |||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $multiContainerMode = false; |
||
57 | |||
58 | /** |
||
59 | * @var CreateContainerOptions |
||
60 | */ |
||
61 | protected $createContainerOptions; |
||
62 | |||
63 | /** |
||
64 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
65 | * @param string|null $containerName |
||
66 | * @param bool $create |
||
67 | * @param bool $detectContentType |
||
68 | * |
||
69 | * @throws \RuntimeException |
||
70 | */ |
||
71 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
82 | |||
83 | /** |
||
84 | * @return CreateContainerOptions |
||
85 | */ |
||
86 | public function getCreateContainerOptions() |
||
90 | |||
91 | /** |
||
92 | * @param CreateContainerOptions $options |
||
93 | */ |
||
94 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
98 | |||
99 | /** |
||
100 | * Creates a new container. |
||
101 | * |
||
102 | * @param string $containerName |
||
103 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
104 | * |
||
105 | * @throws \RuntimeException if cannot create the container |
||
106 | */ |
||
107 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
130 | |||
131 | /** |
||
132 | * Deletes a container. |
||
133 | * |
||
134 | * @param string $containerName |
||
135 | * @param DeleteContainerOptions $options |
||
136 | * |
||
137 | * @throws \RuntimeException if cannot delete the container |
||
138 | */ |
||
139 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * @throws \RuntimeException |
||
162 | * @throws \InvalidArgumentException |
||
163 | */ |
||
164 | View Code Duplication | public function read($key) |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | * @throws \RuntimeException |
||
183 | * @throws \InvalidArgumentException |
||
184 | */ |
||
185 | public function write($key, $content) |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | * @throws \RuntimeException |
||
219 | * @throws \InvalidArgumentException |
||
220 | */ |
||
221 | public function exists($key) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | * @throws \RuntimeException |
||
259 | */ |
||
260 | public function keys() |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | * @throws \RuntimeException |
||
293 | * @throws \InvalidArgumentException |
||
294 | */ |
||
295 | View Code Duplication | public function mtime($key) |
|
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function size($key) |
||
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | * @throws \RuntimeException |
||
334 | * @throws \InvalidArgumentException |
||
335 | */ |
||
336 | View Code Duplication | public function delete($key) |
|
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | * @throws \RuntimeException |
||
355 | * @throws \InvalidArgumentException |
||
356 | */ |
||
357 | public function rename($sourceKey, $targetKey) |
||
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | public function isDirectory($key) |
||
387 | |||
388 | /** |
||
389 | * {@inheritdoc} |
||
390 | * @throws \RuntimeException |
||
391 | * @throws \InvalidArgumentException |
||
392 | */ |
||
393 | View Code Duplication | public function setMetadata($key, $content) |
|
412 | |||
413 | /** |
||
414 | * {@inheritdoc} |
||
415 | * @throws \RuntimeException |
||
416 | * @throws \InvalidArgumentException |
||
417 | */ |
||
418 | View Code Duplication | public function getMetadata($key) |
|
439 | |||
440 | /** |
||
441 | * Lazy initialization, automatically called when some method is called after construction. |
||
442 | */ |
||
443 | protected function init() |
||
449 | |||
450 | /** |
||
451 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
452 | * |
||
453 | * @param ServiceException $exception |
||
454 | * @param string $action |
||
455 | * @param string $containerName |
||
456 | * |
||
457 | * @throws \RuntimeException |
||
458 | */ |
||
459 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
471 | |||
472 | /** |
||
473 | * Extracts the error code from a service exception. |
||
474 | * |
||
475 | * @param ServiceException $exception |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
489 | |||
490 | /** |
||
491 | * @param string|resource $content |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | View Code Duplication | private function guessContentType($content) |
|
505 | |||
506 | /** |
||
507 | * @param string $key |
||
508 | * |
||
509 | * @return array |
||
510 | * @throws \InvalidArgumentException |
||
511 | */ |
||
512 | private function tokenizeKey($key) |
||
530 | |||
531 | /** |
||
532 | * @param string $containerName |
||
533 | * @param null $prefix |
||
534 | * |
||
535 | * @return array |
||
536 | */ |
||
537 | private function fetchBlobs($containerName, $prefix = null) |
||
551 | } |
||
552 |