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 |
||
| 28 | class Card extends AbstractAPI |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Cache. |
||
| 32 | * |
||
| 33 | * @var Cache |
||
| 34 | */ |
||
| 35 | protected $cache; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Ticket cache prefix. |
||
| 39 | */ |
||
| 40 | const TICKET_CACHE_PREFIX = 'overtrue.wechat.card_api_ticket.'; |
||
| 41 | |||
| 42 | const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors'; |
||
| 43 | const API_CREATE = 'https://api.weixin.qq.com/card/create'; |
||
| 44 | const API_QRCODE_CREATE = 'https://api.weixin.qq.com/card/qrcode/create'; |
||
| 45 | const API_QRCODE_SHOW = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
||
| 46 | const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
||
| 47 | const API_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create'; |
||
| 48 | const API_DEPOSIT = 'https://api.weixin.qq.com/card/code/deposit'; |
||
| 49 | const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount'; |
||
| 50 | const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode'; |
||
| 51 | const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml'; |
||
| 52 | const API_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set'; |
||
| 53 | const API_CODE_GET = 'https://api.weixin.qq.com/card/code/get'; |
||
| 54 | const API_CONSUME = 'https://api.weixin.qq.com/card/code/consume'; |
||
| 55 | const API_DECRYPT = 'https://api.weixin.qq.com/card/code/decrypt'; |
||
| 56 | const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist'; |
||
| 57 | const API_CARD_GET = 'https://api.weixin.qq.com/card/get'; |
||
| 58 | const API_BATCH_GET = 'https://api.weixin.qq.com/card/batchget'; |
||
| 59 | const API_UPDATE = 'https://api.weixin.qq.com/card/update'; |
||
| 60 | const API_PAY_CELL_SET = 'https://api.weixin.qq.com/card/paycell/set'; |
||
| 61 | const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock'; |
||
| 62 | const API_CODE_UPDATE = 'https://api.weixin.qq.com/card/code/update'; |
||
| 63 | const API_CARD_DELETE = 'https://api.weixin.qq.com/card/delete'; |
||
| 64 | const API_UNAVAILABLE = 'https://api.weixin.qq.com/card/code/unavailable'; |
||
| 65 | const API_CARD_BIZ_UIN_INFO = 'https://api.weixin.qq.com/datacube/getcardbizuininfo'; |
||
| 66 | const API_CARD_CARD_INFO = 'https://api.weixin.qq.com/datacube/getcardcardinfo'; |
||
| 67 | const API_CARD_MEMBER_CARD_INFO = 'https://api.weixin.qq.com/datacube/getcardmembercardinfo'; |
||
| 68 | const API_CARD_ACTIVATE = 'https://api.weixin.qq.com/card/membercard/activate'; |
||
| 69 | const API_ACTIVATE_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set'; |
||
| 70 | const API_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get'; |
||
| 71 | const API_UPDATE_USER = 'https://api.weixin.qq.com/card/membercard/updateuser'; |
||
| 72 | const API_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit'; |
||
| 73 | const API_GET_APPLY_PROTOCOL = 'https://api.weixin.qq.com/card/getapplyprotocol'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * 获取卡券颜色. |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | 1 | public function getColors() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * 创建卡券. |
||
| 87 | * |
||
| 88 | * @param string $cardType |
||
| 89 | * @param array $baseInfo |
||
| 90 | * @param array $especial |
||
| 91 | * @param array $advancedInfo |
||
| 92 | * |
||
| 93 | * @return bool|array |
||
| 94 | */ |
||
| 95 | 1 | public function create($cardType = 'member_card', $baseInfo = [], $especial = [], $advancedInfo = []) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * 创建二维码. |
||
| 118 | * |
||
| 119 | * @param array $cardList |
||
| 120 | * |
||
| 121 | * @return array|bool |
||
| 122 | */ |
||
| 123 | 1 | public function qrCode($cardList = []) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * ticket 换取二维码图片. |
||
| 130 | * |
||
| 131 | * @param null $ticket |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 1 | public function showQrCode($ticket = null) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * 通过ticket换取二维码 链接. |
||
| 157 | * |
||
| 158 | * @param $ticket |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | 1 | public function showQrCodeUrl($ticket) |
|
| 163 | { |
||
| 164 | 1 | $params = '?ticket='.$ticket; |
|
| 165 | |||
| 166 | 1 | return self::API_QRCODE_SHOW.$params; |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * 获取 卡券 Api_ticket. |
||
| 171 | * |
||
| 172 | * @param bool $isRefresh 是否强制刷新 |
||
| 173 | * |
||
| 174 | * @return string $apiTicket |
||
| 175 | */ |
||
| 176 | 2 | public function cardApiTicket($isRefresh = false) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * 微信卡券:JSAPI 卡券Package - 基础参数没有附带任何值 - 再生产环境中需要根据实际情况进行修改. |
||
| 195 | * |
||
| 196 | * @param array $cardList |
||
| 197 | * @param null $timestamp |
||
| 198 | * @param null $apiTicket |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 1 | public function wxCardPackage(array $cardList, $timestamp = null, $apiTicket = null) |
|
| 203 | { |
||
| 204 | 1 | if (empty($timestamp) || $timestamp === '') { |
|
| 205 | 1 | $timestamp = time(); |
|
| 206 | 1 | } |
|
| 207 | |||
| 208 | 1 | if (empty($apiTicket) || $apiTicket === '') { |
|
| 209 | 1 | $apiTicket = $this->cardApiTicket(); |
|
| 210 | 1 | } |
|
| 211 | |||
| 212 | 1 | $resultArray = []; |
|
| 213 | 1 | foreach ($cardList as $key => $value) { |
|
| 214 | 1 | View Code Duplication | if (empty($value['code']) || !isset($value['code'])) { |
| 215 | 1 | $value['code'] = ''; |
|
| 216 | 1 | } |
|
| 217 | |||
| 218 | 1 | View Code Duplication | if (empty($value['openid']) || !isset($value['openid'])) { |
| 219 | 1 | $value['openid'] = ''; |
|
| 220 | 1 | } |
|
| 221 | |||
| 222 | 1 | $arrays = [$apiTicket, $timestamp, $value['card_id'], $value['code'], $value['openid']]; |
|
| 223 | 1 | sort($arrays, SORT_STRING); |
|
| 224 | 1 | $string = sha1(implode($arrays)); |
|
| 225 | |||
| 226 | 1 | $resultArray['cardList'][$key]['cardId'] = $value['card_id']; |
|
| 227 | 1 | $resultArray['cardList'][$key]['cardExt']['code'] = $value['code']; |
|
| 228 | 1 | $resultArray['cardList'][$key]['cardExt']['openid'] = $value['openid']; |
|
| 229 | |||
| 230 | 1 | $resultArray['cardList'][$key]['cardExt']['timestamp'] = $timestamp; |
|
| 231 | 1 | $resultArray['cardList'][$key]['cardExt']['signature'] = $string; |
|
| 232 | |||
| 233 | 1 | if (!empty($value['outer_id'])) { |
|
| 234 | 1 | $resultArray['cardList'][$key]['cardExt']['outer_id'] = $value['outer_id']; |
|
| 235 | 1 | } |
|
| 236 | |||
| 237 | 1 | $resultArray['cardList'][$key]['cardExt'] = json_encode($resultArray['cardList'][$key]['cardExt']); |
|
| 238 | 1 | } |
|
| 239 | |||
| 240 | 1 | $resultJson = json_encode($resultArray); |
|
| 241 | |||
| 242 | 1 | return $resultJson; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * 创建货架接口. |
||
| 247 | * |
||
| 248 | * @param $banner |
||
| 249 | * @param $pageTitle |
||
| 250 | * @param $canShare |
||
| 251 | * @param $scene |
||
| 252 | * @param $cardList |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | 1 | public function landingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * 导入code接口. |
||
| 271 | * |
||
| 272 | * @param $cardId |
||
| 273 | * @param $code |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | 1 | View Code Duplication | public function deposit($cardId, $code) |
| 286 | |||
| 287 | /** |
||
| 288 | * 查询导入code数目. |
||
| 289 | * |
||
| 290 | * @param $cardId |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | 1 | View Code Duplication | public function getDepositCount($cardId) |
| 302 | |||
| 303 | /** |
||
| 304 | * 核查code接口. |
||
| 305 | * |
||
| 306 | * @param $cardId |
||
| 307 | * @param $code |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 1 | View Code Duplication | public function checkCode($cardId, $code) |
| 320 | |||
| 321 | /** |
||
| 322 | * 图文消息群发卡券. |
||
| 323 | * |
||
| 324 | * @param $cardId |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | 1 | View Code Duplication | public function getHtml($cardId) |
| 336 | |||
| 337 | /** |
||
| 338 | * 设置测试白名单. |
||
| 339 | * |
||
| 340 | * @param $openid |
||
| 341 | * @param $username |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | 1 | public function testWhiteList($openid, $username) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * 查询Code接口. |
||
| 357 | * |
||
| 358 | * @param $code |
||
| 359 | * @param $checkConsume |
||
| 360 | * @param $cardId |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | 1 | View Code Duplication | public function codeGet($code, $checkConsume, $cardId) |
| 374 | |||
| 375 | /** |
||
| 376 | * 核销Code接口. |
||
| 377 | * |
||
| 378 | * @param $cardId |
||
| 379 | * @param $code |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | 1 | View Code Duplication | public function consume($cardId, $code) |
| 392 | |||
| 393 | /** |
||
| 394 | * Code解码接口. |
||
| 395 | * |
||
| 396 | * @param $encryptCode |
||
| 397 | * |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | 1 | public function decrypt($encryptCode) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * 获取用户已领取卡券接口. |
||
| 411 | * |
||
| 412 | * @param $openid |
||
| 413 | * @param string $cardId |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 1 | View Code Duplication | public function getCardList($openid, $cardId = '') |
| 426 | |||
| 427 | /** |
||
| 428 | * 查看卡券详情. |
||
| 429 | * |
||
| 430 | * @param $cardId |
||
| 431 | * |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | 1 | View Code Duplication | public function cardGet($cardId) |
| 442 | |||
| 443 | /** |
||
| 444 | * 批量查询卡列表. |
||
| 445 | * |
||
| 446 | * @param int $offset |
||
| 447 | * @param int $count |
||
| 448 | * @param string $statusList |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 1 | View Code Duplication | public function batchGet($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
| 462 | |||
| 463 | /** |
||
| 464 | * 更改卡券信息接口 and 设置跟随推荐接口. |
||
| 465 | * |
||
| 466 | * @param $cardId |
||
| 467 | * @param $type |
||
| 468 | * @param array $baseInfo |
||
| 469 | * @param array $especial |
||
| 470 | * |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | 1 | public function update($cardId, $type, $baseInfo = [], $especial = []) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * 设置微信买单接口. |
||
| 489 | * 设置买单的card_id必须已经配置了门店,否则会报错. |
||
| 490 | * |
||
| 491 | * @param $cardId |
||
| 492 | * @param bool $isOpen |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | 1 | View Code Duplication | public function payCellSet($cardId, $isOpen = true) |
| 505 | |||
| 506 | /** |
||
| 507 | * 修改库存接口. |
||
| 508 | * |
||
| 509 | * @param $cardId |
||
| 510 | * @param string $stock |
||
| 511 | * @param int $value |
||
| 512 | * |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | 1 | public function modifyStock($cardId, $stock = 'increase', $value = 0) |
|
| 516 | { |
||
| 517 | 1 | $params = []; |
|
| 518 | 1 | $params['card_id'] = $cardId; |
|
| 519 | 1 | if ($stock === 'increase') { |
|
| 520 | 1 | $params['increase_stock_value'] = intval($value); |
|
| 521 | 1 | } elseif ($stock === 'reduce') { |
|
| 522 | $params['reduce_stock_value'] = intval($value); |
||
| 523 | } else { |
||
| 524 | return false; |
||
| 525 | } |
||
| 526 | |||
| 527 | 1 | return $this->parseJSON('json', [self::API_MODIFY_STOCK, $params]); |
|
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * 更改Code接口. |
||
| 532 | * |
||
| 533 | * @param $code |
||
| 534 | * @param $newCode |
||
| 535 | * @param array $cardId |
||
| 536 | * |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | 1 | View Code Duplication | public function codeUpdate($code, $newCode, $cardId = []) |
| 549 | |||
| 550 | /** |
||
| 551 | * 删除卡券接口. |
||
| 552 | * |
||
| 553 | * @param $cardId |
||
| 554 | * |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | 1 | View Code Duplication | public function cardDelete($cardId) |
| 565 | |||
| 566 | /** |
||
| 567 | * 设置卡券失效. |
||
| 568 | * |
||
| 569 | * @param $code |
||
| 570 | * @param null $cardId |
||
| 571 | * |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | 1 | View Code Duplication | public function unavailable($code, $cardId = null) |
| 583 | |||
| 584 | /** |
||
| 585 | * 拉取卡券概况数据接口. |
||
| 586 | * |
||
| 587 | * @param $beginDate |
||
| 588 | * @param $endDate |
||
| 589 | * @param int $condSource |
||
| 590 | * |
||
| 591 | * @return array |
||
| 592 | */ |
||
| 593 | 1 | public function getCardBizUinInfo($beginDate, $endDate, $condSource = 0) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * 获取免费券数据接口. |
||
| 614 | * |
||
| 615 | * @param $beginDate |
||
| 616 | * @param $endDate |
||
| 617 | * @param int $condSource |
||
| 618 | * @param string $cardId |
||
| 619 | * |
||
| 620 | * @return array |
||
| 621 | */ |
||
| 622 | 1 | View Code Duplication | public function getCardCardInfo($beginDate, $endDate, $condSource = 0, $cardId = '') |
| 633 | |||
| 634 | /** |
||
| 635 | * 拉取会员卡数据接口. |
||
| 636 | * |
||
| 637 | * @param $beginDate |
||
| 638 | * @param $endDate |
||
| 639 | * @param int $condSource |
||
| 640 | * |
||
| 641 | * @return array |
||
| 642 | */ |
||
| 643 | 1 | View Code Duplication | public function getCardMemberCardInfo($beginDate, $endDate, $condSource = 0) |
| 653 | |||
| 654 | /** |
||
| 655 | * 会员卡接口激活. |
||
| 656 | * |
||
| 657 | * @param array $activate |
||
| 658 | * |
||
| 659 | * @return array |
||
| 660 | */ |
||
| 661 | 1 | public function activate($activate = []) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * 设置开卡字段接口. |
||
| 668 | * |
||
| 669 | * @param $cardId |
||
| 670 | * @param array $requiredForm |
||
| 671 | * @param array $optionalForm |
||
| 672 | * |
||
| 673 | * @return array |
||
| 674 | */ |
||
| 675 | 1 | public function activateUserFrom($cardId, $requiredForm = [], $optionalForm = []) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * 拉取会员信息接口. |
||
| 687 | * |
||
| 688 | * @param $cardId |
||
| 689 | * @param $code |
||
| 690 | * |
||
| 691 | * @return array |
||
| 692 | */ |
||
| 693 | 1 | View Code Duplication | public function memberCardUserInfo($cardId, $code) |
| 702 | |||
| 703 | /** |
||
| 704 | * 更新会员信息. |
||
| 705 | * |
||
| 706 | * @param array $updateUser |
||
| 707 | * |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | 1 | public function memberCardUpdateUser($updateUser = []) |
|
| 716 | |||
| 717 | /** |
||
| 718 | * 添加子商户. |
||
| 719 | * |
||
| 720 | * @param $brandName |
||
| 721 | * @param $logoUrl |
||
| 722 | * @param $protocol |
||
| 723 | * @param $endTime |
||
| 724 | * @param $primaryCategoryId |
||
| 725 | * @param $secondaryCategoryId |
||
| 726 | * @param $agreementMediaId |
||
| 727 | * @param $operatorMediaId |
||
| 728 | * @param string $appId |
||
| 729 | * |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | 1 | public function subMerchant($brandName, $logoUrl, $protocol, $endTime, $primaryCategoryId, $secondaryCategoryId, $agreementMediaId, $operatorMediaId, $appId = '') |
|
| 750 | |||
| 751 | /** |
||
| 752 | * 卡券开放类目查询接口. |
||
| 753 | * |
||
| 754 | * @return array|bool |
||
| 755 | */ |
||
| 756 | 1 | public function getApplyProtocol() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Set cache manager. |
||
| 763 | * |
||
| 764 | * @param \Doctrine\Common\Cache\Cache $cache |
||
| 765 | * |
||
| 766 | * @return $this |
||
| 767 | */ |
||
| 768 | 2 | public function setCache(Cache $cache) |
|
| 774 | |||
| 775 | /** |
||
| 776 | * Return cache manager. |
||
| 777 | * |
||
| 778 | * @return \Doctrine\Common\Cache\Cache |
||
| 779 | */ |
||
| 780 | 2 | public function getCache() |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Set current url. |
||
| 787 | * |
||
| 788 | * @param string $url |
||
| 789 | * |
||
| 790 | * @return array |
||
| 791 | */ |
||
| 792 | 1 | public function setUrl($url) |
|
| 798 | } |
||
| 799 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.