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 |
||
12 | class MediaApi extends BaseApi |
||
13 | { |
||
14 | /** |
||
15 | * 上传临时媒体文件. $file 为 /logo/img.jpeg 服务器上图片路径 |
||
16 | * |
||
17 | * @author Tian |
||
18 | * |
||
19 | * @date 2015-10-10 |
||
20 | * |
||
21 | * @param string $file 文件路径 |
||
22 | * @param string $type 文件类型 |
||
23 | * |
||
24 | * @return bool|array 接口返回结果 |
||
25 | */ |
||
26 | View Code Duplication | public function upload($file, $type) |
|
51 | |||
52 | /** |
||
53 | * 上传临时媒体文件. |
||
54 | * |
||
55 | * @author Tian |
||
56 | * |
||
57 | * @date 2015-08-02 |
||
58 | * |
||
59 | * @param string $file 文件 为 form 表单的 $_FILES['xxx']; |
||
60 | * @param string $type 文件类型 |
||
61 | * |
||
62 | * @return bool|array 接口返回结果 |
||
63 | */ |
||
64 | public function uploadFrom($file, $type = 'image') |
||
89 | |||
90 | /** |
||
91 | * 上传临时媒体文件. $url 为 http://itse.cc/img.jpeg |
||
92 | * |
||
93 | * @param $url |
||
94 | * @param string $type |
||
95 | * |
||
96 | * @return array|bool |
||
97 | */ |
||
98 | public function uploadCurl($url, $type = 'image') |
||
134 | |||
135 | /** |
||
136 | * 根据mediaID获取媒体文件. |
||
137 | * |
||
138 | * @author Tian |
||
139 | * |
||
140 | * @date 2015-10-10 |
||
141 | * |
||
142 | * @param string $mediaId 由上传接口获取的媒体文件 |
||
143 | * |
||
144 | * @return array 如果成功则返回 content是由base64编码过的文件内容 解码后为正常的文件内容. |
||
145 | */ |
||
146 | public function get($mediaId) |
||
155 | |||
156 | /** |
||
157 | * 上传图文消息内的图片. |
||
158 | * |
||
159 | * @param string $file 媒体文件路径 图片仅支持jpg/png格式,大小必须在1MB以下 |
||
160 | * |
||
161 | * @return string. 获取地址 http://mmbiz.qpic.cn/mmbiz/NdxGKqW8jE9GbAqUEPdSgSvbUbProSmE8NbUFwIYnp0Duibs611ZsCLza6b2dS8Ex3CO5dtv0u1HP9QY32djCxA/0 |
||
162 | */ |
||
163 | View Code Duplication | public function uploadimg($file) |
|
186 | |||
187 | /** |
||
188 | * 上传图文消息内的视频. |
||
189 | * |
||
190 | * @param string $media_id 媒体Id |
||
191 | * @param string $title 标题 |
||
192 | * @param string $description 描述 |
||
193 | * |
||
194 | * @return bool|array. "type":"video","media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc","created_at":1398848981 |
||
195 | */ |
||
196 | public function uploadvideo($media_id, $title, $description) |
||
213 | |||
214 | /** |
||
215 | * 上传图文消息素材【订阅号与服务号认证后均可用】 |
||
216 | * |
||
217 | * @param array $articles 图文消息,一个图文消息支持1到8条图文 |
||
218 | * |
||
219 | * string $articles ->thumb_media_id 图文消息缩略图的media_id,可以在基础支持-上传多媒体文件接口中获得 |
||
220 | * string $articles ->author 图文消息的作者 |
||
221 | * string $articles ->title 图文消息的标题 |
||
222 | * utl $articles ->content_source_url 在图文消息页面点击“阅读原文”后的页面 |
||
223 | * html $articles ->content 图文消息页面的内容,支持HTML标签。具备微信支付权限的公众号,可以使用a标签,其他公众号不能使用 |
||
224 | * string $articles ->digest 图文消息的描述 |
||
225 | * int $articles ->show_cover_pic 是否显示封面,1为显示,0为不显示 |
||
226 | * |
||
227 | * @return bool|array |
||
228 | */ |
||
229 | public function uploadnews($articles = []) |
||
244 | |||
245 | /** |
||
246 | * 上传卡券logo图片. $file 为 /logo/img.jpeg |
||
247 | * |
||
248 | * @param string $file 媒体文件路径 图片仅支持jpg/png格式,大小必须在1MB以下 |
||
249 | * |
||
250 | * @return string. 获取地址 http://mmbiz.qpic.cn/mmbiz/NdxGKqW8jE9GbAqUEPdSgSvbUbProSmE8NbUFwIYnp0Duibs611ZsCLza6b2dS8Ex3CO5dtv0u1HP9QY32djCxA/0 |
||
251 | */ |
||
252 | View Code Duplication | public function cardUploadimg($file) |
|
275 | |||
276 | /** |
||
277 | * 上传卡券logo图片. $file为 $_FILES['xxx']; |
||
278 | * |
||
279 | * @param string $file 文件 |
||
280 | * |
||
281 | * @return string. 获取地址 http://mmbiz.qpic.cn/mmbiz/NdxGKqW8jE9GbAqUEPdSgSvbUbProSmE8NbUFwIYnp0Duibs611ZsCLza6b2dS8Ex3CO5dtv0u1HP9QY32djCxA/0 |
||
282 | */ |
||
283 | public function cardUpload_img($file) |
||
306 | |||
307 | /** |
||
308 | * 上传卡券logo图片. $url 为 http://itse.cc/img.jpeg |
||
309 | * |
||
310 | * @param $url |
||
311 | * |
||
312 | * @return array|bool |
||
313 | */ |
||
314 | function curlCardUpload_img($url) |
||
349 | |||
350 | /** |
||
351 | * curl 抓取图片 + 头信息 |
||
352 | * |
||
353 | * @param $url |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | private static function curl_get($url) |
||
386 | } |
||
387 |
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.