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 Card 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 Card, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Card extends AbstractAPI |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Cache. |
||
| 34 | * |
||
| 35 | * @var Cache |
||
| 36 | */ |
||
| 37 | protected $cache; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Ticket cache prefix. |
||
| 41 | */ |
||
| 42 | const TICKET_CACHE_PREFIX = 'overtrue.wechat.card_api_ticket.'; |
||
| 43 | |||
| 44 | const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors'; |
||
| 45 | const API_CREATE_CARD = 'https://api.weixin.qq.com/card/create'; |
||
| 46 | const API_CREATE_QRCODE = 'https://api.weixin.qq.com/card/qrcode/create'; |
||
| 47 | const API_SHOW_QRCODE = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
||
| 48 | const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
||
| 49 | const API_CREATE_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create'; |
||
| 50 | const API_DEPOSIT_CODE = 'https://api.weixin.qq.com/card/code/deposit'; |
||
| 51 | const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount'; |
||
| 52 | const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode'; |
||
| 53 | const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml'; |
||
| 54 | const API_SET_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set'; |
||
| 55 | const API_GET_CODE = 'https://api.weixin.qq.com/card/code/get'; |
||
| 56 | const API_CONSUME_CARD = 'https://api.weixin.qq.com/card/code/consume'; |
||
| 57 | const API_DECRYPT_CODE = 'https://api.weixin.qq.com/card/code/decrypt'; |
||
| 58 | const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist'; |
||
| 59 | const API_GET_CARD = 'https://api.weixin.qq.com/card/get'; |
||
| 60 | const API_LIST_CARD = 'https://api.weixin.qq.com/card/batchget'; |
||
| 61 | const API_UPDATE_CARD = 'https://api.weixin.qq.com/card/update'; |
||
| 62 | const API_SET_PAY_CELL = 'https://api.weixin.qq.com/card/paycell/set'; |
||
| 63 | const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock'; |
||
| 64 | const API_UPDATE_CODE = 'https://api.weixin.qq.com/card/code/update'; |
||
| 65 | const API_DELETE_CARD = 'https://api.weixin.qq.com/card/delete'; |
||
| 66 | const API_DISABLE_CARD = 'https://api.weixin.qq.com/card/code/unavailable'; |
||
| 67 | const API_ACTIVATE_MEMBER_CARD = 'https://api.weixin.qq.com/card/membercard/activate'; |
||
| 68 | const API_ACTIVATE_MEMBER_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set'; |
||
| 69 | const API_GET_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get'; |
||
| 70 | const API_UPDATE_MEMBER_CARD_USER = 'https://api.weixin.qq.com/card/membercard/updateuser'; |
||
| 71 | const API_CREATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit'; |
||
| 72 | const API_UPDATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/update'; |
||
| 73 | const API_GET_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/get'; |
||
| 74 | const API_LIST_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/batchget'; |
||
| 75 | const API_GET_CATEGORIES = 'https://api.weixin.qq.com/card/getapplyprotocol'; |
||
| 76 | const API_ACTIVATE_GENERAL_CARD = 'https://api.weixin.qq.com/card/generalcard/activate'; |
||
| 77 | const API_UPDATE_GENERAL_CARD_USER = 'https://api.weixin.qq.com/card/generalcard/updateuser'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * 获取卡券颜色. |
||
| 81 | * |
||
| 82 | * @return \EasyWeChat\Support\Collection |
||
| 83 | */ |
||
| 84 | 1 | public function getColors() |
|
| 85 | { |
||
| 86 | 1 | return $this->parseJSON('get', [self::API_GET_COLORS]); |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * 创建卡券. |
||
| 91 | * |
||
| 92 | * @param string $cardType |
||
| 93 | * @param array $baseInfo |
||
| 94 | * @param array $especial |
||
| 95 | * @param array $advancedInfo |
||
| 96 | * |
||
| 97 | * @return \EasyWeChat\Support\Collection |
||
| 98 | */ |
||
| 99 | 1 | public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = []) |
|
| 100 | { |
||
| 101 | $params = [ |
||
| 102 | 'card' => [ |
||
| 103 | 1 | 'card_type' => strtoupper($cardType), |
|
| 104 | 1 | strtolower($cardType) => array_merge(['base_info' => $baseInfo], $especial, $advancedInfo), |
|
| 105 | 1 | ], |
|
| 106 | 1 | ]; |
|
| 107 | |||
| 108 | 1 | return $this->parseJSON('json', [self::API_CREATE_CARD, $params]); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * 创建二维码. |
||
| 113 | * |
||
| 114 | * @param array $cards |
||
| 115 | * |
||
| 116 | * @return \EasyWeChat\Support\Collection |
||
| 117 | */ |
||
| 118 | 1 | public function QRCode(array $cards = []) |
|
| 119 | { |
||
| 120 | 1 | return $this->parseJSON('json', [self::API_CREATE_QRCODE, $cards]); |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * ticket 换取二维码图片. |
||
| 125 | * |
||
| 126 | * @param string $ticket |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function showQRCode($ticket = null) |
||
| 131 | { |
||
| 132 | $params = [ |
||
| 133 | 'ticket' => $ticket, |
||
| 134 | ]; |
||
| 135 | |||
| 136 | $http = $this->getHttp(); |
||
| 137 | |||
| 138 | /** @var ResponseInterface $response */ |
||
| 139 | $response = $http->get(self::API_SHOW_QRCODE, $params); |
||
| 140 | |||
| 141 | return [ |
||
| 142 | 'status' => $response->getStatusCode(), |
||
| 143 | 'reason' => $response->getReasonPhrase(), |
||
| 144 | 'headers' => $response->getHeaders(), |
||
| 145 | 'body' => strval($response->getBody()), |
||
| 146 | 'url' => self::API_SHOW_QRCODE.'?'.http_build_query($params), |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * 通过ticket换取二维码 链接. |
||
| 152 | * |
||
| 153 | * @param string $ticket |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 1 | public function getQRCodeUrl($ticket) |
|
| 158 | { |
||
| 159 | 1 | return self::API_SHOW_QRCODE.'?ticket='.$ticket; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * 获取 卡券 Api_ticket. |
||
| 164 | * |
||
| 165 | * @param bool $refresh 是否强制刷新 |
||
| 166 | * |
||
| 167 | * @return string $apiTicket |
||
| 168 | */ |
||
| 169 | 2 | View Code Duplication | public function getAPITicket($refresh = false) |
| 170 | { |
||
| 171 | 2 | $key = self::TICKET_CACHE_PREFIX.$this->getAccessToken()->getAppId(); |
|
| 172 | |||
| 173 | 2 | $ticket = $this->getCache()->fetch($key); |
|
| 174 | |||
| 175 | 2 | if (!$ticket || $refresh) { |
|
| 176 | 1 | $result = $this->parseJSON('get', [self::API_GET_CARD_TICKET, ['type' => 'wx_card']]); |
|
| 177 | |||
| 178 | 1 | $this->getCache()->save($key, $result['ticket'], $result['expires_in'] - 500); |
|
| 179 | |||
| 180 | 1 | return $result['ticket']; |
|
| 181 | } |
||
| 182 | |||
| 183 | 1 | return $ticket; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * 微信卡券:JSAPI 卡券发放. |
||
| 188 | * |
||
| 189 | * @param array $cards |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function jsConfigForAssign(array $cards) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * 生成 js添加到卡包 需要的 card_list 项. |
||
| 202 | * |
||
| 203 | * @param string $cardId |
||
| 204 | * @param array $extension |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | 1 | public function attachExtension($cardId, array $extension = []) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * 生成签名. |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 1 | public function getSignature() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * 创建货架接口. |
||
| 248 | * |
||
| 249 | * @param string $banner |
||
| 250 | * @param string $pageTitle |
||
| 251 | * @param bool $canShare |
||
| 252 | * @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
||
| 253 | * SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
||
| 254 | * @param array $cardList |
||
| 255 | * |
||
| 256 | * @return \EasyWeChat\Support\Collection |
||
| 257 | */ |
||
| 258 | 1 | public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * 导入code接口. |
||
| 273 | * |
||
| 274 | * @param string $cardId |
||
| 275 | * @param array $code |
||
| 276 | * |
||
| 277 | * @return \EasyWeChat\Support\Collection |
||
| 278 | */ |
||
| 279 | 1 | View Code Duplication | public function deposit($cardId, $code) |
| 288 | |||
| 289 | /** |
||
| 290 | * 查询导入code数目. |
||
| 291 | * |
||
| 292 | * @param string $cardId |
||
| 293 | * |
||
| 294 | * @return \EasyWeChat\Support\Collection |
||
| 295 | */ |
||
| 296 | 1 | View Code Duplication | public function getDepositedCount($cardId) |
| 304 | |||
| 305 | /** |
||
| 306 | * 核查code接口. |
||
| 307 | * |
||
| 308 | * @param string $cardId |
||
| 309 | * @param array $code |
||
| 310 | * |
||
| 311 | * @return \EasyWeChat\Support\Collection |
||
| 312 | */ |
||
| 313 | 1 | View Code Duplication | public function checkCode($cardId, $code) |
| 322 | |||
| 323 | /** |
||
| 324 | * 查询Code接口. |
||
| 325 | * |
||
| 326 | * @param string $code |
||
| 327 | * @param bool $checkConsume |
||
| 328 | * @param string $cardId |
||
| 329 | * |
||
| 330 | * @return \EasyWeChat\Support\Collection |
||
| 331 | */ |
||
| 332 | 1 | View Code Duplication | public function getCode($code, $checkConsume, $cardId) |
| 342 | |||
| 343 | /** |
||
| 344 | * 核销Code接口. |
||
| 345 | * |
||
| 346 | * @param string $code |
||
| 347 | * @param string $cardId |
||
| 348 | * |
||
| 349 | * @return \EasyWeChat\Support\Collection |
||
| 350 | */ |
||
| 351 | 1 | public function consume($code, $cardId = null) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Code解码接口. |
||
| 370 | * |
||
| 371 | * @param string $encryptedCode |
||
| 372 | * |
||
| 373 | * @return \EasyWeChat\Support\Collection |
||
| 374 | */ |
||
| 375 | 1 | public function decryptCode($encryptedCode) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * 图文消息群发卡券. |
||
| 386 | * |
||
| 387 | * @param string $cardId |
||
| 388 | * |
||
| 389 | * @return \EasyWeChat\Support\Collection |
||
| 390 | */ |
||
| 391 | 1 | View Code Duplication | public function getHtml($cardId) |
| 399 | |||
| 400 | /** |
||
| 401 | * 设置测试白名单. |
||
| 402 | * |
||
| 403 | * @param array $openids |
||
| 404 | * |
||
| 405 | * @return \EasyWeChat\Support\Collection |
||
| 406 | */ |
||
| 407 | 1 | public function setTestWhitelist($openids) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * 设置测试白名单(by username). |
||
| 418 | * |
||
| 419 | * @param array $usernames |
||
| 420 | * |
||
| 421 | * @return \EasyWeChat\Support\Collection |
||
| 422 | */ |
||
| 423 | 1 | public function setTestWhitelistByUsername($usernames) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * 获取用户已领取卡券接口. |
||
| 434 | * |
||
| 435 | * @param string $openid |
||
| 436 | * @param string $cardId |
||
| 437 | * |
||
| 438 | * @return \EasyWeChat\Support\Collection |
||
| 439 | */ |
||
| 440 | 1 | View Code Duplication | public function getUserCards($openid, $cardId = '') |
| 449 | |||
| 450 | /** |
||
| 451 | * 查看卡券详情. |
||
| 452 | * |
||
| 453 | * @param string $cardId |
||
| 454 | * |
||
| 455 | * @return \EasyWeChat\Support\Collection |
||
| 456 | */ |
||
| 457 | 1 | View Code Duplication | public function getCard($cardId) |
| 465 | |||
| 466 | /** |
||
| 467 | * 批量查询卡列表. |
||
| 468 | * |
||
| 469 | * @param int $offset |
||
| 470 | * @param int $count |
||
| 471 | * @param string $statusList |
||
| 472 | * |
||
| 473 | * @return \EasyWeChat\Support\Collection |
||
| 474 | */ |
||
| 475 | 1 | View Code Duplication | public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
| 485 | |||
| 486 | /** |
||
| 487 | * 更改卡券信息接口 and 设置跟随推荐接口. |
||
| 488 | * |
||
| 489 | * @param string $cardId |
||
| 490 | * @param string $type |
||
| 491 | * @param array $baseInfo |
||
| 492 | * @param array $especial |
||
| 493 | * |
||
| 494 | * @return \EasyWeChat\Support\Collection |
||
| 495 | */ |
||
| 496 | 1 | public function update($cardId, $type, $baseInfo = [], $especial = []) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * 设置微信买单接口. |
||
| 514 | * 设置买单的 card_id 必须已经配置了门店,否则会报错. |
||
| 515 | * |
||
| 516 | * @param string $cardId |
||
| 517 | * @param bool $isOpen |
||
| 518 | * |
||
| 519 | * @return \EasyWeChat\Support\Collection |
||
| 520 | */ |
||
| 521 | 1 | View Code Duplication | public function setPayCell($cardId, $isOpen = true) |
| 530 | |||
| 531 | /** |
||
| 532 | * 增加库存. |
||
| 533 | * |
||
| 534 | * @param string $cardId |
||
| 535 | * @param int $amount |
||
| 536 | * |
||
| 537 | * @return \EasyWeChat\Support\Collection |
||
| 538 | */ |
||
| 539 | 1 | public function increaseStock($cardId, $amount) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * 减少库存. |
||
| 546 | * |
||
| 547 | * @param string $cardId |
||
| 548 | * @param int $amount |
||
| 549 | * |
||
| 550 | * @return \EasyWeChat\Support\Collection |
||
| 551 | */ |
||
| 552 | 1 | public function reduceStock($cardId, $amount) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * 修改库存接口. |
||
| 559 | * |
||
| 560 | * @param string $cardId |
||
| 561 | * @param int $amount |
||
| 562 | * @param string $action |
||
| 563 | * |
||
| 564 | * @return \EasyWeChat\Support\Collection |
||
| 565 | */ |
||
| 566 | 1 | protected function updateStock($cardId, $amount, $action = 'increase') |
|
| 576 | |||
| 577 | /** |
||
| 578 | * 更改Code接口. |
||
| 579 | * |
||
| 580 | * @param string $code |
||
| 581 | * @param string $newCode |
||
| 582 | * @param array $cardId |
||
| 583 | * |
||
| 584 | * @return \EasyWeChat\Support\Collection |
||
| 585 | */ |
||
| 586 | 1 | View Code Duplication | public function updateCode($code, $newCode, $cardId = []) |
| 596 | |||
| 597 | /** |
||
| 598 | * 删除卡券接口. |
||
| 599 | * |
||
| 600 | * @param string $cardId |
||
| 601 | * |
||
| 602 | * @return \EasyWeChat\Support\Collection |
||
| 603 | */ |
||
| 604 | 1 | View Code Duplication | public function delete($cardId) |
| 612 | |||
| 613 | /** |
||
| 614 | * 设置卡券失效. |
||
| 615 | * |
||
| 616 | * @param string $code |
||
| 617 | * @param string $cardId |
||
| 618 | * |
||
| 619 | * @return \EasyWeChat\Support\Collection |
||
| 620 | */ |
||
| 621 | 1 | View Code Duplication | public function disable($code, $cardId = '') |
| 630 | |||
| 631 | /** |
||
| 632 | * 会员卡接口激活. |
||
| 633 | * |
||
| 634 | * @param array $info |
||
| 635 | * |
||
| 636 | * @return \EasyWeChat\Support\Collection |
||
| 637 | */ |
||
| 638 | 1 | public function activate($info = [], $cardType = 'member_card') |
|
| 646 | |||
| 647 | /** |
||
| 648 | * 设置开卡字段接口. |
||
| 649 | * |
||
| 650 | * @param string $cardId |
||
| 651 | * @param array $requiredForm |
||
| 652 | * @param array $optionalForm |
||
| 653 | * |
||
| 654 | * @return \EasyWeChat\Support\Collection |
||
| 655 | */ |
||
| 656 | 1 | public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * 拉取会员信息接口. |
||
| 665 | * |
||
| 666 | * @param string $cardId |
||
| 667 | * @param string $code |
||
| 668 | * |
||
| 669 | * @return \EasyWeChat\Support\Collection |
||
| 670 | */ |
||
| 671 | 1 | View Code Duplication | public function getMemberCardUser($cardId, $code) |
| 680 | |||
| 681 | /** |
||
| 682 | * 更新会员信息. |
||
| 683 | * |
||
| 684 | * @param array $params |
||
| 685 | * |
||
| 686 | * @return \EasyWeChat\Support\Collection |
||
| 687 | */ |
||
| 688 | 1 | public function updateMemberCardUser(array $params = []) |
|
| 692 | |||
| 693 | /** |
||
| 694 | * 更新通用员信息. |
||
| 695 | * |
||
| 696 | * @param array $params |
||
| 697 | * |
||
| 698 | * @return \EasyWeChat\Support\Collection |
||
| 699 | */ |
||
| 700 | public function updateGeneralCardUser(array $params = []) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * 添加子商户. |
||
| 707 | * |
||
| 708 | * @param array $info |
||
| 709 | * |
||
| 710 | * @return \EasyWeChat\Support\Collection |
||
| 711 | */ |
||
| 712 | 1 | View Code Duplication | public function createSubMerchant(array $info = []) |
| 730 | |||
| 731 | /** |
||
| 732 | * 更新子商户. |
||
| 733 | * |
||
| 734 | * @param int $merchantId |
||
| 735 | * @param array $info |
||
| 736 | * |
||
| 737 | * @return \EasyWeChat\Support\Collection |
||
| 738 | */ |
||
| 739 | 1 | View Code Duplication | public function updateSubMerchant($merchantId, array $info = []) |
| 758 | |||
| 759 | /** |
||
| 760 | * 获取子商户信息. |
||
| 761 | * |
||
| 762 | * @param int $merchantId |
||
| 763 | * |
||
| 764 | * @return \EasyWeChat\Support\Collection |
||
| 765 | */ |
||
| 766 | public function getSubMerchant($merchantId) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * 批量获取子商户信息. |
||
| 773 | * |
||
| 774 | * @param int $beginId |
||
| 775 | * @param int $limit |
||
| 776 | * @param string $status |
||
| 777 | * |
||
| 778 | * @return \EasyWeChat\Support\Collection |
||
| 779 | */ |
||
| 780 | public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
||
| 790 | |||
| 791 | /** |
||
| 792 | * 卡券开放类目查询接口. |
||
| 793 | * |
||
| 794 | * @return \EasyWeChat\Support\Collection |
||
| 795 | */ |
||
| 796 | 1 | public function getCategories() |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Set cache manager. |
||
| 803 | * |
||
| 804 | * @param \Doctrine\Common\Cache\Cache $cache |
||
| 805 | * |
||
| 806 | * @return $this |
||
| 807 | */ |
||
| 808 | 2 | public function setCache(Cache $cache) |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Return cache manager. |
||
| 817 | * |
||
| 818 | * @return \Doctrine\Common\Cache\Cache |
||
| 819 | */ |
||
| 820 | 2 | public function getCache() |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Set current url. |
||
| 827 | * |
||
| 828 | * @param string $url |
||
| 829 | * |
||
| 830 | * @return Card |
||
| 831 | */ |
||
| 832 | 1 | public function setUrl($url) |
|
| 838 | } |
||
| 839 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: