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:
1 | <?php |
||
32 | class API extends AbstractAPI |
||
33 | { |
||
34 | /** |
||
35 | * Merchant instance. |
||
36 | * |
||
37 | * @var Merchant |
||
38 | */ |
||
39 | protected $merchant; |
||
40 | |||
41 | // api |
||
42 | const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; |
||
43 | const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; |
||
44 | const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; |
||
45 | const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder'; |
||
46 | |||
47 | // LuckyMoney type |
||
48 | const TYPE_NORMAL = 'NORMAL'; |
||
49 | const TYPE_GROUP = 'GROUP'; |
||
50 | |||
51 | // Risk control type. |
||
52 | const RISK_NORMAL = 'NORMAL'; |
||
53 | const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT'; |
||
54 | const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT'; |
||
55 | const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT'; |
||
56 | |||
57 | /** |
||
58 | * API constructor. |
||
59 | * |
||
60 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
61 | */ |
||
62 | public function __construct(Merchant $merchant) |
||
66 | |||
67 | /** |
||
68 | * Prepare luckymoney. |
||
69 | * |
||
70 | * |
||
71 | * @return \EasyWeChat\Support\Collection |
||
72 | */ |
||
73 | public function prepare(array $params) |
||
85 | |||
86 | /** |
||
87 | * Query luckymoney. |
||
88 | * |
||
89 | * @param string $orderNo |
||
90 | */ |
||
91 | public function query($orderNo) |
||
101 | |||
102 | /** |
||
103 | * Send Luckymoney. |
||
104 | * |
||
105 | * @param array $params |
||
106 | * @param string $type |
||
107 | * |
||
108 | * @return \EasyWeChat\Support\Collection |
||
109 | */ |
||
110 | public function send(array $params, $type = self::TYPE_NORMAL) |
||
118 | |||
119 | /** |
||
120 | * Send normal lucnymoney. |
||
121 | * |
||
122 | * @param array $params |
||
123 | * |
||
124 | * @return \EasyWeChat\Support\Collection |
||
125 | */ |
||
126 | View Code Duplication | public function sendNormal($params) |
|
133 | |||
134 | /** |
||
135 | * Send group luckymoney. |
||
136 | * |
||
137 | * @param array $params |
||
138 | * |
||
139 | * @return \EasyWeChat\Support\Collection |
||
140 | */ |
||
141 | View Code Duplication | public function sendGroup($params) |
|
148 | |||
149 | /** |
||
150 | * Merchant setter. |
||
151 | * |
||
152 | * @param Merchant $merchant |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setMerchant(Merchant $merchant) |
||
160 | |||
161 | /** |
||
162 | * Merchant getter. |
||
163 | * |
||
164 | * @return Merchant |
||
165 | */ |
||
166 | public function getMerchant() |
||
170 | |||
171 | /** |
||
172 | * Make a API request. |
||
173 | * |
||
174 | * @param string $api |
||
175 | * @param array $params |
||
176 | * @param string $method |
||
177 | * |
||
178 | * @return \EasyWeChat\Support\Collection |
||
179 | */ |
||
180 | protected function request($api, array $params, $method = 'post') |
||
194 | |||
195 | /** |
||
196 | * Parse Response XML to array. |
||
197 | * |
||
198 | * @param \Psr\Http\Message\ResponseInterface|string $response |
||
199 | * |
||
200 | * @return \EasyWeChat\Support\Collection |
||
201 | */ |
||
202 | protected function parseResponse($response) |
||
210 | } |
||
211 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: