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 |
||
10 | class Api |
||
11 | { |
||
12 | const NOTIFY_TOKEN_FIELD = 'echo_4'; |
||
13 | |||
14 | // 1 CREDITCARD 信用卡 |
||
15 | const CREDITCARD = 'CREDITCARD'; |
||
16 | // 2 RECHARGE 儲值卡 |
||
17 | const RECHARGE = 'RECHARGE'; |
||
18 | // 3 CSTORECODE 超商代碼 |
||
19 | const CSTORECODE = 'CSTORECODE'; |
||
20 | // 4 WEBATM WEBATM |
||
21 | const WEBATM = 'WEBATM'; |
||
22 | // 5 TELECOM 電信小額 |
||
23 | const TELECOM = 'TELECOM'; |
||
24 | // 6 E_COLLECTION 虛擬帳號 |
||
25 | const E_COLLECTION = 'E_COLLECTION'; |
||
26 | // 7 UNIONPAY 銀聯卡 |
||
27 | const UNIONPAY = 'UNIONPAY'; |
||
28 | // 8 SVC 點數卡 |
||
29 | const SVC = 'SVC'; |
||
30 | // 9 ABROAD 海外信用卡 |
||
31 | const ABROAD = 'ABROAD'; |
||
32 | // 10 ALIPAY 支付寶 |
||
33 | const ALIPAY = 'ALIPAY'; |
||
34 | // 11 SMARTPAY Smart Pay |
||
35 | const SMARTPAY = 'SMARTPAY'; |
||
36 | |||
37 | /** |
||
38 | * @var \Payum\Core\HttpClientInterface |
||
39 | */ |
||
40 | protected $client; |
||
41 | |||
42 | /** |
||
43 | * @var \Http\Message\MessageFactory |
||
44 | */ |
||
45 | protected $messageFactory; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $options = []; |
||
51 | |||
52 | /** |
||
53 | * $encrypter. |
||
54 | * |
||
55 | * @var Encrypter |
||
56 | */ |
||
57 | protected $encrypter; |
||
58 | |||
59 | /** |
||
60 | * @param array $options |
||
61 | * @param \Payum\Core\HttpClientInterface $client |
||
62 | * @param \Http\Message\MessageFactory $messageFactory |
||
63 | * @param Encrypter $encrypter |
||
64 | * |
||
65 | * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid |
||
66 | */ |
||
67 | 7 | public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, Encrypter $encrypter = null) |
|
74 | |||
75 | /** |
||
76 | * getApiEndpoint. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | 5 | public function getApiEndpoint() |
|
84 | |||
85 | /** |
||
86 | * createTransaction. |
||
87 | * |
||
88 | * @param array $params |
||
89 | * @return array |
||
90 | */ |
||
91 | 1 | public function createTransaction(array $params) |
|
214 | |||
215 | /** |
||
216 | * getTransactionData. |
||
217 | * |
||
218 | * @param array $params |
||
219 | * @return array |
||
220 | */ |
||
221 | 1 | public function getTransactionData(array $params) |
|
237 | |||
238 | /** |
||
239 | * refundTransaction. |
||
240 | * |
||
241 | * @param array $params |
||
242 | * @return array |
||
243 | */ |
||
244 | 1 | View Code Duplication | public function refundTransaction(array $params) |
262 | |||
263 | /** |
||
264 | * cancelTransaction. |
||
265 | * |
||
266 | * @param array $params |
||
267 | * @return array |
||
268 | */ |
||
269 | 1 | View Code Duplication | public function cancelTransaction(array $params) |
286 | |||
287 | /** |
||
288 | * verifyHash. |
||
289 | * |
||
290 | * @param array $params |
||
291 | * @param array $details |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function verifyHash(array $params, $details) |
|
298 | |||
299 | /** |
||
300 | * @param array $fields |
||
301 | * @return array |
||
302 | */ |
||
303 | 4 | protected function doRequest(array $fields) |
|
324 | } |
||
325 |
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.