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 |
||
24 | class Client extends BaseClient |
||
25 | { |
||
26 | /** |
||
27 | * Allow media type. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $allowTypes = ['image', 'voice', 'video', 'thumb', 'news_image']; |
||
32 | |||
33 | /** |
||
34 | * Upload image. |
||
35 | * |
||
36 | * @param string $path |
||
37 | * |
||
38 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
39 | */ |
||
40 | public function uploadImage(string $path) |
||
44 | |||
45 | /** |
||
46 | * Upload voice. |
||
47 | * |
||
48 | * @param string $path |
||
49 | * |
||
50 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
51 | */ |
||
52 | public function uploadVoice(string $path) |
||
56 | |||
57 | /** |
||
58 | * Upload thumb. |
||
59 | * |
||
60 | * @param string $path |
||
61 | * |
||
62 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
63 | */ |
||
64 | public function uploadThumb(string $path) |
||
68 | |||
69 | /** |
||
70 | * Upload video. |
||
71 | * |
||
72 | * @param string $path |
||
73 | * @param string $title |
||
74 | * @param string $description |
||
75 | * |
||
76 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
77 | */ |
||
78 | public function uploadVideo(string $path, string $title, string $description) |
||
90 | |||
91 | /** |
||
92 | * Upload articles. |
||
93 | * |
||
94 | * @param array|Article $articles |
||
95 | * |
||
96 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
97 | */ |
||
98 | public function uploadArticle($articles) |
||
114 | |||
115 | /** |
||
116 | * Update article. |
||
117 | * |
||
118 | * @param string $mediaId |
||
119 | * @param array|Article $article |
||
120 | * @param int $index |
||
121 | * |
||
122 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
123 | */ |
||
124 | public function updateArticle(string $mediaId, $article, int $index = 0) |
||
138 | |||
139 | /** |
||
140 | * Upload image for article. |
||
141 | * |
||
142 | * @param string $path |
||
143 | * |
||
144 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
145 | */ |
||
146 | public function uploadArticleImage(string $path) |
||
150 | |||
151 | /** |
||
152 | * Fetch material. |
||
153 | * |
||
154 | * @param string $mediaId |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | View Code Duplication | public function get(string $mediaId) |
|
168 | |||
169 | /** |
||
170 | * Delete material by media ID. |
||
171 | * |
||
172 | * @param string $mediaId |
||
173 | * |
||
174 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
175 | */ |
||
176 | public function delete(string $mediaId) |
||
180 | |||
181 | /** |
||
182 | * List materials. |
||
183 | * |
||
184 | * example: |
||
185 | * |
||
186 | * { |
||
187 | * "total_count": TOTAL_COUNT, |
||
188 | * "item_count": ITEM_COUNT, |
||
189 | * "item": [{ |
||
190 | * "media_id": MEDIA_ID, |
||
191 | * "name": NAME, |
||
192 | * "update_time": UPDATE_TIME |
||
193 | * }, |
||
194 | * // more... |
||
195 | * ] |
||
196 | * } |
||
197 | * |
||
198 | * @param string $type |
||
199 | * @param int $offset |
||
200 | * @param int $count |
||
201 | * |
||
202 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
203 | */ |
||
204 | public function list(string $type, int $offset = 0, int $count = 20) |
||
214 | |||
215 | /** |
||
216 | * Get stats of materials. |
||
217 | * |
||
218 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
219 | */ |
||
220 | public function stats() |
||
224 | |||
225 | /** |
||
226 | * Upload material. |
||
227 | * |
||
228 | * @param string $type |
||
229 | * @param string $path |
||
230 | * @param array $form |
||
231 | * |
||
232 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
233 | * |
||
234 | * @throws InvalidArgumentException |
||
235 | */ |
||
236 | public function upload(string $type, string $path, array $form = []) |
||
246 | |||
247 | /** |
||
248 | * Get API by type. |
||
249 | * |
||
250 | * @param string $type |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getApiByType(string $type) |
||
263 | } |
||
264 |
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.