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() |
|
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 = []) |
|
107 | |||
108 | /** |
||
109 | * 创建二维码. |
||
110 | * |
||
111 | * @param array $cards |
||
112 | * |
||
113 | * @return array|bool |
||
114 | */ |
||
115 | 1 | public function QRCode(array $cards = []) |
|
119 | |||
120 | /** |
||
121 | * ticket 换取二维码图片. |
||
122 | * |
||
123 | * @param string $ticket |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 1 | public function showQRCode($ticket = null) |
|
146 | |||
147 | /** |
||
148 | * 通过ticket换取二维码 链接. |
||
149 | * |
||
150 | * @param string $ticket |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 1 | public function getQRCodeUrl($ticket) |
|
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) |
182 | |||
183 | /** |
||
184 | * 微信卡券:JSAPI 卡券发放. |
||
185 | * |
||
186 | * @param array $cards |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | public function jsConfigForAssign(array $cards) |
||
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 = []) |
|
229 | |||
230 | /** |
||
231 | * 生成签名. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 1 | public function getSignature() |
|
242 | |||
243 | /** |
||
244 | * 创建货架接口. |
||
245 | * |
||
246 | * @param string $banner |
||
247 | * @param string $pageTitle |
||
248 | * @param bool $canShare |
||
249 | * @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
||
250 | * SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
||
251 | * @param array $cardList |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | 1 | public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
|
267 | |||
268 | /** |
||
269 | * 导入code接口. |
||
270 | * |
||
271 | * @param string $cardId |
||
272 | * @param array $code |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | 1 | View Code Duplication | public function deposit($cardId, $code) |
285 | |||
286 | /** |
||
287 | * 查询导入code数目. |
||
288 | * |
||
289 | * @param string $cardId |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | 1 | View Code Duplication | public function getDepositedCount($cardId) |
301 | |||
302 | /** |
||
303 | * 核查code接口. |
||
304 | * |
||
305 | * @param string $cardId |
||
306 | * @param array $code |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | 1 | View Code Duplication | public function checkCode($cardId, $code) |
319 | |||
320 | /** |
||
321 | * 查询Code接口. |
||
322 | * |
||
323 | * @param string $code |
||
324 | * @param bool $checkConsume |
||
325 | * @param string $cardId |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 1 | View Code Duplication | public function getCode($code, $checkConsume, $cardId) |
339 | |||
340 | /** |
||
341 | * 核销Code接口. |
||
342 | * |
||
343 | * @param string $cardId |
||
344 | * @param string $code |
||
345 | * |
||
346 | * @return array |
||
347 | */ |
||
348 | 1 | View Code Duplication | public function consume($cardId, $code) |
357 | |||
358 | /** |
||
359 | * Code解码接口. |
||
360 | * |
||
361 | * @param string $encryptedCode |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | 1 | public function decryptCode($encryptedCode) |
|
373 | |||
374 | /** |
||
375 | * 图文消息群发卡券. |
||
376 | * |
||
377 | * @param string $cardId |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | 1 | View Code Duplication | public function getHtml($cardId) |
389 | |||
390 | /** |
||
391 | * 设置测试白名单. |
||
392 | * |
||
393 | * @param array $openid |
||
|
|||
394 | * @param array $username |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | 1 | public function setTestWhitelist($openids) |
|
406 | |||
407 | /** |
||
408 | * 设置测试白名单(by username). |
||
409 | * |
||
410 | * @param array $usernames |
||
411 | * |
||
412 | * @return array |
||
413 | */ |
||
414 | 1 | public function setTestWhitelistByUsername($usernames) |
|
422 | |||
423 | /** |
||
424 | * 获取用户已领取卡券接口. |
||
425 | * |
||
426 | * @param string $openid |
||
427 | * @param string $cardId |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | 1 | View Code Duplication | public function getUserCards($openid, $cardId = '') |
440 | |||
441 | /** |
||
442 | * 查看卡券详情. |
||
443 | * |
||
444 | * @param string $cardId |
||
445 | * |
||
446 | * @return array |
||
447 | */ |
||
448 | 1 | View Code Duplication | public function getCard($cardId) |
456 | |||
457 | /** |
||
458 | * 批量查询卡列表. |
||
459 | * |
||
460 | * @param int $offset |
||
461 | * @param int $count |
||
462 | * @param string $statusList |
||
463 | * |
||
464 | * @return array |
||
465 | */ |
||
466 | 1 | View Code Duplication | public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
476 | |||
477 | /** |
||
478 | * 更改卡券信息接口 and 设置跟随推荐接口. |
||
479 | * |
||
480 | * @param string $cardId |
||
481 | * @param string $type |
||
482 | * @param array $baseInfo |
||
483 | * @param array $especial |
||
484 | * |
||
485 | * @return array |
||
486 | */ |
||
487 | 1 | public function update($cardId, $type, $baseInfo = [], $especial = []) |
|
500 | |||
501 | /** |
||
502 | * 设置微信买单接口. |
||
503 | * 设置买单的 card_id 必须已经配置了门店,否则会报错. |
||
504 | * |
||
505 | * @param string $cardId |
||
506 | * @param bool $isOpen |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | 1 | View Code Duplication | public function setPayCell($cardId, $isOpen = true) |
519 | |||
520 | /** |
||
521 | * 增加库存. |
||
522 | * |
||
523 | * @param string $cardId |
||
524 | * @param int $amount |
||
525 | * |
||
526 | * @return bool |
||
527 | */ |
||
528 | 1 | public function increaseStock($cardId, $amount) |
|
532 | |||
533 | /** |
||
534 | * 减少库存. |
||
535 | * |
||
536 | * @param string $cardId |
||
537 | * @param int $amount |
||
538 | * |
||
539 | * @return bool |
||
540 | */ |
||
541 | 1 | public function reduceStock($cardId, $amount) |
|
545 | |||
546 | /** |
||
547 | * 修改库存接口. |
||
548 | * |
||
549 | * @param string $cardId |
||
550 | * @param int $amount |
||
551 | * @param string $action |
||
552 | * |
||
553 | * @return array |
||
554 | */ |
||
555 | 1 | protected function updateStock($cardId, $amount, $action = 'increase') |
|
565 | |||
566 | /** |
||
567 | * 更改Code接口. |
||
568 | * |
||
569 | * @param string $code |
||
570 | * @param string $newCode |
||
571 | * @param array $cardId |
||
572 | * |
||
573 | * @return array |
||
574 | */ |
||
575 | 1 | View Code Duplication | public function updateCode($code, $newCode, $cardId = []) |
585 | |||
586 | /** |
||
587 | * 删除卡券接口. |
||
588 | * |
||
589 | * @param string $cardId |
||
590 | * |
||
591 | * @return array |
||
592 | */ |
||
593 | 1 | View Code Duplication | public function delete($cardId) |
601 | |||
602 | /** |
||
603 | * 设置卡券失效. |
||
604 | * |
||
605 | * @param string $code |
||
606 | * @param string $cardId |
||
607 | * |
||
608 | * @return array |
||
609 | */ |
||
610 | 1 | View Code Duplication | public function disable($code, $cardId = '') |
619 | |||
620 | /** |
||
621 | * 会员卡接口激活. |
||
622 | * |
||
623 | * @param array $info |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | 1 | public function activate($info = []) |
|
631 | |||
632 | /** |
||
633 | * 设置开卡字段接口. |
||
634 | * |
||
635 | * @param string $cardId |
||
636 | * @param array $requiredForm |
||
637 | * @param array $optionalForm |
||
638 | * |
||
639 | * @return array |
||
640 | */ |
||
641 | 1 | public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
|
647 | |||
648 | /** |
||
649 | * 拉取会员信息接口. |
||
650 | * |
||
651 | * @param string $cardId |
||
652 | * @param string $code |
||
653 | * |
||
654 | * @return array |
||
655 | */ |
||
656 | 1 | View Code Duplication | public function getMemberCardUser($cardId, $code) |
665 | |||
666 | /** |
||
667 | * 更新会员信息. |
||
668 | * |
||
669 | * @param array $params |
||
670 | * |
||
671 | * @return array |
||
672 | */ |
||
673 | 1 | public function updateMemberCardUser(array $params = []) |
|
677 | |||
678 | /** |
||
679 | * 添加子商户. |
||
680 | * |
||
681 | * @param array $info |
||
682 | * |
||
683 | * @return array |
||
684 | */ |
||
685 | 1 | View Code Duplication | public function createSubMerchant(array $info = []) |
703 | |||
704 | /** |
||
705 | * 更新子商户. |
||
706 | * |
||
707 | * @param int $merchantId |
||
708 | * @param array $info |
||
709 | * |
||
710 | * @return array |
||
711 | */ |
||
712 | 1 | View Code Duplication | public function updateSubMerchant($merchantId, array $info = []) |
731 | |||
732 | /** |
||
733 | * 获取子商户信息. |
||
734 | * |
||
735 | * @param int $merchantId |
||
736 | * |
||
737 | * @return array |
||
738 | */ |
||
739 | public function getSubMerchant($merchantId) |
||
743 | |||
744 | /** |
||
745 | * 批量获取子商户信息. |
||
746 | * |
||
747 | * @param int $beginId |
||
748 | * @param int $limit |
||
749 | * @param string $status |
||
750 | * |
||
751 | * @return array |
||
752 | */ |
||
753 | public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
||
763 | |||
764 | /** |
||
765 | * 卡券开放类目查询接口. |
||
766 | * |
||
767 | * @return array|bool |
||
768 | */ |
||
769 | 1 | public function getCategories() |
|
773 | |||
774 | /** |
||
775 | * Set cache manager. |
||
776 | * |
||
777 | * @param \Doctrine\Common\Cache\Cache $cache |
||
778 | * |
||
779 | * @return $this |
||
780 | */ |
||
781 | 2 | public function setCache(Cache $cache) |
|
787 | |||
788 | /** |
||
789 | * Return cache manager. |
||
790 | * |
||
791 | * @return \Doctrine\Common\Cache\Cache |
||
792 | */ |
||
793 | 2 | public function getCache() |
|
797 | |||
798 | /** |
||
799 | * Set current url. |
||
800 | * |
||
801 | * @param string $url |
||
802 | * |
||
803 | * @return array |
||
804 | */ |
||
805 | 1 | public function setUrl($url) |
|
811 | } |
||
812 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.