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 Material extends AbstractAPI |
||
13 | { |
||
14 | /** |
||
15 | * Allow media type. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $allowTypes = ['image', 'voice', 'video', 'file', 'news_image']; |
||
20 | |||
21 | const API_GET = 'https://qyapi.weixin.qq.com/cgi-bin/material/get'; |
||
22 | const API_UPLOAD = 'https://qyapi.weixin.qq.com/cgi-bin/material/add_material'; |
||
23 | const API_DELETE = 'https://qyapi.weixin.qq.com/cgi-bin/material/del'; |
||
24 | const API_STATS = 'https://qyapi.weixin.qq.com/cgi-bin/material/get_count'; |
||
25 | const API_LISTS = 'https://qyapi.weixin.qq.com/cgi-bin/material/batchget'; |
||
26 | const API_NEWS_UPLOAD = 'https://qyapi.weixin.qq.com/cgi-bin/material/add_mpnews'; |
||
27 | const API_NEWS_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/material/update_mpnews'; |
||
28 | const API_NEWS_IMAGE_UPLOAD = 'https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg'; |
||
29 | |||
30 | /** |
||
31 | * Upload image. |
||
32 | * |
||
33 | * @param string $path |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | public function uploadImage($path) |
||
41 | |||
42 | /** |
||
43 | * Upload voice. |
||
44 | * |
||
45 | * @param string $path |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function uploadVoice($path) |
||
53 | |||
54 | /** |
||
55 | * Upload file. |
||
56 | * |
||
57 | * @param string $path |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public function uploadFile($path) |
||
65 | |||
66 | /** |
||
67 | * Upload video. |
||
68 | * |
||
69 | * @param string $path |
||
70 | * @param string $title |
||
71 | * @param string $description |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function uploadVideo($path, $title, $description) |
||
87 | |||
88 | /** |
||
89 | * Upload articles. |
||
90 | * |
||
91 | * @param array|Article $articles |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function uploadArticle($articles) |
||
114 | |||
115 | /** |
||
116 | * Update article. |
||
117 | * |
||
118 | * @param string $mediaId |
||
119 | * @param array $article |
||
120 | * @param int $index |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function updateArticle($mediaId, $article, $index = 0) |
||
134 | |||
135 | /** |
||
136 | * Upload image for article. |
||
137 | * |
||
138 | * @param string $path |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | public function uploadArticleImage($path) |
||
146 | |||
147 | /** |
||
148 | * Fetch material. |
||
149 | * |
||
150 | * @param string $mediaId |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function get($mediaId) |
||
175 | |||
176 | /** |
||
177 | * Delete material by media ID. |
||
178 | * |
||
179 | * @param string $mediaId |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function delete($mediaId) |
||
187 | |||
188 | /** |
||
189 | * List materials. |
||
190 | * |
||
191 | * example: |
||
192 | * |
||
193 | * { |
||
194 | * "total_count": TOTAL_COUNT, |
||
195 | * "item_count": ITEM_COUNT, |
||
196 | * "item": [{ |
||
197 | * "media_id": MEDIA_ID, |
||
198 | * "name": NAME, |
||
199 | * "update_time": UPDATE_TIME |
||
200 | * }, |
||
201 | * // more... |
||
202 | * ] |
||
203 | * } |
||
204 | * |
||
205 | * @param string $type |
||
206 | * @param int $offset |
||
207 | * @param int $count |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | View Code Duplication | public function lists($type, $offset = 0, $count = 50) |
|
221 | |||
222 | /** |
||
223 | * Get stats of materials. |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function stats() |
||
231 | |||
232 | /** |
||
233 | * Upload material. |
||
234 | * |
||
235 | * @param string $type |
||
236 | * @param string $path |
||
237 | * @param array $form |
||
238 | * |
||
239 | * @throws InvalidArgumentException |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function uploadMedia($type, $path, array $form = []) |
||
253 | |||
254 | /** |
||
255 | * Get API by type. |
||
256 | * |
||
257 | * @param string $type |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getAPIByType($type) |
||
273 | } |
||
274 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.