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 |
||
20 | class AzureBlobStorage implements Adapter, |
||
|
|||
21 | MetadataSupporter |
||
22 | { |
||
23 | /** |
||
24 | * Error constants. |
||
25 | */ |
||
26 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
27 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
28 | |||
29 | /** |
||
30 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
31 | */ |
||
32 | protected $blobProxyFactory; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $containerName; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $detectContentType; |
||
43 | |||
44 | /** |
||
45 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
46 | */ |
||
47 | protected $blobProxy; |
||
48 | |||
49 | /** |
||
50 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
51 | * @param string $containerName |
||
52 | * @param bool $create |
||
53 | * @param bool $detectContentType |
||
54 | */ |
||
55 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName, $create = false, $detectContentType = true) |
||
64 | |||
65 | /** |
||
66 | * Creates a new container. |
||
67 | * |
||
68 | * @param string $containerName |
||
69 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
70 | * |
||
71 | * @throws \RuntimeException if cannot create the container |
||
72 | */ |
||
73 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
92 | |||
93 | /** |
||
94 | * Deletes a container. |
||
95 | * |
||
96 | * @param string $containerName |
||
97 | * @param DeleteContainerOptions $options |
||
98 | * |
||
99 | * @throws \RuntimeException if cannot delete the container |
||
100 | */ |
||
101 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | View Code Duplication | public function read($key) |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function write($key, $content) |
||
143 | { |
||
144 | $this->init(); |
||
145 | |||
146 | try { |
||
147 | $options = new CreateBlobOptions(); |
||
148 | |||
149 | if ($this->detectContentType) { |
||
150 | $contentType = $this->guessContentType($content); |
||
151 | |||
152 | $options->setBlobContentType($contentType); |
||
153 | } |
||
154 | |||
155 | $this->blobProxy->createBlockBlob($this->containerName, $key, $content, $options); |
||
156 | |||
157 | if (is_resource($content)) { |
||
158 | return Util\Size::fromResource($content); |
||
159 | } |
||
160 | |||
161 | return Util\Size::fromContent($content); |
||
162 | } catch (ServiceException $e) { |
||
163 | $this->failIfContainerNotFound($e, sprintf('write content for key "%s"', $key)); |
||
164 | |||
165 | return false; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function exists($key) |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function keys() |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | public function mtime($key) |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | View Code Duplication | public function delete($key) |
|
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function rename($sourceKey, $targetKey) |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | public function isDirectory($key) |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | View Code Duplication | public function setMetadata($key, $content) |
|
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | View Code Duplication | public function getMetadata($key) |
|
341 | |||
342 | /** |
||
343 | * Lazy initialization, automatically called when some method is called after construction. |
||
344 | */ |
||
345 | protected function init() |
||
351 | |||
352 | /** |
||
353 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
354 | * |
||
355 | * @param ServiceException $exception |
||
356 | * @param string $action |
||
357 | * |
||
358 | * @throws \RuntimeException |
||
359 | */ |
||
360 | protected function failIfContainerNotFound(ServiceException $exception, $action) |
||
372 | |||
373 | /** |
||
374 | * Extracts the error code from a service exception. |
||
375 | * |
||
376 | * @param ServiceException $exception |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
390 | |||
391 | /** |
||
392 | * @param string $content |
||
393 | * |
||
394 | * @return string |
||
395 | */ |
||
396 | View Code Duplication | private function guessContentType($content) |
|
406 | } |
||
407 |