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 |
||
| 33 | class PaymentGateway extends BasePaymentGateway |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Đường dẫn API để yêu cầu tạo giao dịch thanh toán. |
||
| 38 | */ |
||
| 39 | const PURCHASE_URL = '/checkout.html'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Phương thức thanh toán bằng ví điện tử VTCPay. |
||
| 43 | */ |
||
| 44 | const PAYMENT_METHOD_VTCPAY = 'VTCPay'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Phương thức thanh toán bằng ngân hàng trong nước. |
||
| 48 | */ |
||
| 49 | const PAYMENT_METHOD_DOMESTIC_BANK = 'DomesticBank'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Phương thức thanh toán bằng thẻ quốc tế (visa/master). |
||
| 53 | */ |
||
| 54 | const PAYMENT_METHOD_INTERNATIONAL_CARD = 'InternationalCard'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @inheritdoc |
||
| 58 | */ |
||
| 59 | public function getBaseUrl(): string |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @inheritdoc |
||
| 66 | */ |
||
| 67 | public function requestCommands(): array |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @inheritdoc |
||
| 74 | */ |
||
| 75 | protected function initSandboxEnvironment() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @inheritdoc |
||
| 83 | * @throws NotSupportedException |
||
| 84 | */ |
||
| 85 | public function queryDR(array $data, $clientId = null): DataInterface |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritdoc |
||
| 92 | * @throws \yii\base\InvalidConfigException |
||
| 93 | */ |
||
| 94 | protected function requestInternal(RequestData $requestData, HttpClient $httpClient): array |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param int|string $command |
||
| 104 | * @param \yii\web\Request $request |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | protected function getVerifyRequestData($command, \yii\web\Request $request): array |
||
| 127 | |||
| 128 | } |
||
| 129 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: