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 AliyunOssAdapter 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 AliyunOssAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class AliyunOssAdapter extends AbstractAdapter implements CanOverwriteFiles |
||
17 | { |
||
18 | /** |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $debug; |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $resultMap = [ |
||
26 | 'Body' => 'raw_contents', |
||
27 | 'Content-Length' => 'size', |
||
28 | 'ContentType' => 'mimetype', |
||
29 | 'Size' => 'size', |
||
30 | 'StorageClass' => 'storage_class', |
||
31 | ]; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $metaOptions = [ |
||
36 | 'CacheControl', |
||
37 | 'Expires', |
||
38 | 'ServerSideEncryption', |
||
39 | 'Metadata', |
||
40 | 'ACL', |
||
41 | 'ContentType', |
||
42 | 'ContentDisposition', |
||
43 | 'ContentLanguage', |
||
44 | 'ContentEncoding', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected static $metaMap = [ |
||
51 | 'CacheControl' => 'Cache-Control', |
||
52 | 'Expires' => 'Expires', |
||
53 | 'ServerSideEncryption' => 'x-oss-server-side-encryption', |
||
54 | 'Metadata' => 'x-oss-metadata-directive', |
||
55 | 'ACL' => 'x-oss-object-acl', |
||
56 | 'ContentType' => 'Content-Type', |
||
57 | 'ContentDisposition' => 'Content-Disposition', |
||
58 | 'ContentLanguage' => 'response-content-language', |
||
59 | 'ContentEncoding' => 'Content-Encoding', |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * @var OssClient |
||
64 | */ |
||
65 | protected $client; |
||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $bucket; |
||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $endPoint; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $cdnDomain; |
||
79 | |||
80 | /** |
||
81 | * @var bool |
||
82 | */ |
||
83 | protected $ssl; |
||
84 | |||
85 | /** |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $isCname; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $options = [ |
||
94 | // 'Multipart' => 128 |
||
95 | ]; |
||
96 | |||
97 | /** |
||
98 | * AliyunOssAdapter constructor. |
||
99 | * @param OssClient $client |
||
100 | * @param AliyunOssConfig $config |
||
101 | * @param array $options |
||
102 | */ |
||
103 | 2 | public function __construct( |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 3 | View Code Duplication | public function write($path, $contents, Config $config) |
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 1 | public function writeStream($path, $resource, Config $config) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 1 | View Code Duplication | public function writeFile($path, $filePath, Config $config) |
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | View Code Duplication | public function update($path, $contents, Config $config) |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | View Code Duplication | public function updateStream($path, $resource, Config $config) |
|
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function rename($path, $newpath) |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | View Code Duplication | public function copy($path, $newpath) |
|
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | View Code Duplication | public function delete($path) |
|
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function deleteDir($dirname) |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function createDir($dirname, Config $config) |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function setVisibility($path, $visibility) |
||
328 | |||
329 | /** |
||
330 | * 列举文件夹内文件列表;可递归获取子文件夹; |
||
331 | * @param string $dirname 目录 |
||
332 | * @param bool $recursive 是否递归 |
||
333 | * @return mixed |
||
334 | * @throws OssException |
||
335 | */ |
||
336 | public function listDirObjects($dirname = '', $recursive = false) |
||
406 | |||
407 | /** |
||
408 | * {@inheritdoc} |
||
409 | */ |
||
410 | public function has($path) |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | public function read($path) |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | public function readStream($path) |
||
442 | |||
443 | /** |
||
444 | * {@inheritdoc} |
||
445 | */ |
||
446 | public function listContents($directory = '', $recursive = false) |
||
458 | |||
459 | /** |
||
460 | * {@inheritdoc} |
||
461 | */ |
||
462 | View Code Duplication | public function getMetadata($path) |
|
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | public function getSize($path) |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function getMimetype($path) |
||
496 | |||
497 | /** |
||
498 | * {@inheritdoc} |
||
499 | */ |
||
500 | public function getTimestamp($path) |
||
507 | |||
508 | /** |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | public function getVisibility($path) |
||
529 | |||
530 | /** |
||
531 | * @param string $path |
||
532 | * @return array|null[]|string[] |
||
533 | */ |
||
534 | protected function readObject($path) |
||
542 | |||
543 | /** |
||
544 | * @param string $path |
||
545 | * @return string |
||
546 | */ |
||
547 | public function getUrl($path) |
||
552 | |||
553 | /** |
||
554 | * Get a temporary URL for the file at the given path. |
||
555 | * |
||
556 | * @param string $path |
||
557 | * @param \DateTimeInterface|int $expiration |
||
558 | * @param array $options |
||
559 | * @return string |
||
560 | * |
||
561 | * @throws \RuntimeException |
||
562 | */ |
||
563 | public function getTemporaryUrl($path, $expiration, array $options = []) |
||
570 | |||
571 | /** |
||
572 | * The the ACL visibility. |
||
573 | * |
||
574 | * @param string $path |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | protected function getObjectACL($path) |
||
584 | |||
585 | /** |
||
586 | * Normalize a result from OSS. |
||
587 | * |
||
588 | * @param array $object |
||
589 | * @param string $path |
||
590 | * |
||
591 | * @return array file metadata |
||
592 | */ |
||
593 | 5 | protected function normalizeResponse(array $object, $path = null) |
|
613 | |||
614 | /** |
||
615 | * Get options for a OSS call. done |
||
616 | * |
||
617 | * @param array $options |
||
618 | * |
||
619 | * @return array OSS options |
||
620 | */ |
||
621 | 5 | protected function getOptions(array $options = [], Config $config = null) |
|
631 | |||
632 | /** |
||
633 | * Retrieve options from a Config instance. done |
||
634 | * |
||
635 | * @param Config $config |
||
636 | * |
||
637 | * @return array |
||
638 | */ |
||
639 | 5 | protected function getOptionsFromConfig(Config $config) |
|
661 | |||
662 | /** |
||
663 | * @param string $func |
||
664 | * @param \Exception $e |
||
665 | */ |
||
666 | protected function logErr($func, $e) |
||
673 | } |
||
674 |
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.