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:
Complex classes like API often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use API, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class API extends AbstractAPI |
||
35 | { |
||
36 | /** |
||
37 | * Merchant instance. |
||
38 | * |
||
39 | * @var Merchant |
||
40 | */ |
||
41 | protected $merchant; |
||
42 | |||
43 | /** |
||
44 | * Sandbox box mode. |
||
45 | * |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $sandboxEnabled = false; |
||
49 | |||
50 | /** |
||
51 | * Sandbox sign key. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $sandboxSignKey; |
||
56 | |||
57 | /** |
||
58 | * Cache. |
||
59 | * |
||
60 | * @var Cache |
||
61 | */ |
||
62 | protected $cache; |
||
63 | |||
64 | const API_HOST = 'https://api.mch.weixin.qq.com'; |
||
65 | |||
66 | // api |
||
67 | const API_PAY_ORDER = '/pay/micropay'; |
||
68 | const API_PREPARE_ORDER = '/pay/unifiedorder'; |
||
69 | const API_QUERY = '/pay/orderquery'; |
||
70 | const API_CLOSE = '/pay/closeorder'; |
||
71 | const API_REVERSE = '/secapi/pay/reverse'; |
||
72 | const API_REFUND = '/secapi/pay/refund'; |
||
73 | const API_QUERY_REFUND = '/pay/refundquery'; |
||
74 | const API_DOWNLOAD_BILL = '/pay/downloadbill'; |
||
75 | const API_REPORT = '/payitil/report'; |
||
76 | |||
77 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
78 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
79 | const API_SANDBOX_SIGN_KEY = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; |
||
80 | |||
81 | // order id types. |
||
82 | const TRANSACTION_ID = 'transaction_id'; |
||
83 | const OUT_TRADE_NO = 'out_trade_no'; |
||
84 | const OUT_REFUND_NO = 'out_refund_no'; |
||
85 | const REFUND_ID = 'refund_id'; |
||
86 | |||
87 | // bill types. |
||
88 | const BILL_TYPE_ALL = 'ALL'; |
||
89 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
90 | const BILL_TYPE_REFUND = 'REFUND'; |
||
91 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
92 | |||
93 | /** |
||
94 | * API constructor. |
||
95 | * |
||
96 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
97 | * @param \EasyWeChat\Payment\Cache|null $cache |
||
98 | */ |
||
99 | 12 | public function __construct(Merchant $merchant, Cache $cache = null) |
|
104 | |||
105 | /** |
||
106 | * Pay the order. |
||
107 | * |
||
108 | * @param Order $order |
||
109 | * |
||
110 | * @return \EasyWeChat\Support\Collection |
||
111 | */ |
||
112 | 1 | public function pay(Order $order) |
|
116 | |||
117 | /** |
||
118 | * Prepare order to pay. |
||
119 | * |
||
120 | * @param Order $order |
||
121 | * |
||
122 | * @return \EasyWeChat\Support\Collection |
||
123 | */ |
||
124 | 1 | public function prepare(Order $order) |
|
133 | |||
134 | /** |
||
135 | * Query order. |
||
136 | * |
||
137 | * @param string $orderNo |
||
138 | * @param string $type |
||
139 | * |
||
140 | * @return \EasyWeChat\Support\Collection |
||
141 | */ |
||
142 | 1 | View Code Duplication | public function query($orderNo, $type = self::OUT_TRADE_NO) |
150 | |||
151 | /** |
||
152 | * Query order by transaction_id. |
||
153 | * |
||
154 | * @param string $transactionId |
||
155 | * |
||
156 | * @return \EasyWeChat\Support\Collection |
||
157 | */ |
||
158 | 1 | public function queryByTransactionId($transactionId) |
|
162 | |||
163 | /** |
||
164 | * Close order by out_trade_no. |
||
165 | * |
||
166 | * @param $tradeNo |
||
167 | * |
||
168 | * @return \EasyWeChat\Support\Collection |
||
169 | */ |
||
170 | 1 | public function close($tradeNo) |
|
178 | |||
179 | /** |
||
180 | * Reverse order. |
||
181 | * |
||
182 | * @param string $orderNo |
||
183 | * @param string $type |
||
184 | * |
||
185 | * @return \EasyWeChat\Support\Collection |
||
186 | */ |
||
187 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
195 | |||
196 | /** |
||
197 | * Reverse order by transaction_id. |
||
198 | * |
||
199 | * @param int $transactionId |
||
200 | * |
||
201 | * @return \EasyWeChat\Support\Collection |
||
202 | */ |
||
203 | public function reverseByTransactionId($transactionId) |
||
207 | |||
208 | /** |
||
209 | * Make a refund request. |
||
210 | * |
||
211 | * @param string $orderNo |
||
212 | * @param float $totalFee |
||
213 | * @param float $refundFee |
||
214 | * @param string $opUserId |
||
215 | * @param string $type |
||
216 | * @param string $refundAccount |
||
217 | * |
||
218 | * @return \EasyWeChat\Support\Collection |
||
219 | */ |
||
220 | 1 | public function refund( |
|
241 | |||
242 | /** |
||
243 | * Refund by transaction id. |
||
244 | * |
||
245 | * @param string $orderNo |
||
246 | * @param float $totalFee |
||
247 | * @param float $refundFee |
||
248 | * @param string $opUserId |
||
249 | * @param string $refundAccount |
||
250 | * |
||
251 | * @return \EasyWeChat\Support\Collection |
||
252 | */ |
||
253 | public function refundByTransactionId( |
||
263 | |||
264 | /** |
||
265 | * Query refund status. |
||
266 | * |
||
267 | * @param string $orderNo |
||
268 | * @param string $type |
||
269 | * |
||
270 | * @return \EasyWeChat\Support\Collection |
||
271 | */ |
||
272 | 1 | View Code Duplication | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
280 | |||
281 | /** |
||
282 | * Query refund status by out_refund_no. |
||
283 | * |
||
284 | * @param string $refundNo |
||
285 | * |
||
286 | * @return \EasyWeChat\Support\Collection |
||
287 | */ |
||
288 | public function queryRefundByRefundNo($refundNo) |
||
292 | |||
293 | /** |
||
294 | * Query refund status by transaction_id. |
||
295 | * |
||
296 | * @param string $transactionId |
||
297 | * |
||
298 | * @return \EasyWeChat\Support\Collection |
||
299 | */ |
||
300 | public function queryRefundByTransactionId($transactionId) |
||
304 | |||
305 | /** |
||
306 | * Query refund status by refund_id. |
||
307 | * |
||
308 | * @param string $refundId |
||
309 | * |
||
310 | * @return \EasyWeChat\Support\Collection |
||
311 | */ |
||
312 | public function queryRefundByRefundId($refundId) |
||
316 | |||
317 | /** |
||
318 | * Download bill history as a table file. |
||
319 | * |
||
320 | * @param string $date |
||
321 | * @param string $type |
||
322 | * |
||
323 | * @return \Psr\Http\Message\ResponseInterface |
||
324 | */ |
||
325 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
334 | |||
335 | /** |
||
336 | * Convert long url to short url. |
||
337 | * |
||
338 | * @param string $url |
||
339 | * |
||
340 | * @return \EasyWeChat\Support\Collection |
||
341 | */ |
||
342 | 1 | public function urlShorten($url) |
|
346 | |||
347 | /** |
||
348 | * Report API status to WeChat. |
||
349 | * |
||
350 | * @param string $api |
||
351 | * @param int $timeConsuming |
||
352 | * @param string $resultCode |
||
353 | * @param string $returnCode |
||
354 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
355 | * |
||
356 | * @return \EasyWeChat\Support\Collection |
||
357 | */ |
||
358 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
372 | |||
373 | /** |
||
374 | * Get openid by auth code. |
||
375 | * |
||
376 | * @param string $authCode |
||
377 | * |
||
378 | * @return \EasyWeChat\Support\Collection |
||
379 | */ |
||
380 | 1 | public function authCodeToOpenId($authCode) |
|
384 | |||
385 | /** |
||
386 | * Merchant setter. |
||
387 | * |
||
388 | * @param Merchant $merchant |
||
389 | * |
||
390 | * @return $this |
||
391 | */ |
||
392 | 1 | public function setMerchant(Merchant $merchant) |
|
396 | |||
397 | /** |
||
398 | * Merchant getter. |
||
399 | * |
||
400 | * @return Merchant |
||
401 | */ |
||
402 | 1 | public function getMerchant() |
|
406 | |||
407 | /** |
||
408 | * Set sandbox mode. |
||
409 | * |
||
410 | * @param bool $enabled |
||
411 | * |
||
412 | * @return $this |
||
413 | */ |
||
414 | 10 | public function sandboxMode($enabled = false) |
|
420 | |||
421 | /** |
||
422 | * Make a API request. |
||
423 | * |
||
424 | * @param string $api |
||
425 | * @param array $params |
||
426 | * @param string $method |
||
427 | * @param array $options |
||
428 | * @param bool $returnResponse |
||
429 | * |
||
430 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
431 | */ |
||
432 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
452 | |||
453 | /** |
||
454 | * Generate sign. |
||
455 | * |
||
456 | * @param array $params |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | 10 | protected function generateSign($params) |
|
461 | { |
||
462 | 10 | $key = ($this->sandboxEnabled && $this->sandboxSignKey) ? $this->sandboxSignKey : $this->merchant->key; |
|
463 | |||
464 | 10 | return generate_sign($params, $key, 'md5'); |
|
465 | } |
||
466 | |||
467 | /** |
||
468 | * Request with SSL. |
||
469 | * |
||
470 | * @param string $api |
||
471 | * @param array $params |
||
472 | * @param string $method |
||
473 | * |
||
474 | * @return \EasyWeChat\Support\Collection |
||
475 | */ |
||
476 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
485 | |||
486 | /** |
||
487 | * Parse Response XML to array. |
||
488 | * |
||
489 | * @param ResponseInterface $response |
||
490 | * |
||
491 | * @return \EasyWeChat\Support\Collection |
||
492 | */ |
||
493 | 9 | protected function parseResponse($response) |
|
501 | |||
502 | /** |
||
503 | * Wrap API. |
||
504 | * |
||
505 | * @param string $resource |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | 8 | protected function wrapApi($resource) |
|
513 | |||
514 | /** |
||
515 | * Get sandbox sign key. |
||
516 | */ |
||
517 | protected function getSandboxSignKey() |
||
539 | |||
540 | /** |
||
541 | * Return the cache manager. |
||
542 | * |
||
543 | * @return \Doctrine\Common\Cache\Cache |
||
544 | */ |
||
545 | public function getCache() |
||
549 | } |
||
550 |