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 key. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $ticketCacheKey; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Ticket cache prefix. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $ticketCachePrefix = 'overtrue.wechat.card_api_ticket.'; |
||
| 52 | |||
| 53 | const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors'; |
||
| 54 | const API_CREATE_CARD = 'https://api.weixin.qq.com/card/create'; |
||
| 55 | const API_CREATE_QRCODE = 'https://api.weixin.qq.com/card/qrcode/create'; |
||
| 56 | const API_SHOW_QRCODE = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
||
| 57 | const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
||
| 58 | const API_CREATE_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create'; |
||
| 59 | const API_DEPOSIT_CODE = 'https://api.weixin.qq.com/card/code/deposit'; |
||
| 60 | const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount'; |
||
| 61 | const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode'; |
||
| 62 | const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml'; |
||
| 63 | const API_SET_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set'; |
||
| 64 | const API_GET_CODE = 'https://api.weixin.qq.com/card/code/get'; |
||
| 65 | const API_CONSUME_CARD = 'https://api.weixin.qq.com/card/code/consume'; |
||
| 66 | const API_DECRYPT_CODE = 'https://api.weixin.qq.com/card/code/decrypt'; |
||
| 67 | const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist'; |
||
| 68 | const API_GET_CARD = 'https://api.weixin.qq.com/card/get'; |
||
| 69 | const API_LIST_CARD = 'https://api.weixin.qq.com/card/batchget'; |
||
| 70 | const API_UPDATE_CARD = 'https://api.weixin.qq.com/card/update'; |
||
| 71 | const API_SET_PAY_CELL = 'https://api.weixin.qq.com/card/paycell/set'; |
||
| 72 | const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock'; |
||
| 73 | const API_UPDATE_CODE = 'https://api.weixin.qq.com/card/code/update'; |
||
| 74 | const API_DELETE_CARD = 'https://api.weixin.qq.com/card/delete'; |
||
| 75 | const API_DISABLE_CARD = 'https://api.weixin.qq.com/card/code/unavailable'; |
||
| 76 | const API_ACTIVATE_MEMBER_CARD = 'https://api.weixin.qq.com/card/membercard/activate'; |
||
| 77 | const API_ACTIVATE_MEMBER_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set'; |
||
| 78 | const API_GET_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get'; |
||
| 79 | const API_UPDATE_MEMBER_CARD_USER = 'https://api.weixin.qq.com/card/membercard/updateuser'; |
||
| 80 | const API_CREATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit'; |
||
| 81 | const API_UPDATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/update'; |
||
| 82 | 1 | const API_GET_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/get'; |
|
| 83 | const API_LIST_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/batchget'; |
||
| 84 | 1 | const API_GET_CATEGORIES = 'https://api.weixin.qq.com/card/getapplyprotocol'; |
|
| 85 | const API_ACTIVATE_GENERAL_CARD = 'https://api.weixin.qq.com/card/generalcard/activate'; |
||
| 86 | const API_UPDATE_GENERAL_CARD_USER = 'https://api.weixin.qq.com/card/generalcard/updateuser'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 获取卡券颜色. |
||
| 90 | * |
||
| 91 | * @return \EasyWeChat\Support\Collection |
||
| 92 | */ |
||
| 93 | public function getColors() |
||
| 97 | 1 | ||
| 98 | /** |
||
| 99 | * 创建卡券. |
||
| 100 | * |
||
| 101 | 1 | * @param string $cardType |
|
| 102 | 1 | * @param array $baseInfo |
|
| 103 | 1 | * @param array $especial |
|
| 104 | 1 | * @param array $advancedInfo |
|
| 105 | * |
||
| 106 | 1 | * @return \EasyWeChat\Support\Collection |
|
| 107 | */ |
||
| 108 | public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = []) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 创建二维码. |
||
| 122 | * |
||
| 123 | * @param array $cards |
||
| 124 | * |
||
| 125 | * @return \EasyWeChat\Support\Collection |
||
| 126 | */ |
||
| 127 | public function QRCode(array $cards = []) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * ticket 换取二维码图片. |
||
| 134 | * |
||
| 135 | * @param string $ticket |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function showQRCode($ticket = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * 通过ticket换取二维码 链接. |
||
| 161 | * |
||
| 162 | * @param string $ticket |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getQRCodeUrl($ticket) |
||
| 170 | |||
| 171 | 2 | /** |
|
| 172 | * 获取 卡券 Api_ticket. |
||
| 173 | 2 | * |
|
| 174 | 1 | * @param bool $refresh 是否强制刷新 |
|
| 175 | * |
||
| 176 | 1 | * @return string $apiTicket |
|
| 177 | */ |
||
| 178 | 1 | View Code Duplication | public function getAPITicket($refresh = false) |
| 194 | 1 | ||
| 195 | 1 | /** |
|
| 196 | * 微信卡券:JSAPI 卡券发放. |
||
| 197 | * |
||
| 198 | * @param array $cards |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function jsConfigForAssign(array $cards) |
||
| 208 | 1 | ||
| 209 | /** |
||
| 210 | 1 | * 生成 js添加到卡包 需要的 card_list 项. |
|
| 211 | 1 | * |
|
| 212 | 1 | * @param string $cardId |
|
| 213 | 1 | * @param array $extension |
|
| 214 | 1 | * |
|
| 215 | 1 | * @return string |
|
| 216 | 1 | */ |
|
| 217 | 1 | public function attachExtension($cardId, array $extension = []) |
|
| 241 | 1 | ||
| 242 | /** |
||
| 243 | * 生成签名. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getSignature() |
||
| 254 | |||
| 255 | /** |
||
| 256 | 1 | * 创建货架接口. |
|
| 257 | * |
||
| 258 | * @param string $banner |
||
| 259 | 1 | * @param string $pageTitle |
|
| 260 | 1 | * @param bool $canShare |
|
| 261 | 1 | * @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
|
| 262 | 1 | * SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
|
| 263 | 1 | * @param array $cardList |
|
| 264 | 1 | * |
|
| 265 | * @return \EasyWeChat\Support\Collection |
||
| 266 | 1 | */ |
|
| 267 | public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
||
| 279 | |||
| 280 | 1 | /** |
|
| 281 | 1 | * 导入code接口. |
|
| 282 | 1 | * |
|
| 283 | * @param string $cardId |
||
| 284 | 1 | * @param array $code |
|
| 285 | * |
||
| 286 | * @return \EasyWeChat\Support\Collection |
||
| 287 | */ |
||
| 288 | View Code Duplication | public function deposit($cardId, $code) |
|
| 297 | 1 | ||
| 298 | 1 | /** |
|
| 299 | * 查询导入code数目. |
||
| 300 | 1 | * |
|
| 301 | * @param string $cardId |
||
| 302 | * |
||
| 303 | * @return \EasyWeChat\Support\Collection |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function getDepositedCount($cardId) |
|
| 313 | |||
| 314 | 1 | /** |
|
| 315 | 1 | * 核查code接口. |
|
| 316 | 1 | * |
|
| 317 | * @param string $cardId |
||
| 318 | 1 | * @param array $code |
|
| 319 | * |
||
| 320 | * @return \EasyWeChat\Support\Collection |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function checkCode($cardId, $code) |
|
| 331 | |||
| 332 | /** |
||
| 333 | 1 | * 查询Code接口. |
|
| 334 | 1 | * |
|
| 335 | 1 | * @param string $code |
|
| 336 | 1 | * @param bool $checkConsume |
|
| 337 | * @param string $cardId |
||
| 338 | 1 | * |
|
| 339 | * @return \EasyWeChat\Support\Collection |
||
| 340 | */ |
||
| 341 | View Code Duplication | public function getCode($code, $checkConsume, $cardId) |
|
| 351 | 1 | ||
| 352 | 1 | /** |
|
| 353 | 1 | * 核销Code接口. |
|
| 354 | * |
||
| 355 | * @param string $code |
||
| 356 | 1 | * @param string $cardId |
|
| 357 | 1 | * |
|
| 358 | * @return \EasyWeChat\Support\Collection |
||
| 359 | 1 | */ |
|
| 360 | 1 | public function consume($code, $cardId = null) |
|
| 376 | 1 | ||
| 377 | 1 | /** |
|
| 378 | * Code解码接口. |
||
| 379 | 1 | * |
|
| 380 | * @param string $encryptedCode |
||
| 381 | * |
||
| 382 | * @return \EasyWeChat\Support\Collection |
||
| 383 | */ |
||
| 384 | public function decryptCode($encryptedCode) |
||
| 392 | 1 | ||
| 393 | 1 | /** |
|
| 394 | * 图文消息群发卡券. |
||
| 395 | 1 | * |
|
| 396 | * @param string $cardId |
||
| 397 | * |
||
| 398 | * @return \EasyWeChat\Support\Collection |
||
| 399 | */ |
||
| 400 | View Code Duplication | public function getHtml($cardId) |
|
| 408 | 1 | ||
| 409 | 1 | /** |
|
| 410 | * 设置测试白名单. |
||
| 411 | 1 | * |
|
| 412 | * @param array $openids |
||
| 413 | * |
||
| 414 | * @return \EasyWeChat\Support\Collection |
||
| 415 | */ |
||
| 416 | public function setTestWhitelist($openids) |
||
| 424 | 1 | ||
| 425 | 1 | /** |
|
| 426 | * 设置测试白名单(by username). |
||
| 427 | 1 | * |
|
| 428 | * @param array $usernames |
||
| 429 | * |
||
| 430 | * @return \EasyWeChat\Support\Collection |
||
| 431 | */ |
||
| 432 | public function setTestWhitelistByUsername($usernames) |
||
| 440 | |||
| 441 | 1 | /** |
|
| 442 | 1 | * 获取用户已领取卡券接口. |
|
| 443 | 1 | * |
|
| 444 | * @param string $openid |
||
| 445 | 1 | * @param string $cardId |
|
| 446 | * |
||
| 447 | * @return \EasyWeChat\Support\Collection |
||
| 448 | */ |
||
| 449 | View Code Duplication | public function getUserCards($openid, $cardId = '') |
|
| 458 | 1 | ||
| 459 | 1 | /** |
|
| 460 | * 查看卡券详情. |
||
| 461 | 1 | * |
|
| 462 | * @param string $cardId |
||
| 463 | * |
||
| 464 | * @return \EasyWeChat\Support\Collection |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function getCard($cardId) |
|
| 474 | |||
| 475 | /** |
||
| 476 | 1 | * 批量查询卡列表. |
|
| 477 | 1 | * |
|
| 478 | 1 | * @param int $offset |
|
| 479 | 1 | * @param int $count |
|
| 480 | * @param string $statusList |
||
| 481 | 1 | * |
|
| 482 | * @return \EasyWeChat\Support\Collection |
||
| 483 | */ |
||
| 484 | View Code Duplication | public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
|
| 494 | 1 | ||
| 495 | /** |
||
| 496 | 1 | * 更改卡券信息接口 and 设置跟随推荐接口. |
|
| 497 | 1 | * |
|
| 498 | 1 | * @param string $cardId |
|
| 499 | * @param string $type |
||
| 500 | 1 | * @param array $baseInfo |
|
| 501 | 1 | * @param array $especial |
|
| 502 | 1 | * |
|
| 503 | 1 | * @return \EasyWeChat\Support\Collection |
|
| 504 | */ |
||
| 505 | 1 | public function update($cardId, $type, $baseInfo = [], $especial = []) |
|
| 520 | |||
| 521 | /** |
||
| 522 | 1 | * 设置微信买单接口. |
|
| 523 | 1 | * 设置买单的 card_id 必须已经配置了门店,否则会报错. |
|
| 524 | 1 | * |
|
| 525 | * @param string $cardId |
||
| 526 | 1 | * @param bool $isOpen |
|
| 527 | * |
||
| 528 | * @return \EasyWeChat\Support\Collection |
||
| 529 | */ |
||
| 530 | View Code Duplication | public function setPayCell($cardId, $isOpen = true) |
|
| 539 | 1 | ||
| 540 | /** |
||
| 541 | * 增加库存. |
||
| 542 | * |
||
| 543 | * @param string $cardId |
||
| 544 | * @param int $amount |
||
| 545 | * |
||
| 546 | * @return \EasyWeChat\Support\Collection |
||
| 547 | */ |
||
| 548 | public function increaseStock($cardId, $amount) |
||
| 552 | 1 | ||
| 553 | /** |
||
| 554 | * 减少库存. |
||
| 555 | * |
||
| 556 | * @param string $cardId |
||
| 557 | * @param int $amount |
||
| 558 | * |
||
| 559 | * @return \EasyWeChat\Support\Collection |
||
| 560 | */ |
||
| 561 | public function reduceStock($cardId, $amount) |
||
| 565 | |||
| 566 | 1 | /** |
|
| 567 | * 修改库存接口. |
||
| 568 | 1 | * |
|
| 569 | 1 | * @param string $cardId |
|
| 570 | 1 | * @param int $amount |
|
| 571 | * @param string $action |
||
| 572 | 1 | * |
|
| 573 | * @return \EasyWeChat\Support\Collection |
||
| 574 | */ |
||
| 575 | protected function updateStock($cardId, $amount, $action = 'increase') |
||
| 585 | |||
| 586 | /** |
||
| 587 | 1 | * 更改Code接口. |
|
| 588 | 1 | * |
|
| 589 | 1 | * @param string $code |
|
| 590 | 1 | * @param string $newCode |
|
| 591 | * @param array $cardId |
||
| 592 | 1 | * |
|
| 593 | * @return \EasyWeChat\Support\Collection |
||
| 594 | */ |
||
| 595 | View Code Duplication | public function updateCode($code, $newCode, $cardId = []) |
|
| 605 | 1 | ||
| 606 | 1 | /** |
|
| 607 | * 删除卡券接口. |
||
| 608 | 1 | * |
|
| 609 | * @param string $cardId |
||
| 610 | * |
||
| 611 | * @return \EasyWeChat\Support\Collection |
||
| 612 | */ |
||
| 613 | View Code Duplication | public function delete($cardId) |
|
| 621 | |||
| 622 | 1 | /** |
|
| 623 | 1 | * 设置卡券失效. |
|
| 624 | 1 | * |
|
| 625 | * @param string $code |
||
| 626 | 1 | * @param string $cardId |
|
| 627 | * |
||
| 628 | * @return \EasyWeChat\Support\Collection |
||
| 629 | */ |
||
| 630 | View Code Duplication | public function disable($code, $cardId = '') |
|
| 639 | |||
| 640 | /** |
||
| 641 | * 会员卡接口激活. |
||
| 642 | * |
||
| 643 | * @param array $info |
||
| 644 | * |
||
| 645 | * @return \EasyWeChat\Support\Collection |
||
| 646 | */ |
||
| 647 | public function activate($info = [], $cardType = 'member_card') |
||
| 655 | |||
| 656 | /** |
||
| 657 | * 设置开卡字段接口. |
||
| 658 | * |
||
| 659 | * @param string $cardId |
||
| 660 | * @param array $requiredForm |
||
| 661 | * @param array $optionalForm |
||
| 662 | * |
||
| 663 | * @return \EasyWeChat\Support\Collection |
||
| 664 | */ |
||
| 665 | 1 | public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
|
| 671 | |||
| 672 | 1 | /** |
|
| 673 | * 拉取会员信息接口. |
||
| 674 | * |
||
| 675 | * @param string $cardId |
||
| 676 | * @param string $code |
||
| 677 | * |
||
| 678 | * @return \EasyWeChat\Support\Collection |
||
| 679 | */ |
||
| 680 | View Code Duplication | public function getMemberCardUser($cardId, $code) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * 更新会员信息. |
||
| 692 | * |
||
| 693 | * @param array $params |
||
| 694 | 1 | * |
|
| 695 | * @return \EasyWeChat\Support\Collection |
||
| 696 | */ |
||
| 697 | 1 | public function updateMemberCardUser(array $params = []) |
|
| 701 | 1 | ||
| 702 | 1 | /** |
|
| 703 | 1 | * 更新通用员信息. |
|
| 704 | 1 | * |
|
| 705 | 1 | * @param array $params |
|
| 706 | 1 | * |
|
| 707 | 1 | * @return \EasyWeChat\Support\Collection |
|
| 708 | 1 | */ |
|
| 709 | public function updateGeneralCardUser(array $params = []) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * 添加子商户. |
||
| 716 | * |
||
| 717 | * @param array $info |
||
| 718 | * |
||
| 719 | * @return \EasyWeChat\Support\Collection |
||
| 720 | */ |
||
| 721 | 1 | View Code Duplication | public function createSubMerchant(array $info = []) |
| 739 | |||
| 740 | /** |
||
| 741 | * 更新子商户. |
||
| 742 | * |
||
| 743 | * @param int $merchantId |
||
| 744 | * @param array $info |
||
| 745 | * |
||
| 746 | * @return \EasyWeChat\Support\Collection |
||
| 747 | */ |
||
| 748 | View Code Duplication | public function updateSubMerchant($merchantId, array $info = []) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * 获取子商户信息. |
||
| 770 | * |
||
| 771 | * @param int $merchantId |
||
| 772 | * |
||
| 773 | * @return \EasyWeChat\Support\Collection |
||
| 774 | */ |
||
| 775 | public function getSubMerchant($merchantId) |
||
| 779 | |||
| 780 | 1 | /** |
|
| 781 | * 批量获取子商户信息. |
||
| 782 | * |
||
| 783 | * @param int $beginId |
||
| 784 | * @param int $limit |
||
| 785 | * @param string $status |
||
| 786 | * |
||
| 787 | * @return \EasyWeChat\Support\Collection |
||
| 788 | */ |
||
| 789 | public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
||
| 799 | |||
| 800 | /** |
||
| 801 | * 卡券开放类目查询接口. |
||
| 802 | 2 | * |
|
| 803 | * @return \EasyWeChat\Support\Collection |
||
| 804 | 2 | */ |
|
| 805 | public function getCategories() |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Set cache manager. |
||
| 812 | * |
||
| 813 | * @param \Doctrine\Common\Cache\Cache $cache |
||
| 814 | 1 | * |
|
| 815 | * @return $this |
||
| 816 | 1 | */ |
|
| 817 | public function setCache(Cache $cache) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Return cache manager. |
||
| 826 | * |
||
| 827 | * @return \Doctrine\Common\Cache\Cache |
||
| 828 | */ |
||
| 829 | public function getCache() |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Set Api_ticket cache prifix. |
||
| 836 | * |
||
| 837 | * @param string $prefix |
||
| 838 | * |
||
| 839 | * @return $this |
||
| 840 | */ |
||
| 841 | public function setTicketCachePrefix($prefix) |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Set Api_ticket cache key. |
||
| 850 | * |
||
| 851 | * @param string $cacheKey |
||
| 852 | * |
||
| 853 | * @return $this |
||
| 854 | */ |
||
| 855 | public function setTicketCacheKey($cacheKey) |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Get Api_ticket token cache key. |
||
| 864 | * |
||
| 865 | * @return string $this->ticketCacheKey |
||
| 866 | */ |
||
| 867 | public function getTicketCacheKey() |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Set current url. |
||
| 878 | * |
||
| 879 | * @param string $url |
||
| 880 | * |
||
| 881 | * @return Card |
||
| 882 | */ |
||
| 883 | public function setUrl($url) |
||
| 889 | } |
||
| 890 |
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: