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 | 1 | * @return \EasyWeChat\Support\Collection |
|
83 | */ |
||
84 | 1 | public function getColors() |
|
88 | |||
89 | /** |
||
90 | * 创建卡券. |
||
91 | * |
||
92 | * @param string $cardType |
||
93 | * @param array $baseInfo |
||
94 | * @param array $especial |
||
95 | * @param array $advancedInfo |
||
96 | * |
||
97 | 1 | * @return \EasyWeChat\Support\Collection |
|
98 | */ |
||
99 | public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = []) |
||
110 | |||
111 | /** |
||
112 | * 创建二维码. |
||
113 | * |
||
114 | * @param array $cards |
||
115 | * |
||
116 | 1 | * @return \EasyWeChat\Support\Collection |
|
117 | */ |
||
118 | 1 | public function QRCode(array $cards = []) |
|
122 | |||
123 | /** |
||
124 | * ticket 换取二维码图片. |
||
125 | * |
||
126 | * @param string $ticket |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public function showQRCode($ticket = null) |
||
149 | |||
150 | /** |
||
151 | * 通过ticket换取二维码 链接. |
||
152 | * |
||
153 | * @param string $ticket |
||
154 | * |
||
155 | 1 | * @return string |
|
156 | */ |
||
157 | 1 | public function getQRCodeUrl($ticket) |
|
161 | |||
162 | /** |
||
163 | * 获取 卡券 Api_ticket. |
||
164 | * |
||
165 | * @param bool $refresh 是否强制刷新 |
||
166 | * |
||
167 | 2 | * @return string $apiTicket |
|
168 | */ |
||
169 | 2 | View Code Duplication | public function getAPITicket($refresh = false) |
185 | |||
186 | /** |
||
187 | * 微信卡券:JSAPI 卡券发放. |
||
188 | * |
||
189 | * @param array $cards |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 1 | public function jsConfigForAssign(array $cards) |
|
199 | |||
200 | /** |
||
201 | * 生成 js添加到卡包 需要的 card_list 项. |
||
202 | * |
||
203 | * @param string $cardId |
||
204 | * @param array $extension |
||
205 | * |
||
206 | 1 | * @return string |
|
207 | */ |
||
208 | 1 | public function attachExtension($cardId, array $extension = []) |
|
232 | |||
233 | /** |
||
234 | * 生成签名. |
||
235 | * |
||
236 | 1 | * @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 | 1 | * @return \EasyWeChat\Support\Collection |
|
257 | */ |
||
258 | public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
||
270 | |||
271 | /** |
||
272 | * 导入code接口. |
||
273 | * |
||
274 | * @param string $cardId |
||
275 | * @param array $code |
||
276 | * |
||
277 | 1 | * @return \EasyWeChat\Support\Collection |
|
278 | */ |
||
279 | View Code Duplication | public function deposit($cardId, $code) |
|
288 | |||
289 | /** |
||
290 | * 查询导入code数目. |
||
291 | * |
||
292 | * @param string $cardId |
||
293 | * |
||
294 | 1 | * @return \EasyWeChat\Support\Collection |
|
295 | */ |
||
296 | View Code Duplication | public function getDepositedCount($cardId) |
|
304 | |||
305 | /** |
||
306 | * 核查code接口. |
||
307 | * |
||
308 | * @param string $cardId |
||
309 | * @param array $code |
||
310 | * |
||
311 | 1 | * @return \EasyWeChat\Support\Collection |
|
312 | */ |
||
313 | 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 | 1 | * @return \EasyWeChat\Support\Collection |
|
331 | */ |
||
332 | View Code Duplication | public function getCode($code, $checkConsume, $cardId) |
|
342 | |||
343 | /** |
||
344 | * 核销Code接口. |
||
345 | * |
||
346 | * @param string $code |
||
347 | * @param string $cardId |
||
348 | * |
||
349 | 1 | * @return \EasyWeChat\Support\Collection |
|
350 | */ |
||
351 | 1 | public function consume($code, $cardId = null) |
|
367 | |||
368 | /** |
||
369 | * Code解码接口. |
||
370 | * |
||
371 | * @param string $encryptedCode |
||
372 | * |
||
373 | 1 | * @return \EasyWeChat\Support\Collection |
|
374 | */ |
||
375 | public function decryptCode($encryptedCode) |
||
383 | |||
384 | /** |
||
385 | * 图文消息群发卡券. |
||
386 | * |
||
387 | * @param string $cardId |
||
388 | * |
||
389 | 1 | * @return \EasyWeChat\Support\Collection |
|
390 | */ |
||
391 | View Code Duplication | public function getHtml($cardId) |
|
399 | |||
400 | /** |
||
401 | * 设置测试白名单. |
||
402 | * |
||
403 | * @param array $openids |
||
404 | * |
||
405 | 1 | * @return \EasyWeChat\Support\Collection |
|
406 | */ |
||
407 | public function setTestWhitelist($openids) |
||
415 | |||
416 | /** |
||
417 | * 设置测试白名单(by username). |
||
418 | * |
||
419 | * @param array $usernames |
||
420 | * |
||
421 | 1 | * @return \EasyWeChat\Support\Collection |
|
422 | */ |
||
423 | public function setTestWhitelistByUsername($usernames) |
||
431 | |||
432 | /** |
||
433 | * 获取用户已领取卡券接口. |
||
434 | * |
||
435 | * @param string $openid |
||
436 | * @param string $cardId |
||
437 | * |
||
438 | 1 | * @return \EasyWeChat\Support\Collection |
|
439 | */ |
||
440 | View Code Duplication | public function getUserCards($openid, $cardId = '') |
|
449 | |||
450 | /** |
||
451 | * 查看卡券详情. |
||
452 | * |
||
453 | * @param string $cardId |
||
454 | * |
||
455 | 1 | * @return \EasyWeChat\Support\Collection |
|
456 | */ |
||
457 | 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 | 1 | * @return \EasyWeChat\Support\Collection |
|
474 | */ |
||
475 | 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 | 1 | * @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 | 1 | * @return \EasyWeChat\Support\Collection |
|
520 | */ |
||
521 | View Code Duplication | public function setPayCell($cardId, $isOpen = true) |
|
530 | |||
531 | /** |
||
532 | * 增加库存. |
||
533 | * |
||
534 | * @param string $cardId |
||
535 | * @param int $amount |
||
536 | * |
||
537 | 1 | * @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 | 1 | * @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 | 1 | * @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 | 1 | * @return \EasyWeChat\Support\Collection |
|
585 | */ |
||
586 | View Code Duplication | public function updateCode($code, $newCode, $cardId = []) |
|
596 | |||
597 | /** |
||
598 | * 删除卡券接口. |
||
599 | * |
||
600 | * @param string $cardId |
||
601 | * |
||
602 | 1 | * @return \EasyWeChat\Support\Collection |
|
603 | */ |
||
604 | View Code Duplication | public function delete($cardId) |
|
612 | |||
613 | /** |
||
614 | * 设置卡券失效. |
||
615 | * |
||
616 | * @param string $code |
||
617 | * @param string $cardId |
||
618 | * |
||
619 | 1 | * @return \EasyWeChat\Support\Collection |
|
620 | */ |
||
621 | View Code Duplication | public function disable($code, $cardId = '') |
|
630 | |||
631 | /** |
||
632 | * 会员卡接口激活. |
||
633 | * |
||
634 | * @param array $info |
||
635 | * |
||
636 | 1 | * @return \EasyWeChat\Support\Collection |
|
637 | */ |
||
638 | 1 | public function activate($info = [], $cardType = 'member_card') |
|
639 | { |
||
640 | if ($cardType === 'general_card') { |
||
641 | return $this->parseJSON('json', [self::API_ACTIVATE_GENERAL_CARD, $info]); |
||
642 | } |
||
643 | |||
644 | return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_CARD, $info]); |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * 设置开卡字段接口. |
||
649 | * |
||
650 | 1 | * @param string $cardId |
|
651 | * @param array $requiredForm |
||
652 | 1 | * @param array $optionalForm |
|
653 | * |
||
654 | 1 | * @return \EasyWeChat\Support\Collection |
|
655 | */ |
||
656 | public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
||
662 | |||
663 | /** |
||
664 | * 拉取会员信息接口. |
||
665 | 1 | * |
|
666 | * @param string $cardId |
||
667 | * @param string $code |
||
668 | 1 | * |
|
669 | 1 | * @return \EasyWeChat\Support\Collection |
|
670 | 1 | */ |
|
671 | View Code Duplication | public function getMemberCardUser($cardId, $code) |
|
680 | |||
681 | /** |
||
682 | 1 | * 更新会员信息. |
|
683 | * |
||
684 | 1 | * @param array $params |
|
685 | * |
||
686 | * @return \EasyWeChat\Support\Collection |
||
687 | */ |
||
688 | public function updateMemberCardUser(array $params = []) |
||
692 | |||
693 | /** |
||
694 | 1 | * 更新通用员信息. |
|
695 | * |
||
696 | * @param array $params |
||
697 | 1 | * |
|
698 | 1 | * @return \EasyWeChat\Support\Collection |
|
699 | 1 | */ |
|
700 | 1 | public function updateGeneralCardUser(array $params = []) |
|
704 | 1 | ||
705 | 1 | /** |
|
706 | 1 | * 添加子商户. |
|
707 | 1 | * |
|
708 | 1 | * @param array $info |
|
709 | * |
||
710 | 1 | * @return \EasyWeChat\Support\Collection |
|
711 | */ |
||
712 | View Code Duplication | public function createSubMerchant(array $info = []) |
|
730 | 1 | ||
731 | 1 | /** |
|
732 | 1 | * 更新子商户. |
|
733 | 1 | * |
|
734 | 1 | * @param int $merchantId |
|
735 | 1 | * @param array $info |
|
736 | 1 | * |
|
737 | * @return \EasyWeChat\Support\Collection |
||
738 | 1 | */ |
|
739 | 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 | 1 | * @return \EasyWeChat\Support\Collection |
|
779 | */ |
||
780 | 1 | public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
|
790 | 2 | ||
791 | /** |
||
792 | 2 | * 卡券开放类目查询接口. |
|
793 | * |
||
794 | 2 | * @return \EasyWeChat\Support\Collection |
|
795 | */ |
||
796 | public function getCategories() |
||
800 | |||
801 | /** |
||
802 | 2 | * Set cache manager. |
|
803 | * |
||
804 | 2 | * @param \Doctrine\Common\Cache\Cache $cache |
|
805 | * |
||
806 | * @return $this |
||
807 | */ |
||
808 | public function setCache(Cache $cache) |
||
814 | 1 | ||
815 | /** |
||
816 | 1 | * Return cache manager. |
|
817 | * |
||
818 | 1 | * @return \Doctrine\Common\Cache\Cache |
|
819 | */ |
||
820 | public function getCache() |
||
824 | |||
825 | /** |
||
826 | * Set current url. |
||
827 | * |
||
828 | * @param string $url |
||
829 | * |
||
830 | * @return Card |
||
831 | */ |
||
832 | public function setUrl($url) |
||
838 | } |
||
839 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: