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 |
||
| 14 | class Support |
||
| 15 | { |
||
| 16 | use HasHttpRequest; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Instance. |
||
| 20 | * |
||
| 21 | * @var Support |
||
| 22 | */ |
||
| 23 | private static $instance; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Alipay gateway. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $baseUri = 'https://openapi.alipay.com/gateway.do'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get instance. |
||
| 34 | * |
||
| 35 | * @author yansongda <[email protected]> |
||
| 36 | * |
||
| 37 | * @return Support |
||
| 38 | */ |
||
| 39 | public static function getInstance() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get Alipay API result. |
||
| 50 | * |
||
| 51 | * @author yansongda <[email protected]> |
||
| 52 | * |
||
| 53 | * @param array $data |
||
| 54 | * @param string $publicKey |
||
| 55 | * |
||
| 56 | * @return Collection |
||
| 57 | */ |
||
| 58 | public static function requestApi($data, $publicKey): Collection |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Generate sign. |
||
| 86 | * |
||
| 87 | * @author yansongda <[email protected]> |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public static function generateSign($parmas, $privateKey = null): string |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Verfiy sign. |
||
| 112 | * |
||
| 113 | * @author yansongda <[email protected]> |
||
| 114 | * |
||
| 115 | * @param array $data |
||
| 116 | * @param string $publicKey |
||
| 117 | * @param bool $sync |
||
| 118 | * @param string|null $sign |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public static function verifySign($data, $publicKey = null, $sync = false, $sign = null): bool |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get signContent that is to be signed. |
||
| 149 | * |
||
| 150 | * @author yansongda <[email protected]> |
||
| 151 | * |
||
| 152 | * @param array $toBeSigned |
||
| 153 | * @param bool $verify |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public static function getSignContent(array $toBeSigned, $verify = false): string |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Alipay gateway. |
||
| 178 | * |
||
| 179 | * @author yansongda <[email protected]> |
||
| 180 | * |
||
| 181 | * @param string $mode |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public static function baseUri($mode = null): string |
||
| 198 | } |
||
| 199 |
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.