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) |
||
77 | |||
78 | /** |
||
79 | * @return CreateContainerOptions |
||
80 | */ |
||
81 | public function getCreateContainerOptions() |
||
85 | |||
86 | /** |
||
87 | * @param CreateContainerOptions $options |
||
88 | */ |
||
89 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
93 | |||
94 | /** |
||
95 | * Creates a new container. |
||
96 | * |
||
97 | * @param string $containerName |
||
98 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
99 | * |
||
100 | * @throws \RuntimeException if cannot create the container |
||
101 | */ |
||
102 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
121 | |||
122 | /** |
||
123 | * Deletes a container. |
||
124 | * |
||
125 | * @param string $containerName |
||
126 | * @param DeleteContainerOptions $options |
||
127 | * |
||
128 | * @throws \RuntimeException if cannot delete the container |
||
129 | */ |
||
130 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | * @throws \RuntimeException |
||
153 | */ |
||
154 | View Code Duplication | public function read($key) |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | * @throws \RuntimeException |
||
172 | */ |
||
173 | public function write($key, $content) |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | * @throws \RuntimeException |
||
203 | */ |
||
204 | public function exists($key) |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | * @throws \RuntimeException |
||
238 | */ |
||
239 | public function keys() |
||
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | * @throws \RuntimeException |
||
268 | */ |
||
269 | public function mtime($key) |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | * @throws \RuntimeException |
||
287 | */ |
||
288 | View Code Duplication | public function delete($key) |
|
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | * @throws \RuntimeException |
||
306 | */ |
||
307 | public function rename($sourceKey, $targetKey) |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function isDirectory($key) |
||
331 | |||
332 | /** |
||
333 | * {@inheritdoc} |
||
334 | * @throws \RuntimeException |
||
335 | */ |
||
336 | View Code Duplication | public function setMetadata($key, $content) |
|
354 | |||
355 | /** |
||
356 | * {@inheritdoc} |
||
357 | * @throws \RuntimeException |
||
358 | */ |
||
359 | View Code Duplication | public function getMetadata($key) |
|
379 | |||
380 | /** |
||
381 | * Lazy initialization, automatically called when some method is called after construction. |
||
382 | */ |
||
383 | protected function init() |
||
389 | |||
390 | /** |
||
391 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
392 | * |
||
393 | * @param ServiceException $exception |
||
394 | * @param string $action |
||
395 | * |
||
396 | * @throws \RuntimeException |
||
397 | */ |
||
398 | protected function failIfContainerNotFound(ServiceException $exception, $action) |
||
410 | |||
411 | /** |
||
412 | * Extracts the error code from a service exception. |
||
413 | * |
||
414 | * @param ServiceException $exception |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
422 | |||
423 | /** |
||
424 | * @param string|resource $content |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | View Code Duplication | private function guessContentType($content) |
|
438 | } |
||
439 |