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:
1 | <?php |
||
20 | View Code Duplication | class BlobStorage implements Adapter, Adapter\MetadataSupporter |
|
|
|||
21 | { |
||
22 | /** |
||
23 | * Error constants. |
||
24 | */ |
||
25 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
26 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
27 | |||
28 | /** |
||
29 | * @var BlobProxyFactoryInterface |
||
30 | */ |
||
31 | protected $blobProxyFactory; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $containerName; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $detectContentType; |
||
42 | |||
43 | /** |
||
44 | * @var IBlob |
||
45 | */ |
||
46 | protected $blobProxy; |
||
47 | |||
48 | /** |
||
49 | * @param BlobProxyFactoryInterface $blobProxyFactory |
||
50 | * @param string $containerName |
||
51 | * @param bool $create |
||
52 | * @param bool $detectContentType |
||
53 | */ |
||
54 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName, $create = false, $detectContentType = true) |
||
63 | |||
64 | /** |
||
65 | * Creates a new container. |
||
66 | * |
||
67 | * @param string $containerName |
||
68 | * @param CreateContainerOptions|null $options |
||
69 | * |
||
70 | * @throws \RuntimeException if cannot create the container |
||
71 | */ |
||
72 | public function createContainer($containerName, CreateContainerOptions $options = null) |
||
91 | |||
92 | /** |
||
93 | * Deletes a container. |
||
94 | * |
||
95 | * @param string $containerName |
||
96 | * @param DeleteContainerOptions $options |
||
97 | * |
||
98 | * @throws \RuntimeException if cannot delete the container |
||
99 | */ |
||
100 | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function read($key) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function write($key, $content) |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function exists($key) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function keys() |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function mtime($key) |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function delete($key) |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | public function rename($sourceKey, $targetKey) |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | public function isDirectory($key) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function setMetadata($key, $content) |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function getMetadata($key) |
||
336 | |||
337 | /** |
||
338 | * Lazy initialization, automatically called when some method is called after construction. |
||
339 | */ |
||
340 | protected function init() |
||
346 | |||
347 | /** |
||
348 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
349 | * |
||
350 | * @param ServiceException $exception |
||
351 | * @param string $action |
||
352 | * |
||
353 | * @throws \RuntimeException |
||
354 | */ |
||
355 | protected function failIfContainerNotFound(ServiceException $exception, $action) |
||
367 | |||
368 | /** |
||
369 | * Extracts the error code from a service exception. |
||
370 | * |
||
371 | * @param ServiceException $exception |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
385 | } |
||
386 |
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.