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 Cosapi 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 Cosapi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Cosapi |
||
6 | { |
||
7 | //计算sign签名的时间参数 |
||
8 | const EXPIRED_SECONDS = 180; |
||
9 | //512K |
||
10 | const SLICE_SIZE_512K = 524288; |
||
11 | //1M |
||
12 | const SLICE_SIZE_1M = 1048576; |
||
13 | //2M |
||
14 | const SLICE_SIZE_2M = 2097152; |
||
15 | //3M |
||
16 | const SLICE_SIZE_3M = 3145728; |
||
17 | //20M 大于20M的文件需要进行分片传输 |
||
18 | const MAX_UNSLICE_FILE_SIZE = 20971520; |
||
19 | //失败尝试次数 |
||
20 | const MAX_RETRY_TIMES = 3; |
||
21 | //返回的错误码 |
||
22 | const COSAPI_PARAMS_ERROR = -1; |
||
23 | const COSAPI_NETWORK_ERROR = -2; |
||
24 | |||
25 | //HTTP请求超时时间 |
||
26 | private static $timeout = 60; |
||
27 | |||
28 | /** |
||
29 | * 获取当前指定时区的 Unix 时间戳 |
||
30 | * |
||
31 | * @param string $timezone |
||
32 | * |
||
33 | * @return int |
||
34 | */ |
||
35 | 14 | public static function time($timezone = 'Asia/Shanghai') |
|
39 | |||
40 | /* |
||
41 | * 设置HTTP请求超时时间 |
||
42 | * |
||
43 | * @param int $timeout 超时时长 |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public static function setTimeout($timeout = 60) |
||
57 | |||
58 | /** |
||
59 | * 上传文件,自动判断文件大小,如果小于20M则使用普通文件上传,大于20M则使用分片上传. |
||
60 | * |
||
61 | * @param string $bucketName bucket名称 |
||
62 | * @param string $srcPath 本地文件路径 |
||
63 | * @param string $dstPath 上传的文件路径 |
||
64 | * @param string $bizAttr 文件属性 |
||
65 | * @param string $sliceSize 分片大小(512k,1m,2m,3m),默认:1m |
||
66 | * @param string $insertOnly 同名文件是否覆盖 |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | 2 | public static function upload($bucketName, $srcPath, $dstPath, |
|
91 | |||
92 | /** |
||
93 | * 创建目录. |
||
94 | * |
||
95 | * @param string $bucketName bucket名称 |
||
96 | * @param string $path 目录路径 |
||
97 | * @param string $bizAttr 目录属性 |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 1 | public static function createFolder($bucketName, $path, $bizAttr = null) |
|
129 | |||
130 | /** |
||
131 | * 目录列表. |
||
132 | * |
||
133 | * @param string $bucketName bucket名称 |
||
134 | * @param string $path 目录路径,sdk会补齐末尾的 '/' |
||
135 | * @param int $num 拉取的总数 |
||
136 | * @param string $pattern eListBoth,ListDirOnly,eListFileOnly 默认both |
||
137 | * @param int $order 默认正序(=0), 填1为反序, |
||
138 | * @param string $context 透传字段,用于翻页,前端不需理解,需要往前/往后翻页则透传回来 |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 1 | View Code Duplication | public static function listFolder( |
152 | |||
153 | /** |
||
154 | * 目录列表(前缀搜索). |
||
155 | * |
||
156 | * @param string $bucketName bucket名称 |
||
157 | * @param string $prefix 列出含此前缀的所有文件 |
||
158 | * @param int $num 拉取的总数 |
||
159 | * @param string $pattern eListBoth(默认),ListDirOnly,eListFileOnly |
||
160 | * @param int $order 默认正序(=0), 填1为反序, |
||
161 | * @param string $context 透传字段,用于翻页,前端不需理解,需要往前/往后翻页则透传回来 |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | View Code Duplication | public static function prefixSearch( |
|
175 | |||
176 | /** |
||
177 | * 目录更新. |
||
178 | * |
||
179 | * @param string $bucketName bucket名称 |
||
180 | * @param string $path 文件夹路径,SDK会补齐末尾的 '/' |
||
181 | * @param string $bizAttr 目录属性 |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public static function updateFolder($bucketName, $path, $bizAttr = null) |
||
191 | |||
192 | /** |
||
193 | * 查询目录信息. |
||
194 | * |
||
195 | * @param string $bucketName bucket名称 |
||
196 | * @param string $path 目录路径 |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | public static function statFolder($bucketName, $path) |
||
206 | |||
207 | /** |
||
208 | * 删除目录. |
||
209 | * |
||
210 | * @param string $bucketName bucket名称 |
||
211 | * @param string $path 目录路径 |
||
212 | * 注意不能删除bucket下根目录/ |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | 1 | public static function delFolder($bucketName, $path) |
|
222 | |||
223 | /** |
||
224 | * 更新文件. |
||
225 | * |
||
226 | * @param string $bucketName bucket名称 |
||
227 | * @param string $path 文件路径 |
||
228 | * @param null $bizAttr |
||
229 | * @param string $authority : eInvalid(继承Bucket的读写权限)/eWRPrivate(私有读写)/eWPrivateRPublic(公有读私有写) |
||
230 | * @param array $customer_headers_array 携带的用户自定义头域,包括 |
||
231 | * 'Cache-Control' => '*' |
||
232 | * 'Content-Type' => '*' |
||
233 | * 'Content-Disposition' => '*' |
||
234 | * 'Content-Language' => '*' |
||
235 | * 'x-cos-meta-自定义内容' => '*' |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | 1 | public static function update($bucketName, $path, |
|
246 | |||
247 | /** |
||
248 | * 移动(重命名)文件. |
||
249 | * |
||
250 | * @param string $bucketName bucket名称 |
||
251 | * @param string $srcPath 源文件路径 |
||
252 | * @param string $dstPath 目的文件名(可以是单独文件名也可以是带目录的文件名) |
||
253 | * @param int $toOverWrite 是否覆盖(当目的文件名已经存在同名文件时是否覆盖) |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 1 | public static function move($bucketName, $srcPath, $dstPath, $toOverWrite = 0) |
|
287 | |||
288 | /** |
||
289 | * 查询文件信息. |
||
290 | * |
||
291 | * @param string $bucketName bucket名称 |
||
292 | * @param string $path 文件路径 |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 6 | public static function stat($bucketName, $path) |
|
302 | |||
303 | /** |
||
304 | * 删除文件. |
||
305 | * |
||
306 | * @param string $bucketName |
||
307 | * @param string $path 文件路径 |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | 1 | public static function delFile($bucketName, $path) |
|
317 | |||
318 | /** |
||
319 | * 内部方法, 上传文件. |
||
320 | * |
||
321 | * @param string $bucketName bucket名称 |
||
322 | * @param string $srcPath 本地文件路径 |
||
323 | * @param string $dstPath 上传的文件路径 |
||
324 | * @param string $bizAttr 文件属性 |
||
325 | * @param int $insertOnly 是否覆盖同名文件:0 覆盖,1:不覆盖 |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 2 | private static function uploadfile($bucketName, $srcPath, $dstPath, $bizAttr = null, $insertOnly = null) |
|
374 | |||
375 | /** |
||
376 | * 内部方法,上传文件. |
||
377 | * |
||
378 | * @param string $bucketName bucket名称 |
||
379 | * @param string $srcPath 本地文件路径 |
||
380 | * @param string $dstPath 上传的文件路径 |
||
381 | * @param string $bizAttr 文件属性 |
||
382 | * @param string $sliceSize 分片大小 |
||
383 | * @param int $insertOnly 是否覆盖同名文件:0 覆盖,1:不覆盖 |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | private static function upload_slice( |
||
439 | |||
440 | /** |
||
441 | * 第一个分片控制消息. |
||
442 | * |
||
443 | * @param string $fileSize 文件大小 |
||
444 | * @param string $sha1 文件sha值 |
||
445 | * @param string $sliceSize 分片大小 |
||
446 | * @param string $sign 签名 |
||
447 | * @param string $url URL |
||
448 | * @param string $bizAttr 文件属性 |
||
449 | * @param string $insertOnly 同名文件是否覆盖 |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | private static function upload_prepare( |
||
491 | |||
492 | /** |
||
493 | * 分片上传. |
||
494 | * |
||
495 | * @param int $fileSize 文件大小 |
||
496 | * @param string $sha1 文件sha值 |
||
497 | * @param int $sliceSize 文件分片大小 |
||
498 | * @param string $sign 签名 |
||
499 | * @param string $url url |
||
500 | * @param string $srcPath 源文件路径 |
||
501 | * @param int $offset 文件偏移offset |
||
502 | * @param string $session session |
||
503 | * |
||
504 | * @return array |
||
505 | */ |
||
506 | private static function upload_data( |
||
562 | |||
563 | /** |
||
564 | * 构造分片body体. |
||
565 | * |
||
566 | * @param string $fileContent 文件内容 |
||
567 | * @param string $offset 文件偏移 |
||
568 | * @param string $sha 文件sha值 |
||
569 | * @param string $session session |
||
570 | * @param string $fileName 文件名 |
||
571 | * @param string $boundary 分隔符 |
||
572 | * |
||
573 | * @return string |
||
574 | */ |
||
575 | private static function generateSliceBody( |
||
598 | |||
599 | /** |
||
600 | * 内部公共函数. |
||
601 | * |
||
602 | * @param string $bucketName bucket名称 |
||
603 | * @param string $path 文件夹路径 |
||
604 | * @param int $num 拉取的总数 |
||
605 | * @param string $pattern eListBoth(默认),ListDirOnly,eListFileOnly |
||
606 | * @param int $order 默认正序(=0), 填1为反序, |
||
607 | * @param string $context 在翻页查询时候用到 |
||
608 | * |
||
609 | * @return array |
||
610 | */ |
||
611 | 1 | private static function listBase( |
|
665 | |||
666 | /** |
||
667 | * 内部公共方法(更新文件和更新文件夹). |
||
668 | * |
||
669 | * @param string $bucketName bucket名称 |
||
670 | * @param string $path 路径 |
||
671 | * @param string $bizAttr 文件/目录属性 |
||
672 | * @param string $authority : eInvalid/eWRPrivate(私有)/eWPrivateRPublic(公有读写) |
||
673 | * @param array $custom_headers_array 携带的用户自定义头域,包括 |
||
674 | * 'Cache-Control' => '*' |
||
675 | * 'Content-Type' => '*' |
||
676 | * 'Content-Disposition' => '*' |
||
677 | * 'Content-Language' => '*' |
||
678 | * 'x-cos-meta-自定义内容' => '*' |
||
679 | * |
||
680 | * @return array|mixed |
||
681 | */ |
||
682 | 1 | private static function updateBase($bucketName, $path, |
|
738 | |||
739 | /** |
||
740 | * 内部方法. |
||
741 | * |
||
742 | * @param string $bucketName bucket名称 |
||
743 | * @param string $path 文件/目录路径 |
||
744 | * |
||
745 | * @return array |
||
746 | */ |
||
747 | 6 | private static function statBase($bucketName, $path) |
|
771 | |||
772 | /** |
||
773 | * 内部私有方法. |
||
774 | * |
||
775 | * @param string $bucketName bucket名称 |
||
776 | * @param string $path 文件/目录路径路径 |
||
777 | * |
||
778 | * @return array |
||
779 | */ |
||
780 | 2 | private static function delBase($bucketName, $path) |
|
814 | |||
815 | /** |
||
816 | * 内部公共方法, 路径编码 |
||
817 | * |
||
818 | * @param string $path 待编码路径 |
||
819 | * |
||
820 | * @return mixed |
||
821 | */ |
||
822 | 14 | private static function cosUrlEncode($path) |
|
826 | |||
827 | /** |
||
828 | * 内部公共方法, 构造URL. |
||
829 | * |
||
830 | * @param string $bucketName |
||
831 | * @param string $dstPath |
||
832 | * |
||
833 | * @return string |
||
834 | */ |
||
835 | 14 | private static function generateResUrl($bucketName, $dstPath) |
|
839 | |||
840 | /** |
||
841 | * 内部公共方法, 发送消息. |
||
842 | * |
||
843 | * @param array $req |
||
844 | * |
||
845 | * @return array |
||
846 | */ |
||
847 | 14 | private static function sendRequest($req) |
|
871 | |||
872 | /** |
||
873 | * 设置分片大小. |
||
874 | * |
||
875 | * @param string $sliceSize |
||
876 | * |
||
877 | * @return int |
||
878 | */ |
||
879 | private static function getSliceSize($sliceSize) |
||
898 | |||
899 | /** |
||
900 | * 内部方法, 规整文件路径. |
||
901 | * |
||
902 | * @param string $path 文件路径 |
||
903 | * @param bool $isfolder 是否为文件夹 |
||
904 | * |
||
905 | * @return string |
||
906 | */ |
||
907 | 12 | private static function normalizerPath($path, $isfolder = false) |
|
921 | |||
922 | /** |
||
923 | * 判断authority值是否正确. |
||
924 | * |
||
925 | * @param string $authority |
||
926 | * |
||
927 | * @return bool |
||
928 | */ |
||
929 | 1 | private static function isAuthorityValid($authority) |
|
939 | |||
940 | /** |
||
941 | * 判断pattern值是否正确. |
||
942 | * |
||
943 | * |
||
944 | * @param string $pattern |
||
945 | * |
||
946 | * @return bool |
||
947 | */ |
||
948 | 1 | private static function isPatternValid($pattern) |
|
958 | |||
959 | /** |
||
960 | * 判断是否符合自定义属性. |
||
961 | * |
||
962 | * @param string $key |
||
963 | * |
||
964 | * @return bool |
||
965 | */ |
||
966 | private static function isCustomer_header($key) |
||
978 | |||
979 | /** |
||
980 | * 增加自定义属性到data中. |
||
981 | * |
||
982 | * @param array $data |
||
983 | * @param array $customer_headers_array |
||
984 | */ |
||
985 | private static function add_customer_header(&$data, &$customer_headers_array) |
||
996 | } |
||
997 |
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.