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 | { |
||
25 | /** |
||
26 | * Error constants. |
||
27 | */ |
||
28 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
29 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
30 | |||
31 | /** |
||
32 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
33 | */ |
||
34 | protected $blobProxyFactory; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $containerName; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $detectContentType; |
||
45 | |||
46 | /** |
||
47 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
48 | */ |
||
49 | protected $blobProxy; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $multiContainerMode = false; |
||
55 | |||
56 | /** |
||
57 | * @var CreateContainerOptions |
||
58 | */ |
||
59 | protected $createContainerOptions; |
||
60 | |||
61 | /** |
||
62 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
63 | * @param string|null $containerName |
||
64 | * @param bool $create |
||
65 | * @param bool $detectContentType |
||
66 | * |
||
67 | * @throws \RuntimeException |
||
68 | */ |
||
69 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
80 | |||
81 | /** |
||
82 | * @return CreateContainerOptions |
||
83 | */ |
||
84 | public function getCreateContainerOptions() |
||
88 | |||
89 | /** |
||
90 | * @param CreateContainerOptions $options |
||
91 | */ |
||
92 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
96 | |||
97 | /** |
||
98 | * Creates a new container. |
||
99 | * |
||
100 | * @param string $containerName |
||
101 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
102 | * |
||
103 | * @throws \RuntimeException if cannot create the container |
||
104 | */ |
||
105 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
128 | |||
129 | /** |
||
130 | * Deletes a container. |
||
131 | * |
||
132 | * @param string $containerName |
||
133 | * @param DeleteContainerOptions $options |
||
134 | * |
||
135 | * @throws \RuntimeException if cannot delete the container |
||
136 | */ |
||
137 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | * @throws \RuntimeException |
||
160 | * @throws \InvalidArgumentException |
||
161 | */ |
||
162 | View Code Duplication | public function read($key) |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | * @throws \RuntimeException |
||
181 | * @throws \InvalidArgumentException |
||
182 | */ |
||
183 | public function write($key, $content) |
||
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | * @throws \RuntimeException |
||
217 | * @throws \InvalidArgumentException |
||
218 | */ |
||
219 | public function exists($key) |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | * @throws \RuntimeException |
||
257 | */ |
||
258 | public function keys() |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | * @throws \RuntimeException |
||
291 | * @throws \InvalidArgumentException |
||
292 | */ |
||
293 | View Code Duplication | public function mtime($key) |
|
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | * @throws \RuntimeException |
||
312 | * @throws \InvalidArgumentException |
||
313 | */ |
||
314 | View Code Duplication | public function delete($key) |
|
329 | |||
330 | /** |
||
331 | * {@inheritdoc} |
||
332 | * @throws \RuntimeException |
||
333 | * @throws \InvalidArgumentException |
||
334 | */ |
||
335 | public function rename($sourceKey, $targetKey) |
||
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | public function isDirectory($key) |
||
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | * @throws \RuntimeException |
||
369 | * @throws \InvalidArgumentException |
||
370 | */ |
||
371 | View Code Duplication | public function setMetadata($key, $content) |
|
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | * @throws \RuntimeException |
||
394 | * @throws \InvalidArgumentException |
||
395 | */ |
||
396 | View Code Duplication | public function getMetadata($key) |
|
417 | |||
418 | /** |
||
419 | * Lazy initialization, automatically called when some method is called after construction. |
||
420 | */ |
||
421 | protected function init() |
||
427 | |||
428 | /** |
||
429 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
430 | * |
||
431 | * @param ServiceException $exception |
||
432 | * @param string $action |
||
433 | * @param string $containerName |
||
434 | * |
||
435 | * @throws \RuntimeException |
||
436 | */ |
||
437 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
449 | |||
450 | /** |
||
451 | * Extracts the error code from a service exception. |
||
452 | * |
||
453 | * @param ServiceException $exception |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
467 | |||
468 | /** |
||
469 | * @param string|resource $content |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | View Code Duplication | private function guessContentType($content) |
|
483 | |||
484 | /** |
||
485 | * @param string $key |
||
486 | * |
||
487 | * @return array |
||
488 | * @throws \InvalidArgumentException |
||
489 | */ |
||
490 | private function tokenizeKey($key) |
||
508 | |||
509 | /** |
||
510 | * @param string $containerName |
||
511 | * @param null $prefix |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | private function fetchBlobs($containerName, $prefix = null) |
||
529 | } |
||
530 |