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 CardApi 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 CardApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class CardApi extends BaseApi |
||
12 | { |
||
13 | /** |
||
14 | * 获取卡券颜色 |
||
15 | * |
||
16 | * @return array [颜色列表] |
||
17 | */ |
||
18 | public function getcolors() |
||
29 | |||
30 | /** |
||
31 | * 创建卡券 |
||
32 | * |
||
33 | * @param string $type [卡券类型] |
||
34 | * @param array $base_info [必要字段] |
||
35 | * @param array $especial [特殊字段] |
||
36 | * @param array $advanced_info [图文列表] |
||
37 | * |
||
38 | * @return string [CardId] |
||
39 | */ |
||
40 | public function create($type = 'member_card', $base_info = [], $especial = [], $advanced_info = []) |
||
68 | |||
69 | /** |
||
70 | * 卡券二维码 |
||
71 | * |
||
72 | * @param $card |
||
73 | * |
||
74 | * @return array|bool |
||
75 | */ |
||
76 | View Code Duplication | public function qrcode($card) |
|
91 | |||
92 | /** |
||
93 | * 通过ticket换取二维码 |
||
94 | * |
||
95 | * @param string $ticket 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。 |
||
96 | * |
||
97 | * @return array|bool 是一张图片,可以直接展示或者下载 |
||
98 | */ |
||
99 | View Code Duplication | public function showqrcode($ticket = '') |
|
100 | { |
||
101 | Api::setApiUrl('https://mp.weixin.qq.com/'); |
||
102 | |||
103 | $this->apitype = 'cgi-bin'; |
||
104 | $this->module = 'showqrcode'; |
||
105 | |||
106 | $queryStr = ['ticket' => $ticket]; |
||
107 | |||
108 | $res = $this->_get('', $queryStr); |
||
109 | |||
110 | return $res; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * 通过ticket换取二维码 链接 |
||
115 | * |
||
116 | * @param string $ticket 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。 |
||
117 | * |
||
118 | * @return array|bool 是一张图片,可以直接展示或者下载 |
||
119 | */ |
||
120 | public function showqrcode_url($ticket = '') |
||
132 | |||
133 | /** |
||
134 | * 获取 卡券 Api_ticket |
||
135 | * |
||
136 | * @param boolean $jus 是否强制刷新 |
||
137 | * |
||
138 | * @return string $api_ticket api_ticket |
||
139 | */ |
||
140 | public function cardApiTicket($jus = false) |
||
164 | |||
165 | /** |
||
166 | * 微信卡券:JSAPI 卡券Package - 基础参数没有附带任何值 - 再生产环境中需要根据实际情况进行修改 |
||
167 | * |
||
168 | * @param string $card_id |
||
169 | * @param int $code |
||
170 | * @param int $openid |
||
171 | * @param int $outer_id |
||
172 | * @param int $timestamp |
||
173 | * @param int $api_ticket |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function wxCardPackage(array $card_list, $timestamp = null, $api_ticket = null) |
||
221 | |||
222 | /** |
||
223 | * 创建货架接口 |
||
224 | * |
||
225 | * @ string url $banner 页面的banner图片链接 |
||
226 | * |
||
227 | * @param string $page_title 页面的title |
||
228 | * @param boolean $can_share 页面是否可以分享,填入true/false |
||
229 | * @param string $scene 投放页面的场景值 SCENE_NEAR_BY 附近 SCENE_MENU 自定义菜单 SCENE_QRCODE 二维码 SCENE_ARTICLE 公众号文章 SCENE_H5 h5页面 SCENE_IVR 自动回复 SCENE_CARD_CUSTOM_CELL 卡券自定义cell |
||
230 | * @param array $card_list 卡券列表,每个item有两个字段 |
||
231 | * @ string $card_list ->cardid 所要在页面投放的cardid |
||
232 | * @ string url $card_list ->thumb_url 缩略图url |
||
233 | * |
||
234 | * @return string url 货架链接 page_id 货架ID。货架的唯一标识。 |
||
235 | */ |
||
236 | public function landingpage($banner, $page_title, $can_share = false, $scene = 'SCENE_CARD_CUSTOM_CELL', $card_list = []) |
||
257 | |||
258 | /** |
||
259 | * 导入code接口 |
||
260 | * |
||
261 | * @param string $card_id 卡券Id |
||
262 | * @param array $code 自定义code 数组 |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | View Code Duplication | public function deposit($card_id, $code = []) |
|
284 | |||
285 | /** |
||
286 | * 查询导入code数目 |
||
287 | * |
||
288 | * @param string $card_id 卡券ID |
||
289 | * |
||
290 | * @return int |
||
291 | */ |
||
292 | public function getdepositcount($card_id) |
||
309 | |||
310 | /** |
||
311 | * 核查code接口 |
||
312 | * |
||
313 | * @param [string] $card_id 卡券Id |
||
314 | * @param [array] $code 自定义code 数组 |
||
315 | * |
||
316 | */ |
||
317 | View Code Duplication | public function checkcode($card_id, $code = []) |
|
335 | |||
336 | /** |
||
337 | * 图文消息群发卡券 |
||
338 | * |
||
339 | * @param [type] $card_id [description] |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | public function gethtml($card_id) |
||
360 | |||
361 | /** |
||
362 | * 设置测试白名单 |
||
363 | * |
||
364 | * @param array $openid 白名单 openid 列表 |
||
365 | * @param array $username 白名单 微信号 列表 |
||
366 | * |
||
367 | * @return |
||
368 | */ |
||
369 | View Code Duplication | public function testwhitelist($openid = [], $username = []) |
|
387 | |||
388 | /** |
||
389 | * 查询Code接口 |
||
390 | * |
||
391 | * @param string $code 单张卡券的唯一标准。 |
||
392 | * @param boolean $check_consume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同。 |
||
393 | * @param string $card_id 卡券ID代表一类卡券。自定义code卡券必填。 |
||
394 | * |
||
395 | * @return array |
||
396 | */ |
||
397 | View Code Duplication | public function codeGet($code, $check_consume = true, $card_id = '') |
|
418 | |||
419 | /** |
||
420 | * 核销卡券 |
||
421 | * |
||
422 | * @param [type] $code 需核销的Code码 |
||
423 | * @param string $card_id 卡券ID。创建卡券时use_custom_code填写true时必填。非自定义Code不必填写。 |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | View Code Duplication | public function consume($code, $card_id = '') |
|
449 | |||
450 | /** |
||
451 | * Code解码接口 |
||
452 | * |
||
453 | * @param [string] $encrypt_code 经过加密的Code码。 |
||
454 | * |
||
455 | * @return [string] code |
||
456 | */ |
||
457 | public function decrypt($encrypt_code) |
||
474 | |||
475 | /** |
||
476 | * 用于获取用户卡包里的,属于该appid下的卡券 |
||
477 | * |
||
478 | * @param string $openid 需要查询的用户openid |
||
479 | * @param string $card_id 卡券ID。不填写时默认查询当前appid下的卡券 |
||
480 | * |
||
481 | * @return array |
||
482 | */ |
||
483 | View Code Duplication | public function getcardlist($openid, $card_id = '') |
|
504 | |||
505 | /** |
||
506 | * 获取卡券内容 |
||
507 | * |
||
508 | * @param string $card_id 卡券ID |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | View Code Duplication | public function cardGet($card_id) |
|
529 | |||
530 | /** |
||
531 | * 批量查询卡列表 |
||
532 | * |
||
533 | * @param integer $offset 查询卡列表的起始偏移量,从0开始,即offset: 5是指从从列表里的第六个开始读取 |
||
534 | * @param integer $count 需要查询的卡片的数量(数量最大50)。 |
||
535 | * @param string $status_list 支持开发者拉出指定状态的卡券列表 |
||
536 | * |
||
537 | * @return array |
||
538 | */ |
||
539 | public function batchget($offset = 0, $count = 10, $status_list = 'CARD_STATUS_VERIFY_OK') |
||
564 | |||
565 | /** |
||
566 | * 修改卡券 |
||
567 | * |
||
568 | * @param string $card_id [卡券ID] |
||
569 | * @param string $type [卡券类型] |
||
570 | * @param array $base_info [必要字段] |
||
571 | * @param array $especial [特殊字段] |
||
572 | * |
||
573 | * @return bool send_check 是否提交审核,false为修改后不会重新提审,true为修改字段后重新提审,该卡券的状态变为审核中 |
||
574 | */ |
||
575 | public function update($card_id, $type, $base_info = [], $especial = []) |
||
598 | |||
599 | /** |
||
600 | * 设置微信买单接口 |
||
601 | * |
||
602 | * @param string $card_id 卡券ID |
||
603 | * @param boolean $is_open 是否开启买单功能,填true/false |
||
604 | * |
||
605 | * @return bool|array |
||
606 | */ |
||
607 | View Code Duplication | public function paycellSet($card_id, $is_open = true) |
|
620 | |||
621 | /** |
||
622 | * 修改库存接口 |
||
623 | * |
||
624 | * @param string $card_id 卡券ID |
||
625 | * @param string $stock 操作 increase(增加) reduce(减少) |
||
626 | * @param integer $value 数值 |
||
627 | * |
||
628 | * @return [type] [description] |
||
629 | */ |
||
630 | public function modifystock($card_id, $stock = 'increase', $value = 0) |
||
651 | |||
652 | /** |
||
653 | * 更改Code接口 |
||
654 | * |
||
655 | * @param string $code 需变更的Code码 |
||
656 | * @param string $new_code 变更后的有效Code码 |
||
657 | * @param string $card_id 卡券ID。自定义Code码卡券为必填 |
||
658 | * |
||
659 | * @return |
||
660 | */ |
||
661 | View Code Duplication | public function codeUpdate($code, $new_code, $card_id) |
|
683 | |||
684 | /** |
||
685 | * 删除卡券接口 |
||
686 | * |
||
687 | * @param string $card_id 变更后的有效Code码。 |
||
688 | * |
||
689 | * @return |
||
690 | */ |
||
691 | View Code Duplication | public function cardDelete($card_id) |
|
708 | |||
709 | /** |
||
710 | * 设置卡券失效 |
||
711 | * |
||
712 | * @param string $code 设置失效的Code码。 |
||
713 | * @param string $card_id 卡券ID 非自定义code 可不填。 |
||
714 | * |
||
715 | * @return |
||
716 | */ |
||
717 | View Code Duplication | public function unavailable($code, $card_id) |
|
738 | |||
739 | /** |
||
740 | * 拉取卡券概况数据接口 |
||
741 | * |
||
742 | * @param string $begin_date 查询数据的起始时间。 |
||
743 | * @param int $end_date 查询数据的截至时间。 |
||
744 | * @param int $cond_source 卡券来源,0为公众平台创建的卡券数据、1是API创建的卡券数据 |
||
745 | * |
||
746 | * @return array |
||
747 | */ |
||
748 | View Code Duplication | public function getcardbizuininfo($begin_date, $end_date, $cond_source = 0) |
|
775 | |||
776 | /** |
||
777 | * 获取免费券数据接口 |
||
778 | * |
||
779 | * @param string $begin_date 查询数据的起始时间。 |
||
780 | * @param int $end_date 查询数据的截至时间。 |
||
781 | * @param int $cond_source 卡券来源,0为公众平台创建的卡券数据、1是API创建的卡券数据 |
||
782 | * @param string $card_id 卡券来源,0为公众平台创建的卡券数据、1是API创建的卡券数据 |
||
783 | * |
||
784 | * @return array |
||
785 | */ |
||
786 | public function getcardcardinfo($begin_date, $end_date, $cond_source = 0, $card_id = '') |
||
817 | |||
818 | /** |
||
819 | * 拉取会员卡数据接口 |
||
820 | * |
||
821 | * @param string $begin_date 查询数据的起始时间。 |
||
822 | * @param int $end_date 查询数据的截至时间。 |
||
823 | * @param int $cond_source 卡券来源,0为公众平台创建的卡券数据、1是API创建的卡券数据 |
||
824 | * |
||
825 | * @return array |
||
826 | */ |
||
827 | View Code Duplication | public function getcardmembercardinfo($begin_date, $end_date, $cond_source = 0) |
|
854 | |||
855 | /** |
||
856 | * 会员卡接口激活 |
||
857 | * |
||
858 | * @param array $activate 数据 |
||
859 | * |
||
860 | * @return |
||
861 | */ |
||
862 | public function activate($activate = []) |
||
878 | |||
879 | /** |
||
880 | * 设置开卡字段接口 |
||
881 | * |
||
882 | * @param string $card_id 卡券ID。 |
||
883 | * @param array $required_form 会员卡激活时的必填选项。 |
||
884 | * @param array $optional_form 会员卡激活时的选填项。 |
||
885 | * |
||
886 | * string common_field_id_list 微信格式化的选项类型。见以下列表。 |
||
887 | * string custom_field_list 喜欢的家具风格 自定义选项名称。 |
||
888 | * |
||
889 | * @return array|bool |
||
890 | */ |
||
891 | public function activateuserform($card_id, $required_form = [], $optional_form = []) |
||
910 | |||
911 | /** |
||
912 | * 拉取会员信息接口 |
||
913 | * |
||
914 | * @param string $card_id CardID |
||
915 | * @param string $code Code |
||
916 | * |
||
917 | * @return array |
||
918 | */ |
||
919 | View Code Duplication | public function membercardUserinfo($card_id, $code) |
|
937 | |||
938 | /** |
||
939 | * 更新会员信息 |
||
940 | * |
||
941 | * @param array $updateuser 参数 |
||
942 | * |
||
943 | * @return array|bool |
||
944 | */ |
||
945 | public function membercardUpdateuser($updateuser = []) |
||
961 | |||
962 | /** |
||
963 | * 添加子商户 |
||
964 | * |
||
965 | * @param string $brand_name 子商户名称(12个汉字内),该名称将在制券时填入并显示在卡券页面上 |
||
966 | * @param string $logo_url 子商户logo,可通过上传logo接口获取。该logo将在制券时填入并显示在卡券页面上 |
||
967 | * @param string $protocol 授权函ID,即通过上传临时素材接口上传授权函后获得的meida_id |
||
968 | * @param int $end_time 授权函有效期截止时间(东八区时间,单位为秒),需要与提交的扫描件一致 |
||
969 | * @param string $primary_category_id 一级类目id,可以通过本文档中接口查询 |
||
970 | * @param string $secondary_category_id 二级类目id,可以通过本文档中接口查询 |
||
971 | * @param string $agreement_media_id 营业执照或个体工商户营业执照彩照或扫描件 |
||
972 | * @param string $operator_media_id 营业执照内登记的经营者身份证彩照或扫描件 |
||
973 | * @param string $app_id 子商户的公众号app_id,配置后子商户卡券券面上的app_id为该app_id。注意:该app_id须经过认证 |
||
974 | * |
||
975 | * @return bool|array |
||
976 | */ |
||
977 | public function submerchant($brand_name, $logo_url, $protocol, $end_time, $primary_category_id, $secondary_category_id, $agreement_media_id, $operator_media_id, $app_id = '') |
||
999 | |||
1000 | /** |
||
1001 | * 卡券开放类目查询接口 |
||
1002 | * |
||
1003 | * @return array|bool |
||
1004 | */ |
||
1005 | public function getapplyprotocol() |
||
1015 | |||
1016 | /** |
||
1017 | * 朋友券锁定接口 |
||
1018 | * |
||
1019 | * @author yangyang <[email protected]> |
||
1020 | * |
||
1021 | * @date 2016-10-12 |
||
1022 | * |
||
1023 | * @param int $code 需要锁定的code码 |
||
1024 | * @param string $card_id 需要锁定的card_id |
||
1025 | * @param string $openid 领取者的openid |
||
1026 | * @param boolean $is_mark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用 |
||
1027 | * |
||
1028 | * @return array|bool |
||
1029 | */ |
||
1030 | public function mark($code, $card_id, $openid, $is_mark = true) |
||
1051 | |||
1052 | } |
||
1053 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.