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 |
||
| 33 | class API extends AbstractAPI |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Merchant instance. |
||
| 37 | * |
||
| 38 | * @var Merchant |
||
| 39 | */ |
||
| 40 | protected $merchant; |
||
| 41 | |||
| 42 | // api |
||
| 43 | const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon'; |
||
| 44 | const API_QUERY_STOCK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/query_coupon_stock'; |
||
| 45 | const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/querycouponsinfo'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * API constructor. |
||
| 49 | * |
||
| 50 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
| 51 | */ |
||
| 52 | 3 | public function __construct(Merchant $merchant) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * send a cash coupon. |
||
| 59 | * |
||
| 60 | * @param array $params |
||
| 61 | * |
||
| 62 | * @return \EasyWeChat\Support\Collection |
||
| 63 | */ |
||
| 64 | 1 | public function send(array $params) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * query a coupon stock. |
||
| 73 | * |
||
| 74 | * @param array $params |
||
| 75 | * |
||
| 76 | * @return \EasyWeChat\Support\Collection |
||
| 77 | */ |
||
| 78 | 1 | public function queryStock(array $params) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * query a info of coupon. |
||
| 85 | * |
||
| 86 | * @param array $params |
||
| 87 | * |
||
| 88 | * @return \EasyWeChat\Support\Collection |
||
| 89 | */ |
||
| 90 | 1 | public function query(array $params) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Merchant setter. |
||
| 97 | * |
||
| 98 | * @param Merchant $merchant |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function setMerchant(Merchant $merchant) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Merchant getter. |
||
| 109 | * |
||
| 110 | * @return Merchant |
||
| 111 | */ |
||
| 112 | public function getMerchant() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Make a API request. |
||
| 119 | * |
||
| 120 | * @param string $api |
||
| 121 | * @param array $params |
||
| 122 | * @param string $method |
||
| 123 | * |
||
| 124 | * @return \EasyWeChat\Support\Collection |
||
| 125 | */ |
||
| 126 | 3 | View Code Duplication | protected function request($api, array $params, $method = 'post') |
| 142 | |||
| 143 | /** |
||
| 144 | * Parse Response XML to array. |
||
| 145 | * |
||
| 146 | * @param \Psr\Http\Message\ResponseInterface|string $response |
||
| 147 | * |
||
| 148 | * @return \EasyWeChat\Support\Collection |
||
| 149 | */ |
||
| 150 | 3 | protected function parseResponse($response) |
|
| 158 | } |
||
| 159 |