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 | 7 | public function __construct(Merchant $merchant) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Prepare luckymoney. |
||
| 69 | * |
||
| 70 | * @param array $params |
||
| 71 | * |
||
| 72 | * @return \EasyWeChat\Support\Collection |
||
| 73 | */ |
||
| 74 | 1 | public function prepare(array $params) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Query luckymoney. |
||
| 89 | * |
||
| 90 | * @param string $mchBillNo |
||
| 91 | * |
||
| 92 | * @return \EasyWeChat\Support\Collection |
||
| 93 | */ |
||
| 94 | 1 | public function query($mchBillNo) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Send LuckyMoney. |
||
| 107 | * |
||
| 108 | * @param array $params |
||
| 109 | * @param string $type |
||
| 110 | * |
||
| 111 | * @return \EasyWeChat\Support\Collection |
||
| 112 | */ |
||
| 113 | 3 | public function send(array $params, $type = self::TYPE_NORMAL) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Send normal LuckyMoney. |
||
| 127 | * |
||
| 128 | * @param array $params |
||
| 129 | * |
||
| 130 | * @return \EasyWeChat\Support\Collection |
||
| 131 | */ |
||
| 132 | 1 | public function sendNormal($params) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Send group luckymoney. |
||
| 142 | * |
||
| 143 | * @param array $params |
||
| 144 | * |
||
| 145 | * @return \EasyWeChat\Support\Collection |
||
| 146 | */ |
||
| 147 | 1 | public function sendGroup($params) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Merchant setter. |
||
| 156 | * |
||
| 157 | * @param Merchant $merchant |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | 1 | public function setMerchant(Merchant $merchant) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Merchant getter. |
||
| 168 | * |
||
| 169 | * @return Merchant |
||
| 170 | */ |
||
| 171 | 1 | public function getMerchant() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Make a API request. |
||
| 178 | * |
||
| 179 | * @param string $api |
||
| 180 | * @param array $params |
||
| 181 | * @param string $method |
||
| 182 | * |
||
| 183 | * @return \EasyWeChat\Support\Collection |
||
| 184 | */ |
||
| 185 | 5 | View Code Duplication | protected function request($api, array $params, $method = 'post') |
| 200 | |||
| 201 | /** |
||
| 202 | * Parse Response XML to array. |
||
| 203 | * |
||
| 204 | * @param \Psr\Http\Message\ResponseInterface|string $response |
||
| 205 | * |
||
| 206 | * @return \EasyWeChat\Support\Collection |
||
| 207 | */ |
||
| 208 | 5 | protected function parseResponse($response) |
|
| 216 | } |
||
| 217 |