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 |
||
37 | class PaymentGateway extends BasePaymentGateway |
||
38 | { |
||
39 | /** |
||
40 | * Lệnh `refund` sử dụng cho việc tạo [[request()]] yêu cầu hoàn trả tiền. |
||
41 | */ |
||
42 | const RC_REFUND = 'refund'; |
||
43 | |||
44 | /** |
||
45 | * Lệnh `queryRefund` sử dụng cho việc tạo [[request()]] để kiểm tra trang thái của lệnh `refund` đã tạo. |
||
46 | */ |
||
47 | const RC_QUERY_REFUND = 'queryRefund'; |
||
48 | |||
49 | /** |
||
50 | * @event RequestEvent được gọi trước khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]]. |
||
51 | */ |
||
52 | const EVENT_BEFORE_REFUND = 'beforeRefund'; |
||
53 | |||
54 | /** |
||
55 | * @event RequestEvent được gọi sau khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]]. |
||
56 | */ |
||
57 | const EVENT_AFTER_REFUND = 'afterRefund'; |
||
58 | |||
59 | /** |
||
60 | * @event RequestEvent được gọi trước khi khởi tạo lệnh [[RC_QUERY_REFUND]] ở phương thức [[request()]]. |
||
61 | */ |
||
62 | const EVENT_BEFORE_QUERY_REFUND = 'beforeQueryRefund'; |
||
63 | |||
64 | /** |
||
65 | * @event RequestEvent được gọi sau khi khởi tạo lệnh [[RC_QUERY_REFUND]] ở phương thức [[request()]]. |
||
66 | */ |
||
67 | const EVENT_AFTER_QUERY_REFUND = 'afterQueryRefund'; |
||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | public $clientConfig = ['class' => PaymentClient::class]; |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | public $requestDataConfig = ['class' => RequestData::class]; |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public $responseDataConfig = ['class' => ResponseData::class]; |
||
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | public $verifiedDataConfig = ['class' => VerifiedData::class]; |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | public function getBaseUrl(): string |
||
96 | |||
97 | /** |
||
98 | * Phương thức yêu cầu MOMO hoàn trả tiền cho đơn hàng chỉ định. |
||
99 | * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_REFUND]]. |
||
100 | * |
||
101 | * @param array $data Dữ liệu yêu cầu hoàn trả. |
||
102 | * @param null $clientId Client id sử dụng để tạo yêu cầu. |
||
103 | * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client. |
||
104 | * @return ResponseData|DataInterface Trả về [[DataInterface]] là dữ liệu tổng hợp từ MOMO phản hồi. |
||
105 | * @throws \ReflectionException|\yii\base\InvalidConfigException|\yii\base\InvalidArgumentException |
||
106 | */ |
||
107 | public function refund(array $data, $clientId = null): DataInterface |
||
111 | |||
112 | /** |
||
113 | * Phương thức truy vấn thông tin của lệnh hoàn tiền tại MOMO. |
||
114 | * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_QUERY_REFUND]]. |
||
115 | * |
||
116 | * @param array $data Dữ liệu trạng thái hoàn tiền. |
||
117 | * @param null $clientId Client id sử dụng để tạo yêu cầu truy vấn trạng thái. |
||
118 | * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client. |
||
119 | * @return ResponseData|DataInterface Trả về [[DataInterface]] là dữ liệu tổng hợp từ MOMO phản hồi. |
||
120 | * @throws \ReflectionException|\yii\base\InvalidConfigException|\yii\base\InvalidArgumentException |
||
121 | */ |
||
122 | public function queryRefund(array $data, $clientId = null): DataInterface |
||
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | View Code Duplication | public function beforeRequest(RequestEvent $event) |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | View Code Duplication | public function afterRequest(RequestEvent $event) |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | * @throws \yii\base\InvalidConfigException |
||
168 | */ |
||
169 | protected function initSandboxEnvironment() |
||
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | * @throws \yii\base\InvalidConfigException |
||
178 | * @throws \yii\httpclient\Exception |
||
179 | */ |
||
180 | protected function requestInternal(RequestData $requestData, HttpClient $httpClient): array |
||
186 | |||
187 | protected function getVerifyRequestData($command, \yii\web\Request $request): array |
||
191 | |||
192 | } |
||
193 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: