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 |
||
| 35 | class API extends AbstractAPI |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Merchant instance. |
||
| 39 | * |
||
| 40 | * @var Merchant |
||
| 41 | */ |
||
| 42 | protected $merchant; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sandbox box mode. |
||
| 46 | * |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $sandboxEnabled = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Sandbox sign key. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $sandboxSignKey; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Cache. |
||
| 60 | * |
||
| 61 | * @var Cache |
||
| 62 | */ |
||
| 63 | protected $cache; |
||
| 64 | |||
| 65 | const API_HOST = 'https://api.mch.weixin.qq.com'; |
||
| 66 | |||
| 67 | // api |
||
| 68 | const API_PAY_ORDER = '/pay/micropay'; |
||
| 69 | const API_PREPARE_ORDER = '/pay/unifiedorder'; |
||
| 70 | const API_QUERY = '/pay/orderquery'; |
||
| 71 | const API_CLOSE = '/pay/closeorder'; |
||
| 72 | const API_REVERSE = '/secapi/pay/reverse'; |
||
| 73 | const API_REFUND = '/secapi/pay/refund'; |
||
| 74 | const API_QUERY_REFUND = '/pay/refundquery'; |
||
| 75 | const API_DOWNLOAD_BILL = '/pay/downloadbill'; |
||
| 76 | const API_REPORT = '/payitil/report'; |
||
| 77 | |||
| 78 | const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl'; |
||
| 79 | const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid'; |
||
| 80 | const API_SANDBOX_SIGN_KEY = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; |
||
| 81 | |||
| 82 | // order id types. |
||
| 83 | const TRANSACTION_ID = 'transaction_id'; |
||
| 84 | const OUT_TRADE_NO = 'out_trade_no'; |
||
| 85 | const OUT_REFUND_NO = 'out_refund_no'; |
||
| 86 | const REFUND_ID = 'refund_id'; |
||
| 87 | |||
| 88 | // bill types. |
||
| 89 | const BILL_TYPE_ALL = 'ALL'; |
||
| 90 | const BILL_TYPE_SUCCESS = 'SUCCESS'; |
||
| 91 | const BILL_TYPE_REFUND = 'REFUND'; |
||
| 92 | const BILL_TYPE_REVOKED = 'REVOKED'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * API constructor. |
||
| 96 | * |
||
| 97 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 98 | * @param \EasyWeChat\Payment\Cache|null $cache |
||
| 99 | */ |
||
| 100 | 12 | public function __construct(Merchant $merchant, Cache $cache = null) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Pay the order. |
||
| 108 | * |
||
| 109 | * @param Order $order |
||
| 110 | * |
||
| 111 | * @return \EasyWeChat\Support\Collection |
||
| 112 | */ |
||
| 113 | 1 | public function pay(Order $order) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Prepare order to pay. |
||
| 120 | * |
||
| 121 | * @param Order $order |
||
| 122 | * |
||
| 123 | * @return \EasyWeChat\Support\Collection |
||
| 124 | */ |
||
| 125 | 1 | public function prepare(Order $order) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Query order. |
||
| 137 | * |
||
| 138 | * @param string $orderNo |
||
| 139 | * @param string $type |
||
| 140 | * |
||
| 141 | * @return \EasyWeChat\Support\Collection |
||
| 142 | */ |
||
| 143 | 1 | View Code Duplication | public function query($orderNo, $type = self::OUT_TRADE_NO) |
| 151 | |||
| 152 | /** |
||
| 153 | * Query order by transaction_id. |
||
| 154 | * |
||
| 155 | * @param string $transactionId |
||
| 156 | * |
||
| 157 | * @return \EasyWeChat\Support\Collection |
||
| 158 | */ |
||
| 159 | 1 | public function queryByTransactionId($transactionId) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Close order by out_trade_no. |
||
| 166 | * |
||
| 167 | * @param $tradeNo |
||
| 168 | * |
||
| 169 | * @return \EasyWeChat\Support\Collection |
||
| 170 | */ |
||
| 171 | 1 | public function close($tradeNo) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Reverse order. |
||
| 182 | * |
||
| 183 | * @param string $orderNo |
||
| 184 | * @param string $type |
||
| 185 | * |
||
| 186 | * @return \EasyWeChat\Support\Collection |
||
| 187 | */ |
||
| 188 | 1 | public function reverse($orderNo, $type = self::OUT_TRADE_NO) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Reverse order by transaction_id. |
||
| 199 | * |
||
| 200 | * @param int $transactionId |
||
| 201 | * |
||
| 202 | * @return \EasyWeChat\Support\Collection |
||
| 203 | */ |
||
| 204 | public function reverseByTransactionId($transactionId) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Make a refund request. |
||
| 211 | * |
||
| 212 | * @param string $orderNo |
||
| 213 | * @param string $refundNo |
||
| 214 | * @param float $totalFee |
||
| 215 | * @param float $refundFee |
||
| 216 | * @param string $opUserId |
||
| 217 | * @param string $type |
||
| 218 | * @param string $refundAccount |
||
| 219 | * @param string $refundReason |
||
| 220 | * |
||
| 221 | * @return Collection |
||
| 222 | */ |
||
| 223 | 1 | public function refund( |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Refund by transaction id. |
||
| 249 | * |
||
| 250 | * @param string $orderNo |
||
| 251 | * @param string $refundNo |
||
| 252 | * @param float $totalFee |
||
| 253 | * @param float $refundFee |
||
| 254 | * @param string $opUserId |
||
| 255 | * @param string $refundAccount |
||
| 256 | * @param string $refundReason |
||
| 257 | * |
||
| 258 | * @return Collection |
||
| 259 | */ |
||
| 260 | public function refundByTransactionId( |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Query refund status. |
||
| 274 | * |
||
| 275 | * @param string $orderNo |
||
| 276 | * @param string $type |
||
| 277 | * |
||
| 278 | * @return \EasyWeChat\Support\Collection |
||
| 279 | */ |
||
| 280 | 1 | View Code Duplication | public function queryRefund($orderNo, $type = self::OUT_TRADE_NO) |
| 288 | |||
| 289 | /** |
||
| 290 | * Query refund status by out_refund_no. |
||
| 291 | * |
||
| 292 | * @param string $refundNo |
||
| 293 | * |
||
| 294 | * @return \EasyWeChat\Support\Collection |
||
| 295 | */ |
||
| 296 | public function queryRefundByRefundNo($refundNo) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Query refund status by transaction_id. |
||
| 303 | * |
||
| 304 | * @param string $transactionId |
||
| 305 | * |
||
| 306 | * @return \EasyWeChat\Support\Collection |
||
| 307 | */ |
||
| 308 | public function queryRefundByTransactionId($transactionId) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Query refund status by refund_id. |
||
| 315 | * |
||
| 316 | * @param string $refundId |
||
| 317 | * |
||
| 318 | * @return \EasyWeChat\Support\Collection |
||
| 319 | */ |
||
| 320 | public function queryRefundByRefundId($refundId) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Download bill history as a table file. |
||
| 327 | * |
||
| 328 | * @param string $date |
||
| 329 | * @param string $type |
||
| 330 | * |
||
| 331 | * @return \Psr\Http\Message\ResponseInterface |
||
| 332 | */ |
||
| 333 | 1 | public function downloadBill($date, $type = self::BILL_TYPE_ALL) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Convert long url to short url. |
||
| 345 | * |
||
| 346 | * @param string $url |
||
| 347 | * |
||
| 348 | * @return \EasyWeChat\Support\Collection |
||
| 349 | */ |
||
| 350 | 1 | public function urlShorten($url) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Report API status to WeChat. |
||
| 357 | * |
||
| 358 | * @param string $api |
||
| 359 | * @param int $timeConsuming |
||
| 360 | * @param string $resultCode |
||
| 361 | * @param string $returnCode |
||
| 362 | * @param array $other ex: err_code,err_code_des,out_trade_no,user_ip... |
||
| 363 | * |
||
| 364 | * @return \EasyWeChat\Support\Collection |
||
| 365 | */ |
||
| 366 | public function report($api, $timeConsuming, $resultCode, $returnCode, array $other = []) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get openid by auth code. |
||
| 383 | * |
||
| 384 | * @param string $authCode |
||
| 385 | * |
||
| 386 | * @return \EasyWeChat\Support\Collection |
||
| 387 | */ |
||
| 388 | 1 | public function authCodeToOpenId($authCode) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Merchant setter. |
||
| 395 | * |
||
| 396 | * @param Merchant $merchant |
||
| 397 | * |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | 1 | public function setMerchant(Merchant $merchant) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Merchant getter. |
||
| 407 | * |
||
| 408 | * @return Merchant |
||
| 409 | */ |
||
| 410 | 1 | public function getMerchant() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Set sandbox mode. |
||
| 417 | * |
||
| 418 | * @param bool $enabled |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 10 | public function sandboxMode($enabled = false) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Make a API request. |
||
| 431 | * |
||
| 432 | * @param string $api |
||
| 433 | * @param array $params |
||
| 434 | * @param string $method |
||
| 435 | * @param array $options |
||
| 436 | * @param bool $returnResponse |
||
| 437 | * |
||
| 438 | * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface |
||
| 439 | */ |
||
| 440 | 10 | protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Return key to sign. |
||
| 463 | * |
||
| 464 | * @param string $api |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 10 | protected function getSignkey($api) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Request with SSL. |
||
| 475 | * |
||
| 476 | * @param string $api |
||
| 477 | * @param array $params |
||
| 478 | * @param string $method |
||
| 479 | * |
||
| 480 | * @return \EasyWeChat\Support\Collection |
||
| 481 | */ |
||
| 482 | 2 | protected function safeRequest($api, array $params, $method = 'post') |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Parse Response XML to array. |
||
| 494 | * |
||
| 495 | * @param ResponseInterface $response |
||
| 496 | * |
||
| 497 | * @return \EasyWeChat\Support\Collection |
||
| 498 | */ |
||
| 499 | 9 | protected function parseResponse($response) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Wrap API. |
||
| 510 | * |
||
| 511 | * @param string $resource |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | 8 | protected function wrapApi($resource) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Get sandbox sign key. |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | 2 | protected function getSandboxSignKey() |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Return the cache manager. |
||
| 557 | * |
||
| 558 | * @return \Doctrine\Common\Cache\Cache |
||
| 559 | */ |
||
| 560 | public function getCache() |
||
| 564 | } |
||
| 565 |