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 |
||
| 32 | class PaymentGateway extends BasePaymentGateway |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Lệnh `refund` sử dụng cho việc tạo [[request()]] yêu cầu hoàn trả tiền. |
||
| 36 | */ |
||
| 37 | const RC_REFUND = 'refund'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @event RequestEvent được gọi trước khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]]. |
||
| 41 | */ |
||
| 42 | const EVENT_BEFORE_REFUND = 'beforeRefund'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @event RequestEvent được gọi sau khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]]. |
||
| 46 | */ |
||
| 47 | const EVENT_AFTER_REFUND = 'afterRefund'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Đường dẫn API để yêu cầu tạo giao dịch thanh toán. |
||
| 51 | */ |
||
| 52 | const PURCHASE_URL = '/paymentv2/vpcpay.html'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Đường dẫn API để truy vấn thông tin giao dịch. |
||
| 56 | */ |
||
| 57 | const QUERY_DR_URL = '/merchant_webapi/merchant.html'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Đường dẫn API để yêu cầu hoàn trả tiền. |
||
| 61 | */ |
||
| 62 | const REFUND_URL = '/merchant_webapi/merchant.html'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @inheritdoc |
||
| 66 | */ |
||
| 67 | public $clientConfig = ['class' => PaymentClient::class]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | public $requestDataConfig = ['class' => RequestData::class]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritdoc |
||
| 76 | */ |
||
| 77 | public $responseDataConfig = ['class' => ResponseData::class]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
| 82 | public $verifiedDataConfig = ['class' => VerifiedData::class]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @inheritdoc |
||
| 86 | */ |
||
| 87 | 3 | public function getBaseUrl(): string |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @inheritdoc |
||
| 94 | */ |
||
| 95 | 3 | protected function defaultVersion(): string |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Phương thức yêu cầu VnPayment hoàn trả tiền cho đơn hàng chỉ định. |
||
| 102 | * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_REFUND]]. |
||
| 103 | * |
||
| 104 | * @param array $data Dữ liệu yêu cầu hoàn trả. |
||
| 105 | * @param null $clientId Client id sử dụng để tạo yêu cầu. |
||
| 106 | * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client. |
||
| 107 | * @return ResponseData|DataInterface Trả về [[DataInterface]] là dữ liệu tổng hợp từ VnPayment phản hồi. |
||
| 108 | * @throws \ReflectionException|\yii\base\InvalidConfigException|\yii\base\InvalidArgumentException |
||
| 109 | */ |
||
| 110 | 1 | public function refund(array $data, $clientId = null): DataInterface |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @inheritdoc |
||
| 117 | */ |
||
| 118 | 3 | public function beforeRequest(RequestEvent $event) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
| 130 | 3 | public function afterRequest(RequestEvent $event) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritdoc |
||
| 141 | * @throws \yii\base\InvalidConfigException |
||
| 142 | */ |
||
| 143 | 4 | protected function initSandboxEnvironment() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @inheritdoc |
||
| 152 | * @throws \yii\base\InvalidConfigException |
||
| 153 | */ |
||
| 154 | 3 | protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @inheritdoc |
||
| 174 | */ |
||
| 175 | protected function getVerifyRequestData($command, \yii\web\Request $request): array |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @inheritdoc |
||
| 194 | */ |
||
| 195 | 3 | View Code Duplication | protected function getHttpClientConfig(): array |
| 207 | } |
||
| 208 |
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.