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 |
||
18 | class AzureAdapter extends AbstractAdapter |
||
19 | { |
||
20 | use NotSupportingVisibilityTrait; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $container; |
||
26 | |||
27 | /** |
||
28 | * @var IBlob |
||
29 | */ |
||
30 | protected $client; |
||
31 | |||
32 | /** |
||
33 | * @var string[] |
||
34 | */ |
||
35 | protected static $metaOptions = [ |
||
36 | 'CacheControl', |
||
37 | 'ContentType', |
||
38 | 'Metadata', |
||
39 | 'ContentLanguage', |
||
40 | 'ContentEncoding', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param IBlob $azureClient |
||
47 | * @param string $container |
||
48 | * @param string $prefix |
||
49 | */ |
||
50 | public function __construct(IBlob $azureClient, $container, $prefix = null) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function write($path, $contents, Config $config) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function writeStream($path, $resource, Config $config) |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function update($path, $contents, Config $config) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function updateStream($path, $resource, Config $config) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function rename($path, $newpath) |
||
98 | |||
99 | public function copy($path, $newpath) |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function delete($path) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function deleteDir($dirname) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function createDir($dirname, Config $config) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function has($path) |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | View Code Duplication | public function read($path) |
|
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | View Code Duplication | public function readStream($path) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function listContents($directory = '', $recursive = false) |
||
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | public function getMetadata($path) |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function getSize($path) |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function getMimetype($path) |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function getTimestamp($path) |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | public function getUrl($path) |
||
284 | |||
285 | /** |
||
286 | * Builds the normalized output array. |
||
287 | * |
||
288 | * @param string $path |
||
289 | * @param int $timestamp |
||
290 | * @param mixed $content |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | protected function normalize($path, $timestamp, $content = null) |
||
309 | |||
310 | /** |
||
311 | * Builds the normalized output array from a Blob object. |
||
312 | * |
||
313 | * @param string $path |
||
314 | * @param BlobProperties $properties |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | protected function normalizeBlobProperties($path, BlobProperties $properties) |
||
335 | |||
336 | /** |
||
337 | * Builds the normalized output array from a BlobPrefix object. |
||
338 | * |
||
339 | * @param BlobPrefix $blobPrefix |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function normalizeBlobPrefix(BlobPrefix $blobPrefix) |
||
347 | |||
348 | /** |
||
349 | * Retrieves content streamed by Azure into a string. |
||
350 | * |
||
351 | * @param resource $resource |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function streamContentsToString($resource) |
||
359 | |||
360 | /** |
||
361 | * Upload a file. |
||
362 | * |
||
363 | * @param string $path Path |
||
364 | * @param string|resource $contents Either a string or a stream. |
||
365 | * @param Config $config Config |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | protected function upload($path, $contents, Config $config) |
||
385 | |||
386 | /** |
||
387 | * Retrieve options from a Config instance. |
||
388 | * |
||
389 | * @param Config $config |
||
390 | * |
||
391 | * @return CreateBlobOptions |
||
392 | */ |
||
393 | protected function getOptionsFromConfig(Config $config) |
||
411 | } |
||
412 |
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.