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 Support 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 Support, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Support |
||
30 | { |
||
31 | use HasHttpRequest; |
||
32 | |||
33 | /** |
||
34 | * Alipay gateway. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $baseUri; |
||
39 | |||
40 | /** |
||
41 | * Config. |
||
42 | * |
||
43 | * @var Config |
||
44 | */ |
||
45 | protected $config; |
||
46 | |||
47 | /** |
||
48 | * Instance. |
||
49 | * |
||
50 | * @var Support |
||
51 | */ |
||
52 | private static $instance; |
||
53 | |||
54 | /** |
||
55 | * Bootstrap. |
||
56 | * |
||
57 | * @author yansongda <[email protected]> |
||
58 | * |
||
59 | * @param Config $config |
||
60 | */ |
||
61 | View Code Duplication | private function __construct(Config $config) |
|
68 | |||
69 | /** |
||
70 | * __get. |
||
71 | * |
||
72 | * @author yansongda <[email protected]> |
||
73 | * |
||
74 | * @param $key |
||
75 | * |
||
76 | * @return mixed|Config|null |
||
77 | */ |
||
78 | public function __get($key) |
||
82 | |||
83 | /** |
||
84 | * create. |
||
85 | * |
||
86 | * @author yansongda <[email protected]> |
||
87 | * |
||
88 | * @param Config $config |
||
89 | * |
||
90 | * @return Support |
||
91 | */ |
||
92 | View Code Duplication | public static function create(Config $config) |
|
100 | |||
101 | /** |
||
102 | * getInstance. |
||
103 | * |
||
104 | * @author yansongda <[email protected]> |
||
105 | * |
||
106 | * @throws InvalidArgumentException |
||
107 | * |
||
108 | * @return Support |
||
109 | */ |
||
110 | public static function getInstance() |
||
118 | |||
119 | /** |
||
120 | * clear. |
||
121 | * |
||
122 | * @author yansongda <[email protected]> |
||
123 | */ |
||
124 | public function clear() |
||
128 | |||
129 | /** |
||
130 | * Get Alipay API result. |
||
131 | * |
||
132 | * @author yansongda <[email protected]> |
||
133 | * |
||
134 | * @param array $data |
||
135 | * |
||
136 | * @throws GatewayException |
||
137 | * @throws InvalidConfigException |
||
138 | * @throws InvalidSignException |
||
139 | * |
||
140 | * @return Collection |
||
141 | */ |
||
142 | public static function requestApi(array $data): Collection |
||
156 | |||
157 | /** |
||
158 | * Generate sign. |
||
159 | * |
||
160 | * @author yansongda <[email protected]> |
||
161 | * |
||
162 | * @param array $params |
||
163 | * |
||
164 | * @throws InvalidConfigException |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public static function generateSign(array $params): string |
||
198 | |||
199 | /** |
||
200 | * Verify sign. |
||
201 | * |
||
202 | * @author yansongda <[email protected]> |
||
203 | * |
||
204 | * @param array $data |
||
205 | * @param bool $sync |
||
206 | * @param string|null $sign |
||
207 | * |
||
208 | * @throws InvalidConfigException |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public static function verifySign(array $data, $sync = false, $sign = null): bool |
||
213 | { |
||
214 | $publicKey = self::$instance->ali_public_key; |
||
215 | |||
216 | if (is_null($publicKey)) { |
||
217 | throw new InvalidConfigException('Missing Alipay Config -- [ali_public_key]'); |
||
218 | } |
||
219 | |||
220 | if (Str::endsWith($publicKey, '.crt')) { |
||
221 | $publicKey = file_get_contents($publicKey); |
||
222 | } elseif (Str::endsWith($publicKey, '.pem')) { |
||
223 | $publicKey = openssl_pkey_get_public( |
||
224 | Str::startsWith($publicKey, 'file://') ? $publicKey : 'file://'.$publicKey |
||
225 | ); |
||
226 | } else { |
||
227 | $publicKey = "-----BEGIN PUBLIC KEY-----\n". |
||
228 | wordwrap($publicKey, 64, "\n", true). |
||
229 | "\n-----END PUBLIC KEY-----"; |
||
230 | } |
||
231 | |||
232 | $sign = $sign ?? $data['sign']; |
||
233 | |||
234 | $toVerify = $sync ? json_encode($data, JSON_UNESCAPED_UNICODE) : self::getSignContent($data, true); |
||
235 | |||
236 | $isVerify = 1 === openssl_verify($toVerify, base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256); |
||
237 | |||
238 | if (is_resource($publicKey)) { |
||
239 | openssl_free_key($publicKey); |
||
240 | } |
||
241 | |||
242 | return $isVerify; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Get signContent that is to be signed. |
||
247 | * |
||
248 | * @author yansongda <[email protected]> |
||
249 | * |
||
250 | * @param array $data |
||
251 | * @param bool $verify |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public static function getSignContent(array $data, $verify = false): string |
||
256 | { |
||
257 | ksort($data); |
||
258 | |||
259 | $stringToBeSigned = ''; |
||
260 | foreach ($data as $k => $v) { |
||
261 | if ($verify && 'sign' != $k && 'sign_type' != $k) { |
||
262 | $stringToBeSigned .= $k.'='.$v.'&'; |
||
263 | } |
||
264 | if (!$verify && '' !== $v && !is_null($v) && 'sign' != $k && '@' != substr($v, 0, 1)) { |
||
265 | $stringToBeSigned .= $k.'='.$v.'&'; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | Log::debug('Alipay Generate Sign Content Before Trim', [$data, $stringToBeSigned]); |
||
270 | |||
271 | return trim($stringToBeSigned, '&'); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Convert encoding. |
||
276 | * |
||
277 | * @author yansongda <[email protected]> |
||
278 | * |
||
279 | * @param string|array $data |
||
280 | * @param string $to |
||
281 | * @param string $from |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public static function encoding($data, $to = 'utf-8', $from = 'gb2312'): array |
||
286 | { |
||
287 | return Arr::encoding((array) $data, $to, $from); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get service config. |
||
292 | * |
||
293 | * @author yansongda <[email protected]> |
||
294 | * |
||
295 | * @param string|null $key |
||
296 | * @param mixed|null $default |
||
297 | * |
||
298 | * @return mixed|null |
||
299 | */ |
||
300 | View Code Duplication | public function getConfig($key = null, $default = null) |
|
301 | { |
||
302 | if (is_null($key)) { |
||
303 | return $this->config->all(); |
||
304 | } |
||
305 | |||
306 | if ($this->config->has($key)) { |
||
307 | return $this->config[$key]; |
||
308 | } |
||
309 | |||
310 | return $default; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Get Base Uri. |
||
315 | * |
||
316 | * @author yansongda <[email protected]> |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getBaseUri() |
||
321 | { |
||
322 | return $this->baseUri; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * processingApiResult. |
||
327 | * |
||
328 | * @author yansongda <[email protected]> |
||
329 | * |
||
330 | * @param $data |
||
331 | * @param $result |
||
332 | * |
||
333 | * @throws GatewayException |
||
334 | * @throws InvalidConfigException |
||
335 | * @throws InvalidSignException |
||
336 | * |
||
337 | * @return Collection |
||
338 | */ |
||
339 | protected static function processingApiResult($data, $result): Collection |
||
340 | { |
||
341 | $method = str_replace('.', '_', $data['method']).'_response'; |
||
342 | |||
343 | if (!isset($result['sign']) || '10000' != $result[$method]['code']) { |
||
344 | throw new GatewayException( |
||
345 | 'Get Alipay API Error:'.$result[$method]['msg']. |
||
346 | (isset($result[$method]['sub_code']) ? (' - '.$result[$method]['sub_code']) : ''), |
||
347 | $result |
||
348 | ); |
||
349 | } |
||
350 | |||
351 | if (self::verifySign($result[$method], true, $result['sign'])) { |
||
352 | return new Collection($result[$method]); |
||
353 | } |
||
354 | |||
355 | Events::dispatch(new Events\SignFailed('Alipay', '', $result)); |
||
356 | |||
357 | throw new InvalidSignException('Alipay Sign Verify FAILED', $result); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Set Http options. |
||
362 | * |
||
363 | * @author yansongda <[email protected]> |
||
364 | * |
||
365 | * @return self |
||
366 | */ |
||
367 | View Code Duplication | protected function setHttpOptions(): self |
|
368 | { |
||
369 | if ($this->config->has('http') && is_array($this->config->get('http'))) { |
||
370 | $this->config->forget('http.base_uri'); |
||
371 | $this->httpOptions = $this->config->get('http'); |
||
372 | } |
||
373 | |||
374 | return $this; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * 生成应用证书SN |
||
379 | * |
||
380 | * @author 大冰 https://sbing.vip/archives/2019-new-alipay-php-docking.html |
||
381 | * |
||
382 | * @param $certPath |
||
383 | * @return string |
||
384 | * @throws /Exception |
||
385 | */ |
||
386 | public static function getCertSN($certPath): string |
||
387 | { |
||
388 | if (!is_file($certPath)) { |
||
389 | throw new \Exception('unknown certPath -- [getCertSN]'); |
||
390 | } |
||
391 | $x509data = file_get_contents($certPath); |
||
392 | if ($x509data === false) { |
||
393 | throw new \Exception('Alipay CertSN Error -- [getCertSN]'); |
||
394 | } |
||
395 | openssl_x509_read($x509data); |
||
396 | $certdata = openssl_x509_parse($x509data); |
||
397 | if (empty($certdata)) { |
||
398 | throw new \Exception('Alipay openssl_x509_parse Error -- [getCertSN]'); |
||
399 | } |
||
400 | $issuer_arr = []; |
||
401 | foreach ($certdata['issuer'] as $key => $val) { |
||
402 | $issuer_arr[] = $key . '=' . $val; |
||
403 | } |
||
404 | $issuer = implode(',', array_reverse($issuer_arr)); |
||
405 | Log::debug('getCertSN:', [$certPath, $issuer, $certdata['serialNumber']]); |
||
406 | return md5($issuer . $certdata['serialNumber']); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * 0x转高精度数字 |
||
411 | * |
||
412 | * @author 大冰 https://sbing.vip/archives/2019-new-alipay-php-docking.html |
||
413 | * |
||
414 | * @param $hex |
||
415 | * @return int|string |
||
416 | */ |
||
417 | private static function bchexdec($hex) |
||
426 | |||
427 | /** |
||
428 | * 生成支付宝根证书SN |
||
429 | * |
||
430 | * @author 大冰 https://sbing.vip/archives/2019-new-alipay-php-docking.html |
||
431 | * |
||
432 | * @param $certPath |
||
433 | * @return string |
||
434 | * @throws /Exception |
||
435 | */ |
||
436 | public static function getRootCertSN($certPath) |
||
471 | } |
||
472 |
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.