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 |
||
29 | class Card extends AbstractAPI |
||
30 | { |
||
31 | /** |
||
32 | * Cache. |
||
33 | * |
||
34 | * @var Cache |
||
35 | */ |
||
36 | protected $cache; |
||
37 | |||
38 | /** |
||
39 | * Ticket cache prefix. |
||
40 | */ |
||
41 | const TICKET_CACHE_PREFIX = 'overtrue.wechat.card_api_ticket.'; |
||
42 | |||
43 | const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors'; |
||
44 | const API_CREATE_CARD = 'https://api.weixin.qq.com/card/create'; |
||
45 | const API_CREATE_QRCODE = 'https://api.weixin.qq.com/card/qrcode/create'; |
||
46 | const API_SHOW_QRCODE = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
||
47 | const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
||
48 | const API_CREATE_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create'; |
||
49 | const API_DEPOSIT_CODE = 'https://api.weixin.qq.com/card/code/deposit'; |
||
50 | const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount'; |
||
51 | const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode'; |
||
52 | const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml'; |
||
53 | const API_SET_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set'; |
||
54 | const API_GET_CODE = 'https://api.weixin.qq.com/card/code/get'; |
||
55 | const API_CONSUME_CARD = 'https://api.weixin.qq.com/card/code/consume'; |
||
56 | const API_DECRYPT_CODE = 'https://api.weixin.qq.com/card/code/decrypt'; |
||
57 | const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist'; |
||
58 | const API_GET_CARD = 'https://api.weixin.qq.com/card/get'; |
||
59 | const API_LIST_CARD = 'https://api.weixin.qq.com/card/batchget'; |
||
60 | const API_UPDATE_CARD = 'https://api.weixin.qq.com/card/update'; |
||
61 | const API_SET_PAY_CELL = 'https://api.weixin.qq.com/card/paycell/set'; |
||
62 | const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock'; |
||
63 | const API_UPDATE_CODE = 'https://api.weixin.qq.com/card/code/update'; |
||
64 | const API_DELETE_CARD = 'https://api.weixin.qq.com/card/delete'; |
||
65 | const API_DISABLE_CARD = 'https://api.weixin.qq.com/card/code/unavailable'; |
||
66 | const API_ACTIVATE_CARD = 'https://api.weixin.qq.com/card/membercard/activate'; |
||
67 | const API_ACTIVATE_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set'; |
||
68 | const API_GET_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get'; |
||
69 | const API_UPDATE_MEMBER_CARD_USER = 'https://api.weixin.qq.com/card/membercard/updateuser'; |
||
70 | const API_CREATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit'; |
||
71 | const API_UPDATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/update'; |
||
72 | const API_GET_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/get'; |
||
73 | const API_LIST_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/batchget'; |
||
74 | const API_GET_CATEGORIES = 'https://api.weixin.qq.com/card/getapplyprotocol'; |
||
75 | |||
76 | /** |
||
77 | * 获取卡券颜色. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | 1 | public function getColors() |
|
82 | { |
||
83 | 1 | return $this->parseJSON('get', [self::API_GET_COLORS]); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * 创建卡券. |
||
88 | * |
||
89 | * @param string $cardType |
||
90 | * @param array $baseInfo |
||
91 | * @param array $especial |
||
92 | * @param array $advancedInfo |
||
93 | * |
||
94 | * @return bool|array |
||
95 | */ |
||
96 | 1 | public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = []) |
|
97 | { |
||
98 | $params = [ |
||
99 | 'card' => [ |
||
100 | 1 | 'card_type' => strtoupper($cardType), |
|
101 | 1 | strtolower($cardType) => array_merge(['base_info' => $baseInfo], $especial, $advancedInfo), |
|
102 | 1 | ], |
|
103 | 1 | ]; |
|
104 | |||
105 | 1 | return $this->parseJSON('json', [self::API_CREATE_CARD, $params]); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * 创建二维码. |
||
110 | * |
||
111 | * @param array $cards |
||
112 | * |
||
113 | * @return array|bool |
||
114 | */ |
||
115 | 1 | public function QRCode(array $cards = []) |
|
116 | { |
||
117 | 1 | return $this->parseJSON('json', [self::API_CREATE_QRCODE, $cards]); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * ticket 换取二维码图片. |
||
122 | * |
||
123 | * @param string $ticket |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 1 | public function showQRCode($ticket = null) |
|
128 | { |
||
129 | $params = [ |
||
130 | 1 | 'ticket' => $ticket, |
|
131 | 1 | ]; |
|
132 | |||
133 | 1 | $http = $this->getHttp(); |
|
134 | |||
135 | /** @var ResponseInterface $response */ |
||
136 | 1 | $response = $http->get(self::API_SHOW_QRCODE, $params); |
|
137 | |||
138 | return [ |
||
139 | 1 | 'status' => $response->getStatusCode(), |
|
140 | 1 | 'reason' => $response->getReasonPhrase(), |
|
141 | 1 | 'headers' => $response->getHeaders(), |
|
142 | 1 | 'body' => strval($response->getBody()), |
|
143 | 1 | 'url' => self::API_SHOW_QRCODE.'?'.http_build_query($params), |
|
144 | 1 | ]; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * 通过ticket换取二维码 链接. |
||
149 | * |
||
150 | * @param string $ticket |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 1 | public function getQRCodeUrl($ticket) |
|
155 | { |
||
156 | 1 | return self::API_SHOW_QRCODE.'?ticket='.$ticket; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * 获取 卡券 Api_ticket. |
||
161 | * |
||
162 | * @param bool $refresh 是否强制刷新 |
||
163 | * |
||
164 | * @return string $apiTicket |
||
165 | */ |
||
166 | 2 | View Code Duplication | public function getAPITicket($refresh = false) |
167 | { |
||
168 | 2 | $key = self::TICKET_CACHE_PREFIX.$this->getAccessToken()->getAppId(); |
|
169 | |||
170 | 2 | $ticket = $this->getCache()->fetch($key); |
|
171 | |||
172 | 2 | if (!$ticket || $refresh) { |
|
173 | 1 | $result = $this->parseJSON('get', [self::API_GET_CARD_TICKET, ['type' => 'wx_card']]); |
|
174 | |||
175 | 1 | $this->getCache()->save($key, $result['ticket'], $result['expires_in'] - 500); |
|
176 | |||
177 | 1 | return $result['ticket']; |
|
178 | } |
||
179 | |||
180 | 1 | return $ticket; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * 微信卡券:JSAPI 卡券发放 |
||
185 | * |
||
186 | * @param array $cards |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | public function jsConfigForAssign(array $cards) |
||
191 | { |
||
192 | 1 | return json_encode(array_map(function($card){ |
|
193 | 1 | return $this->attachExtension($card['card_id'], $card); |
|
194 | 1 | }, $cards)); |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * 生成 js添加到卡包 需要的 card_list 项. |
||
199 | * |
||
200 | * @param string $cardId |
||
201 | * @param array $extension |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | 1 | public function attachExtension($cardId, array $extension = array()) |
|
206 | { |
||
207 | 1 | $timestamp = time(); |
|
208 | $ext = [ |
||
209 | 1 | 'code' => Arr::get($extension, 'code'), |
|
210 | 1 | 'openid' => Arr::get($extension, 'openid', Arr::get($extension, 'open_id')), |
|
211 | 1 | 'timestamp' => $timestamp, |
|
212 | 1 | 'outer_id' => Arr::get($extension, 'outer_id'), |
|
213 | 1 | 'balance' => Arr::get($extension, 'balance'), |
|
214 | 1 | ]; |
|
215 | 1 | $ext['signature'] = $this->getSignature( |
|
216 | 1 | $this->getAPITicket(), |
|
217 | 1 | $timestamp, |
|
218 | 1 | $cardId, |
|
219 | 1 | $ext['code'], |
|
220 | 1 | $ext['openid'], |
|
221 | 1 | $ext['balance'] |
|
222 | 1 | ); |
|
223 | |||
224 | return [ |
||
225 | 1 | 'cardId' => $cardId, |
|
226 | 1 | 'cardExt' => json_encode($ext), |
|
227 | 1 | ]; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * 生成签名. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 1 | public function getSignature() |
|
236 | { |
||
237 | 1 | $params = func_get_args(); |
|
238 | 1 | sort($params, SORT_STRING); |
|
239 | 1 | return sha1(implode($params)); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * 创建货架接口. |
||
244 | * |
||
245 | * @param string $banner |
||
246 | * @param string $pageTitle |
||
247 | * @param bool $canShare |
||
248 | * @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
||
249 | * SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
||
250 | * @param array $cardList |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | 1 | public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
|
255 | { |
||
256 | $params = [ |
||
257 | 1 | 'banner' => $banner, |
|
258 | 1 | 'page_title' => $pageTitle, |
|
259 | 1 | 'can_share' => $canShare, |
|
260 | 1 | 'scene' => $scene, |
|
261 | 1 | 'card_list' => $cardList, |
|
262 | 1 | ]; |
|
263 | |||
264 | 1 | return $this->parseJSON('json', [self::API_CREATE_LANDING_PAGE, $params]); |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * 导入code接口. |
||
269 | * |
||
270 | * @param string $cardId |
||
271 | * @param array $code |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | 1 | View Code Duplication | public function deposit($cardId, $code) |
276 | { |
||
277 | $params = [ |
||
278 | 1 | 'card_id' => $cardId, |
|
279 | 1 | 'code' => $code, |
|
280 | 1 | ]; |
|
281 | |||
282 | 1 | return $this->parseJSON('json', [self::API_DEPOSIT_CODE, $params]); |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * 查询导入code数目. |
||
287 | * |
||
288 | * @param string $cardId |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 1 | View Code Duplication | public function getDepositedCount($cardId) |
293 | { |
||
294 | $params = [ |
||
295 | 1 | 'card_id' => $cardId, |
|
296 | 1 | ]; |
|
297 | |||
298 | 1 | return $this->parseJSON('json', [self::API_GET_DEPOSIT_COUNT, $params]); |
|
299 | } |
||
300 | |||
301 | /** |
||
302 | * 核查code接口. |
||
303 | * |
||
304 | * @param string $cardId |
||
305 | * @param array $code |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | 1 | View Code Duplication | public function checkCode($cardId, $code) |
318 | |||
319 | /** |
||
320 | * 查询Code接口. |
||
321 | * |
||
322 | * @param string $code |
||
323 | * @param bool $checkConsume |
||
324 | * @param string $cardId |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | 1 | View Code Duplication | public function getCode($code, $checkConsume, $cardId) |
329 | { |
||
330 | $params = [ |
||
338 | |||
339 | /** |
||
340 | * 核销Code接口. |
||
341 | * |
||
342 | * @param string $cardId |
||
343 | * @param string $code |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | 1 | View Code Duplication | public function consume($cardId, $code) |
356 | |||
357 | /** |
||
358 | * Code解码接口. |
||
359 | * |
||
360 | * @param string $encryptedCode |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | 1 | public function decryptCode($encryptedCode) |
|
372 | |||
373 | /** |
||
374 | * 图文消息群发卡券. |
||
375 | * |
||
376 | * @param string $cardId |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | 1 | View Code Duplication | public function getHtml($cardId) |
388 | |||
389 | /** |
||
390 | * 设置测试白名单. |
||
391 | * |
||
392 | * @param array $openid |
||
393 | * @param array $username |
||
394 | * |
||
395 | * @return array |
||
396 | */ |
||
397 | 1 | public function setTestWhitelist($openid, $username) |
|
406 | |||
407 | /** |
||
408 | * 获取用户已领取卡券接口. |
||
409 | * |
||
410 | * @param string $openid |
||
411 | * @param string $cardId |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | 1 | View Code Duplication | public function getUserCards($openid, $cardId = '') |
424 | |||
425 | /** |
||
426 | * 查看卡券详情. |
||
427 | * |
||
428 | * @param string $cardId |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | 1 | View Code Duplication | public function getCard($cardId) |
440 | |||
441 | /** |
||
442 | * 批量查询卡列表. |
||
443 | * |
||
444 | * @param int $offset |
||
445 | * @param int $count |
||
446 | * @param string $statusList |
||
447 | * |
||
448 | * @return array |
||
449 | */ |
||
450 | 1 | View Code Duplication | public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
460 | |||
461 | /** |
||
462 | * 更改卡券信息接口 and 设置跟随推荐接口. |
||
463 | * |
||
464 | * @param string $cardId |
||
465 | * @param string $type |
||
466 | * @param array $baseInfo |
||
467 | * @param array $especial |
||
468 | * |
||
469 | * @return array |
||
470 | */ |
||
471 | 1 | public function update($cardId, $type, $baseInfo = [], $especial = []) |
|
484 | |||
485 | /** |
||
486 | * 设置微信买单接口. |
||
487 | * 设置买单的 card_id 必须已经配置了门店,否则会报错. |
||
488 | * |
||
489 | * @param string $cardId |
||
490 | * @param bool $isOpen |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | 1 | View Code Duplication | public function setPayCell($cardId, $isOpen = true) |
503 | |||
504 | /** |
||
505 | * 增加库存 |
||
506 | * |
||
507 | * @param string $cardId |
||
508 | * @param int $amount |
||
509 | * |
||
510 | * @return bool |
||
511 | */ |
||
512 | 1 | public function increaseStock($cardId, $amount) |
|
516 | |||
517 | /** |
||
518 | * 减少库存 |
||
519 | * |
||
520 | * @param string $cardId |
||
521 | * @param int $amount |
||
522 | * |
||
523 | * @return bool |
||
524 | */ |
||
525 | 1 | public function reduceStock($cardId, $amount) |
|
529 | |||
530 | /** |
||
531 | * 修改库存接口. |
||
532 | * |
||
533 | * @param string $cardId |
||
534 | * @param int $amount |
||
535 | * @param string $action |
||
536 | * |
||
537 | * @return array |
||
538 | */ |
||
539 | 1 | protected function updateStock($cardId, $amount, $action = 'increase') |
|
549 | |||
550 | /** |
||
551 | * 更改Code接口. |
||
552 | * |
||
553 | * @param string $code |
||
554 | * @param string $newCode |
||
555 | * @param array $cardId |
||
556 | * |
||
557 | * @return array |
||
558 | */ |
||
559 | 1 | View Code Duplication | public function updateCode($code, $newCode, $cardId = []) |
569 | |||
570 | /** |
||
571 | * 删除卡券接口. |
||
572 | * |
||
573 | * @param string $cardId |
||
574 | * |
||
575 | * @return array |
||
576 | */ |
||
577 | 1 | View Code Duplication | public function delete($cardId) |
585 | |||
586 | /** |
||
587 | * 设置卡券失效. |
||
588 | * |
||
589 | * @param string $code |
||
590 | * @param string $cardId |
||
591 | * |
||
592 | * @return array |
||
593 | */ |
||
594 | 1 | View Code Duplication | public function disable($code, $cardId = '') |
603 | |||
604 | /** |
||
605 | * 会员卡接口激活. |
||
606 | * |
||
607 | * @param array $activate |
||
608 | * |
||
609 | * @return array |
||
610 | */ |
||
611 | 1 | public function activate($activate = []) |
|
615 | |||
616 | /** |
||
617 | * 设置开卡字段接口. |
||
618 | * |
||
619 | * @param string $cardId |
||
620 | * @param array $requiredForm |
||
621 | * @param array $optionalForm |
||
622 | * |
||
623 | * @return array |
||
624 | */ |
||
625 | 1 | View Code Duplication | public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
632 | |||
633 | /** |
||
634 | * 拉取会员信息接口. |
||
635 | * |
||
636 | * @param string $cardId |
||
637 | * @param string $code |
||
638 | * |
||
639 | * @return array |
||
640 | */ |
||
641 | 1 | View Code Duplication | public function getMemberCardUser($cardId, $code) |
650 | |||
651 | /** |
||
652 | * 更新会员信息. |
||
653 | * |
||
654 | * @param array $params |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | 1 | public function updateMemberCardUser(array $params = []) |
|
662 | |||
663 | /** |
||
664 | * 添加子商户. |
||
665 | * |
||
666 | * @param array $info |
||
667 | * |
||
668 | * @return array |
||
669 | */ |
||
670 | 1 | View Code Duplication | public function createSubMerchant(array $info = []) |
688 | |||
689 | /** |
||
690 | * 更新子商户. |
||
691 | * |
||
692 | * @param int $merchantId |
||
693 | * @param array $info |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | 1 | View Code Duplication | public function updateSubMerchant($merchantId, array $info = []) |
716 | |||
717 | /** |
||
718 | * 获取子商户信息. |
||
719 | * |
||
720 | * @param int $merchantId |
||
721 | * |
||
722 | * @return array |
||
723 | */ |
||
724 | public function getSubMerchant($merchantId) |
||
728 | |||
729 | /** |
||
730 | * 批量获取子商户信息. |
||
731 | * |
||
732 | * @param int $beginId |
||
733 | * @param int $limit |
||
734 | * @param string $status |
||
735 | * |
||
736 | * @return array |
||
737 | */ |
||
738 | public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
||
748 | |||
749 | /** |
||
750 | * 卡券开放类目查询接口. |
||
751 | * |
||
752 | * @return array|bool |
||
753 | */ |
||
754 | 1 | public function getCategories() |
|
758 | |||
759 | /** |
||
760 | * Set cache manager. |
||
761 | * |
||
762 | * @param \Doctrine\Common\Cache\Cache $cache |
||
763 | * |
||
764 | * @return $this |
||
765 | */ |
||
766 | 2 | public function setCache(Cache $cache) |
|
772 | |||
773 | /** |
||
774 | * Return cache manager. |
||
775 | * |
||
776 | * @return \Doctrine\Common\Cache\Cache |
||
777 | */ |
||
778 | 2 | public function getCache() |
|
782 | |||
783 | /** |
||
784 | * Set current url. |
||
785 | * |
||
786 | * @param string $url |
||
787 | * |
||
788 | * @return array |
||
789 | */ |
||
790 | 1 | public function setUrl($url) |
|
796 | } |
||
797 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: