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 |
||
8 | class ExpressCheckout |
||
9 | { |
||
10 | // Integrate PayPal Request trait |
||
11 | use PayPalAPIRequest; |
||
12 | |||
13 | /** |
||
14 | * PayPal Processor Constructor. |
||
15 | */ |
||
16 | public function __construct() |
||
21 | |||
22 | /** |
||
23 | * Set ExpressCheckout API endpoints & options. |
||
24 | * |
||
25 | * @param array $credentials |
||
26 | * @param string $mode |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | protected function setExpressCheckoutOptions($credentials, $mode) |
||
50 | |||
51 | /** |
||
52 | * Function to perform SetExpressCheckout PayPal API operation. |
||
53 | * |
||
54 | * @param array $data |
||
55 | * @param bool $subscription |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | public function setExpressCheckout($data, $subscription = false) |
||
99 | |||
100 | /** |
||
101 | * Function to perform GetExpressCheckoutDetails PayPal API operation. |
||
102 | * |
||
103 | * @param string $token |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getExpressCheckoutDetails($token) |
||
115 | |||
116 | /** |
||
117 | * Function to perform DoExpressCheckoutPayment PayPal API operation. |
||
118 | * |
||
119 | * @param array $data |
||
120 | * @param string $token |
||
121 | * @param string $payerid |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function doExpressCheckoutPayment($data, $token, $payerid) |
||
149 | |||
150 | /** |
||
151 | * Function to perform DoCapture PayPal API operation. |
||
152 | * |
||
153 | * @param string $authorization_id Transaction ID |
||
154 | * @param float $amount Amount to capture |
||
155 | * @param string $complete Indicates whether or not this is the last capture. |
||
156 | * @param array $data Optional request fields |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = []) |
||
171 | |||
172 | /** |
||
173 | * Function to perform DoAuthorization PayPal API operation. |
||
174 | * |
||
175 | * @param string $authorization_id Transaction ID |
||
176 | * @param float $amount Amount to capture |
||
177 | * @param array $data Optional request fields |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | View Code Duplication | public function doAuthorization($authorization_id, $amount, $data = []) |
|
190 | |||
191 | /** |
||
192 | * Function to perform DoVoid PayPal API operation. |
||
193 | * |
||
194 | * @param string $authorization_id Transaction ID |
||
195 | * @param array $data Optional request fields |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | View Code Duplication | public function doVoid($authorization_id, $data = []) |
|
207 | |||
208 | /** |
||
209 | * Function to perform CreateBillingAgreement PayPal API operation. |
||
210 | * |
||
211 | * @param string $token |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | public function createBillingAgreement($token) |
||
223 | |||
224 | /** |
||
225 | * Function to perform CreateRecurringPaymentsProfile PayPal API operation. |
||
226 | * |
||
227 | * @param array $data |
||
228 | * @param string $token |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function createRecurringPaymentsProfile($data, $token) |
||
240 | |||
241 | /** |
||
242 | * Function to perform GetRecurringPaymentsProfileDetails PayPal API operation. |
||
243 | * |
||
244 | * @param string $id |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | public function getRecurringPaymentsProfileDetails($id) |
||
256 | |||
257 | /** |
||
258 | * Function to perform UpdateRecurringPaymentsProfile PayPal API operation. |
||
259 | * |
||
260 | * @param array $data |
||
261 | * @param string $id |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public function updateRecurringPaymentsProfile($data, $id) |
||
273 | |||
274 | /** |
||
275 | * Function to cancel RecurringPaymentsProfile on PayPal. |
||
276 | * |
||
277 | * @param string $id |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function cancelRecurringPaymentsProfile($id) |
||
290 | |||
291 | /** |
||
292 | * Function to suspend an active RecurringPaymentsProfile on PayPal. |
||
293 | * |
||
294 | * @param string $id |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | public function suspendRecurringPaymentsProfile($id) |
||
307 | |||
308 | /** |
||
309 | * Function to reactivate a suspended RecurringPaymentsProfile on PayPal. |
||
310 | * |
||
311 | * @param string $id |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public function reactivateRecurringPaymentsProfile($id) |
||
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.