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 declare(strict_types=1); |
||
24 | class Client |
||
25 | { |
||
26 | /** |
||
27 | * |
||
28 | */ |
||
29 | const MSU_API_VERSION = 2; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private static $validActions = [ |
||
35 | |||
36 | 'session', |
||
37 | 'financialTransactions', |
||
38 | 'approveActions', |
||
39 | 'rejectActions', |
||
40 | 'dealer', |
||
41 | 'dealerPst', |
||
42 | 'dealerType', |
||
43 | 'eWallet', |
||
44 | 'merchant', |
||
45 | 'merchantUser', |
||
46 | 'messageContent', |
||
47 | 'payByLinkPayment', |
||
48 | 'paymentPolicy', |
||
49 | 'paymentSystem', |
||
50 | 'paymentType', |
||
51 | 'recurringPayment', |
||
52 | 'recurringPlan', |
||
53 | 'recurringPlanCard' |
||
54 | ]; |
||
55 | |||
56 | private static $validQueryActions = [ |
||
57 | 'Transaction', |
||
58 | 'DealerTransaction', |
||
59 | 'SubDealerTransaction', |
||
60 | 'Installment', |
||
61 | 'Card', |
||
62 | 'CardExpiry', |
||
63 | 'Customer', |
||
64 | 'Session', |
||
65 | 'PayByLinkPayment', |
||
66 | 'Bin', |
||
67 | 'Campaign', |
||
68 | 'OnlineCampaign', |
||
69 | 'RecurringPlan', |
||
70 | 'PaymentSystems', |
||
71 | 'MerchantPaymentSystems', |
||
72 | 'MerchantProfile', |
||
73 | 'PaymentSystemData', |
||
74 | 'Points', |
||
75 | 'PaymentPolicy', |
||
76 | 'SplitPayment', |
||
77 | 'Merchant', |
||
78 | 'MerchantContent', |
||
79 | 'MerchantStatusHistory', |
||
80 | 'MerchantUser', |
||
81 | 'UserRolePermission', |
||
82 | 'Dealer', |
||
83 | 'DealerType', |
||
84 | 'DealerPst', |
||
85 | 'DealerStatusHistory', |
||
86 | 'MerchantUserDealers', |
||
87 | 'Groups', |
||
88 | 'ExecutiveReport', |
||
89 | 'TransactionRule' |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * @var Environment |
||
94 | */ |
||
95 | private $environment; |
||
96 | /** |
||
97 | * @var GuzzleClient |
||
98 | */ |
||
99 | private $guzzleClient; |
||
100 | |||
101 | private $logger; |
||
102 | |||
103 | private static $headers = [ |
||
104 | 'User-Agent' => 'MerchantSafeUnipayPhpSDK/1.0', |
||
105 | 'Accept' => 'application/json' |
||
106 | ]; |
||
107 | |||
108 | /** |
||
109 | * Client constructor. |
||
110 | * @param Environment $environment |
||
111 | * @param GuzzleClient $guzzleClient |
||
112 | * @param LoggerInterface $logger |
||
113 | */ |
||
114 | public function __construct(Environment $environment, GuzzleClient $guzzleClient, LoggerInterface $logger) |
||
120 | |||
121 | /** |
||
122 | * @param $name |
||
123 | * @param $arguments |
||
124 | * @throws BadMethodCallException |
||
125 | * @throws RequestException |
||
126 | * @throws InvalidArgumentException |
||
127 | * @return array |
||
128 | */ |
||
129 | public function __call(string $name, array $arguments) |
||
133 | |||
134 | /** |
||
135 | * @param $name |
||
136 | * @param $arguments |
||
137 | * @throws BadMethodCallException |
||
138 | * @throws RequestException |
||
139 | * @throws InvalidArgumentException |
||
140 | * @return array |
||
141 | */ |
||
142 | public function query(string $name, array $arguments) |
||
146 | |||
147 | private function getCallAction(string $name, array $arguments) |
||
168 | |||
169 | /** |
||
170 | * @param string $name |
||
171 | * @param array $arguments |
||
172 | * @param string $namespace |
||
173 | * @return ActionInterface |
||
174 | * @throws BadMethodCallException |
||
175 | * @throws InvalidArgumentException |
||
176 | */ |
||
177 | private function actionFactory(string $name, array $arguments, string $namespace) |
||
199 | |||
200 | private function requestAction(ActionInterface $action) |
||
212 | |||
213 | /** |
||
214 | * @param string $actionName |
||
215 | * @param array $headers |
||
216 | * @param array $queryParams |
||
217 | * @throws RequestException |
||
218 | * @return ResponseInterface |
||
219 | */ |
||
220 | private function httpRequest(string $actionName, array $headers, array $queryParams) |
||
236 | } |
||
237 |
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.