Conditions | 12 |
Paths | 8 |
Total Lines | 59 |
Code Lines | 37 |
Lines | 12 |
Ratio | 20.34 % |
Changes | 4 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
122 | public function getResponse() |
||
123 | { |
||
124 | if (is_null($this->business)) { |
||
125 | throw new Exception('Business is required'); |
||
126 | } |
||
127 | |||
128 | static::$params['appid'] = $this->business->appid; |
||
129 | static::$params['mch_id'] = $this->business->mch_id; |
||
130 | $this->checkParams(); |
||
131 | $signGenerator = new SignGenerator(static::$params); |
||
132 | $signGenerator->onSortAfter(function (SignGenerator $that) { |
||
133 | $that->key = $this->business->mch_key; |
||
134 | }); |
||
135 | static::$params['sign'] = $signGenerator->getResult(); |
||
136 | |||
137 | $request = XML::build(static::$params); |
||
138 | //设置Http使用的证书 |
||
139 | $options['sslcert_path'] = $this->business->getClientCert(); |
||
140 | $options['sslkey_path'] = $this->business->getClientKey(); |
||
141 | |||
142 | $http = new Http(); |
||
143 | $response = $http->request(static::REFUNDORDER_URL, Http::POST, $request, $options); |
||
144 | if (empty($response)) { |
||
145 | throw new Exception('Get Refund Failure:'); |
||
146 | } |
||
147 | $refundOrder = XML::parse($response); |
||
148 | |||
149 | View Code Duplication | if (isset($refundOrder['return_code']) && |
|
150 | $refundOrder['return_code'] === 'FAIL') { |
||
151 | throw new Exception($refundOrder['return_code'].': '.$refundOrder['return_msg']); |
||
152 | } |
||
153 | |||
154 | //返回签名数据校验 |
||
155 | if (empty($refundOrder) || empty($refundOrder['sign'])) { |
||
156 | throw new Exception('param sign is missing or empty'); |
||
157 | } |
||
158 | $sign = $refundOrder['sign']; |
||
159 | unset($refundOrder['sign']); |
||
160 | $signGenerator = new SignGenerator($refundOrder); |
||
161 | $signGenerator->onSortAfter(function (SignGenerator $that) { |
||
162 | $that->key = $this->business->mch_key; |
||
163 | }); |
||
164 | if ($sign !== $signGenerator->getResult()) { |
||
165 | throw new Exception('check sign error'); |
||
166 | } |
||
167 | |||
168 | //返回结果判断 |
||
169 | View Code Duplication | if (isset($refundOrder['result_code']) && |
|
170 | ($refundOrder['result_code'] === 'FAIL')) { |
||
171 | throw new Exception($refundOrder['err_code'].': '.$refundOrder['err_code_des']); |
||
172 | } |
||
173 | |||
174 | View Code Duplication | if (isset($refundOrder['return_code']) && |
|
175 | $refundOrder['return_code'] === 'FAIL') { |
||
176 | throw new Exception($refundOrder['return_code'].': '.$refundOrder['return_msg']); |
||
177 | } |
||
178 | |||
179 | return $this->refundInfo = $refundOrder; |
||
180 | } |
||
181 | |||
217 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.