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 |
||
7 | class Batch extends AbstractAPI |
||
8 | { |
||
9 | const API_SYNC_USER = 'https://qyapi.weixin.qq.com/cgi-bin/batch/syncuser'; |
||
10 | const API_REPLACE_USER = 'https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser'; |
||
11 | const API_REPLACE_PARTY = 'https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty'; |
||
12 | const API_GET_RESULT = 'https://qyapi.weixin.qq.com/cgi-bin/batch/getresult'; |
||
13 | |||
14 | /** |
||
15 | * Batch sync user. |
||
16 | * |
||
17 | * @param $mediaId |
||
18 | * @param array $callback |
||
19 | * |
||
20 | * @return \EntWeChat\Support\Collection |
||
21 | */ |
||
22 | View Code Duplication | public function batchSyncUser($mediaId, $callback = []) |
|
31 | |||
32 | /** |
||
33 | * Batch replace user. |
||
34 | * |
||
35 | * @param $mediaId |
||
36 | * @param array $callback |
||
37 | * |
||
38 | * @return \EntWeChat\Support\Collection |
||
39 | */ |
||
40 | View Code Duplication | public function batchReplaceUser($mediaId, $callback = []) |
|
49 | |||
50 | /** |
||
51 | * Batch replace party. |
||
52 | * |
||
53 | * @param $mediaId |
||
54 | * @param array $callback |
||
55 | * |
||
56 | * @return \EntWeChat\Support\Collection |
||
57 | */ |
||
58 | View Code Duplication | public function batchReplaceParty($mediaId, $callback = []) |
|
67 | |||
68 | /** |
||
69 | * Get result. |
||
70 | * |
||
71 | * @param $jobId |
||
72 | * |
||
73 | * @return \EntWeChat\Support\Collection |
||
74 | */ |
||
75 | public function getResult($jobId) |
||
83 | } |
||
84 |
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.