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 |
||
| 30 | class Support |
||
| 31 | { |
||
| 32 | use HasHttpRequest; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Alipay gateway. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $baseUri; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Config. |
||
| 43 | * |
||
| 44 | * @var Config |
||
| 45 | */ |
||
| 46 | protected $config; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Instance. |
||
| 50 | * |
||
| 51 | * @var Support |
||
| 52 | */ |
||
| 53 | private static $instance; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Bootstrap. |
||
| 57 | * |
||
| 58 | * @author yansongda <[email protected]> |
||
| 59 | */ |
||
| 60 | View Code Duplication | private function __construct(Config $config) |
|
|
|
|||
| 61 | { |
||
| 62 | $this->baseUri = Alipay::URL[$config->get('mode', Alipay::MODE_NORMAL)]; |
||
| 63 | $this->config = $config; |
||
| 64 | |||
| 65 | $this->setHttpOptions(); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * __get. |
||
| 70 | * |
||
| 71 | * @author yansongda <[email protected]> |
||
| 72 | * |
||
| 73 | * @param $key |
||
| 74 | * |
||
| 75 | * @return mixed|Config|null |
||
| 76 | */ |
||
| 77 | public function __get($key) |
||
| 78 | { |
||
| 79 | return $this->getConfig($key); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * create. |
||
| 84 | * |
||
| 85 | * @author yansongda <[email protected]> |
||
| 86 | * |
||
| 87 | * @return Support |
||
| 88 | */ |
||
| 89 | View Code Duplication | public static function create(Config $config) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * getInstance. |
||
| 100 | * |
||
| 101 | * @author yansongda <[email protected]> |
||
| 102 | * |
||
| 103 | * @throws InvalidArgumentException |
||
| 104 | * |
||
| 105 | * @return Support |
||
| 106 | */ |
||
| 107 | public static function getInstance() |
||
| 108 | { |
||
| 109 | if (is_null(self::$instance)) { |
||
| 110 | throw new InvalidArgumentException('You Should [Create] First Before Using'); |
||
| 111 | } |
||
| 112 | |||
| 113 | return self::$instance; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * clear. |
||
| 118 | * |
||
| 119 | * @author yansongda <[email protected]> |
||
| 120 | */ |
||
| 121 | public function clear() |
||
| 122 | { |
||
| 123 | self::$instance = null; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get Alipay API result. |
||
| 128 | * |
||
| 129 | * @author yansongda <[email protected]> |
||
| 130 | * |
||
| 131 | * @throws GatewayException |
||
| 132 | * @throws InvalidConfigException |
||
| 133 | * @throws InvalidSignException |
||
| 134 | */ |
||
| 135 | public static function requestApi(array $data, bool $response = false): Collection |
||
| 136 | { |
||
| 137 | Events::dispatch(new Events\ApiRequesting('Alipay', '', self::$instance->getBaseUri(), $data)); |
||
| 138 | |||
| 139 | $data = array_filter($data, function ($value) { |
||
| 140 | return ('' == $value || is_null($value)) ? false : true; |
||
| 141 | }); |
||
| 142 | |||
| 143 | $result = json_decode(self::$instance->post('', $data), true); |
||
| 144 | |||
| 145 | Events::dispatch(new Events\ApiRequested('Alipay', '', self::$instance->getBaseUri(), $result)); |
||
| 146 | |||
| 147 | return self::processingApiResult($data, $result, $response); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Generate sign. |
||
| 152 | * |
||
| 153 | * @author yansongda <[email protected]> |
||
| 154 | * |
||
| 155 | * @throws InvalidConfigException |
||
| 156 | */ |
||
| 157 | public static function generateSign(array $params): string |
||
| 158 | { |
||
| 159 | $privateKey = self::$instance->private_key; |
||
| 160 | |||
| 161 | if (is_null($privateKey)) { |
||
| 162 | throw new InvalidConfigException('Missing Alipay Config -- [private_key]'); |
||
| 163 | } |
||
| 164 | |||
| 165 | if (Str::endsWith($privateKey, '.pem')) { |
||
| 166 | $privateKey = openssl_pkey_get_private( |
||
| 167 | Str::startsWith($privateKey, 'file://') ? $privateKey : 'file://'.$privateKey |
||
| 168 | ); |
||
| 169 | } else { |
||
| 170 | $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n". |
||
| 171 | wordwrap($privateKey, 64, "\n", true). |
||
| 172 | "\n-----END RSA PRIVATE KEY-----"; |
||
| 173 | } |
||
| 174 | |||
| 175 | openssl_sign(self::getSignContent($params), $sign, $privateKey, OPENSSL_ALGO_SHA256); |
||
| 176 | |||
| 177 | $sign = base64_encode($sign); |
||
| 178 | |||
| 179 | Log::debug('Alipay Generate Sign', [$params, $sign]); |
||
| 180 | |||
| 181 | if (is_resource($privateKey)) { |
||
| 182 | openssl_free_key($privateKey); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $sign; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Verify sign. |
||
| 190 | * |
||
| 191 | * @author yansongda <[email protected]> |
||
| 192 | * |
||
| 193 | * @param bool $sync |
||
| 194 | * @param string|null $sign |
||
| 195 | * |
||
| 196 | * @throws InvalidConfigException |
||
| 197 | */ |
||
| 198 | public static function verifySign(array $data, $sync = false, $sign = null): bool |
||
| 199 | { |
||
| 200 | $publicKey = self::$instance->ali_public_key; |
||
| 201 | |||
| 202 | if (is_null($publicKey)) { |
||
| 203 | throw new InvalidConfigException('Missing Alipay Config -- [ali_public_key]'); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (Str::endsWith($publicKey, '.crt')) { |
||
| 207 | $publicKey = file_get_contents($publicKey); |
||
| 208 | } elseif (Str::endsWith($publicKey, '.pem')) { |
||
| 209 | $publicKey = openssl_pkey_get_public( |
||
| 210 | Str::startsWith($publicKey, 'file://') ? $publicKey : 'file://'.$publicKey |
||
| 211 | ); |
||
| 212 | } else { |
||
| 213 | $publicKey = "-----BEGIN PUBLIC KEY-----\n". |
||
| 214 | wordwrap($publicKey, 64, "\n", true). |
||
| 215 | "\n-----END PUBLIC KEY-----"; |
||
| 216 | } |
||
| 217 | |||
| 218 | $sign = $sign ?? $data['sign']; |
||
| 219 | |||
| 220 | $toVerify = $sync ? json_encode($data, JSON_UNESCAPED_UNICODE) : self::getSignContent($data, true); |
||
| 221 | |||
| 222 | $isVerify = 1 === openssl_verify($toVerify, base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256); |
||
| 223 | |||
| 224 | if (is_resource($publicKey)) { |
||
| 225 | openssl_free_key($publicKey); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $isVerify; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get signContent that is to be signed. |
||
| 233 | * |
||
| 234 | * @author yansongda <[email protected]> |
||
| 235 | * |
||
| 236 | * @param bool $verify |
||
| 237 | */ |
||
| 238 | public static function getSignContent(array $data, $verify = false): string |
||
| 239 | { |
||
| 240 | ksort($data); |
||
| 241 | |||
| 242 | $stringToBeSigned = ''; |
||
| 243 | foreach ($data as $k => $v) { |
||
| 244 | if ($verify && 'sign' != $k && 'sign_type' != $k) { |
||
| 245 | $stringToBeSigned .= $k.'='.$v.'&'; |
||
| 246 | } |
||
| 247 | if (!$verify && '' !== $v && !is_null($v) && 'sign' != $k && '@' != substr($v, 0, 1)) { |
||
| 248 | $stringToBeSigned .= $k.'='.$v.'&'; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | Log::debug('Alipay Generate Sign Content Before Trim', [$data, $stringToBeSigned]); |
||
| 253 | |||
| 254 | return trim($stringToBeSigned, '&'); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Convert encoding. |
||
| 259 | * |
||
| 260 | * @author yansongda <[email protected]> |
||
| 261 | * |
||
| 262 | * @param string|array $data |
||
| 263 | * @param string $to |
||
| 264 | * @param string $from |
||
| 265 | */ |
||
| 266 | public static function encoding($data, $to = 'utf-8', $from = 'gb2312'): array |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get service config. |
||
| 273 | * |
||
| 274 | * @author yansongda <[email protected]> |
||
| 275 | * |
||
| 276 | * @param string|null $key |
||
| 277 | * @param mixed|null $default |
||
| 278 | * |
||
| 279 | * @return mixed|null |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function getConfig($key = null, $default = null) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Get Base Uri. |
||
| 296 | * |
||
| 297 | * @author yansongda <[email protected]> |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function getBaseUri() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * 生成应用证书SN. |
||
| 308 | * |
||
| 309 | * @author 大冰 https://sbing.vip/archives/2019-new-alipay-php-docking.html |
||
| 310 | * |
||
| 311 | * @param $certPath |
||
| 312 | * |
||
| 313 | * @throws /Exception |
||
| 314 | */ |
||
| 315 | public static function getCertSN($certPath): string |
||
| 338 | |||
| 339 | /** |
||
| 340 | * 生成支付宝根证书SN. |
||
| 341 | * |
||
| 342 | * @author 大冰 https://sbing.vip/archives/2019-new-alipay-php-docking.html |
||
| 343 | * |
||
| 344 | * @param $certPath |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | * |
||
| 348 | * @throws /Exception |
||
| 349 | */ |
||
| 350 | public static function getRootCertSN($certPath) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * processingApiResult. |
||
| 389 | * |
||
| 390 | * @author yansongda <[email protected]> |
||
| 391 | * |
||
| 392 | * @param $data |
||
| 393 | * @param $result |
||
| 394 | * |
||
| 395 | * @throws GatewayException |
||
| 396 | * @throws InvalidConfigException |
||
| 397 | * @throws InvalidSignException |
||
| 398 | */ |
||
| 399 | protected static function processingApiResult($data, $result, $response = false): Collection |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set Http options. |
||
| 422 | * |
||
| 423 | * @author yansongda <[email protected]> |
||
| 424 | */ |
||
| 425 | View Code Duplication | protected function setHttpOptions(): self |
|
| 434 | |||
| 435 | /** |
||
| 436 | * 0x转高精度数字. |
||
| 437 | * |
||
| 438 | * @author 大冰 https://sbing.vip/archives/2019-new-alipay-php-docking.html |
||
| 439 | * |
||
| 440 | * @param $hex |
||
| 441 | * |
||
| 442 | * @return int|string |
||
| 443 | */ |
||
| 444 | private static function bchexdec($hex) |
||
| 456 | } |
||
| 457 |
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.