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 |
||
32 | View Code Duplication | class API extends AbstractAPI |
|
33 | { |
||
34 | /** |
||
35 | * Merchant instance. |
||
36 | * |
||
37 | * @var Merchant |
||
38 | */ |
||
39 | protected $merchant; |
||
40 | |||
41 | // api |
||
42 | const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; |
||
43 | const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; |
||
44 | const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; |
||
45 | const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder'; |
||
46 | |||
47 | // LuckyMoney type |
||
48 | const TYPE_NORMAL = 'NORMAL'; |
||
49 | const TYPE_GROUP = 'GROUP'; |
||
50 | |||
51 | // Risk control type. |
||
52 | const RISK_NORMAL = 'NORMAL'; |
||
53 | const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT'; |
||
54 | const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT'; |
||
55 | const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT'; |
||
56 | |||
57 | /** |
||
58 | * API constructor. |
||
59 | * |
||
60 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
61 | */ |
||
62 | public function __construct(Merchant $merchant) |
||
66 | |||
67 | /** |
||
68 | * Prepare luckymoney. |
||
69 | * |
||
70 | * |
||
71 | * @return Collection |
||
72 | */ |
||
73 | public function prepare(array $params) |
||
86 | |||
87 | /** |
||
88 | * Query luckymoney. |
||
89 | * |
||
90 | * @param string $mchBillNo |
||
91 | */ |
||
92 | public function query($orderNo) |
||
102 | |||
103 | /** |
||
104 | * Send Luckymoney. |
||
105 | * |
||
106 | * @param array $params |
||
107 | * @param string $type |
||
108 | */ |
||
109 | public function send(array $params, $type = self::TYPE_NORMAL) |
||
121 | |||
122 | /** |
||
123 | * Send normal lucnymoney. |
||
124 | * |
||
125 | * @param array $params |
||
126 | * |
||
127 | * @return Collection |
||
128 | */ |
||
129 | public function sendNormal($params) |
||
136 | |||
137 | /** |
||
138 | * Send group luckymoney. |
||
139 | * |
||
140 | * @param array $params |
||
141 | * |
||
142 | * @return Collection |
||
143 | */ |
||
144 | public function sendGroup($params) |
||
151 | |||
152 | /** |
||
153 | * Merchant setter. |
||
154 | * |
||
155 | * @param Merchant $merchant |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function setMerchant(Merchant $merchant) |
||
163 | |||
164 | /** |
||
165 | * Merchant getter. |
||
166 | * |
||
167 | * @return Merchant |
||
168 | */ |
||
169 | public function getMerchant() |
||
173 | |||
174 | /** |
||
175 | * Make a API request. |
||
176 | * |
||
177 | * @param string $api |
||
178 | * @param array $params |
||
179 | * @param string $method |
||
180 | * |
||
181 | * @return Collection |
||
182 | */ |
||
183 | protected function request($api, array $params, $method = 'post') |
||
195 | |||
196 | /** |
||
197 | * Parse Response XML to array. |
||
198 | * |
||
199 | * @param string $response |
||
200 | * |
||
201 | * @return Collection |
||
202 | */ |
||
203 | protected function parseResponse($response) |
||
211 | } |
||
212 |
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.