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 ExpressCheckout |
||
11 | { |
||
12 | // Integrate PayPal Request trait |
||
13 | use PayPalAPIRequest, PayPalTransactions, RecurringProfiles; |
||
14 | |||
15 | /** |
||
16 | * ExpressCheckout constructor. |
||
17 | * |
||
18 | * @param array $config |
||
19 | * |
||
20 | * @throws \Exception |
||
21 | */ |
||
22 | public function __construct(array $config = []) |
||
31 | |||
32 | /** |
||
33 | * Set ExpressCheckout API endpoints & options. |
||
34 | * |
||
35 | * @param array $credentials |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | public function setExpressCheckoutOptions($credentials) |
||
61 | |||
62 | /** |
||
63 | * Set cart item details for PayPal. |
||
64 | * |
||
65 | * @param array $items |
||
66 | * |
||
67 | * @return \Illuminate\Support\Collection |
||
68 | */ |
||
69 | protected function setCartItems($items) |
||
81 | |||
82 | /** |
||
83 | * Set Recurring payments details for SetExpressCheckout API call. |
||
84 | * |
||
85 | * @param array $data |
||
86 | * @param bool $subscription |
||
87 | */ |
||
88 | protected function setExpressCheckoutRecurringPaymentConfig($data, $subscription = false) |
||
106 | |||
107 | /** |
||
108 | * Set item subtotal if available. |
||
109 | * |
||
110 | * @param array $data |
||
111 | */ |
||
112 | protected function setItemSubTotal($data) |
||
116 | |||
117 | /** |
||
118 | * Set shipping amount if available. |
||
119 | * |
||
120 | * @param array $data |
||
121 | */ |
||
122 | protected function setShippingAmount($data) |
||
130 | |||
131 | /** |
||
132 | * Set shipping discount if available. |
||
133 | * |
||
134 | * @param array $data |
||
135 | */ |
||
136 | protected function setShippingDiscount($data) |
||
149 | |||
150 | /** |
||
151 | * Perform a SetExpressCheckout API call on PayPal. |
||
152 | * |
||
153 | * @param array $data |
||
154 | * @param bool $subscription |
||
155 | * |
||
156 | * @throws \Exception |
||
157 | * |
||
158 | * @return array|\Psr\Http\Message\StreamInterface |
||
159 | */ |
||
160 | public function setExpressCheckout($data, $subscription = false) |
||
189 | |||
190 | /** |
||
191 | * Perform a GetExpressCheckoutDetails API call on PayPal. |
||
192 | * |
||
193 | * @param string $token |
||
194 | * |
||
195 | * @throws \Exception |
||
196 | * |
||
197 | * @return array|\Psr\Http\Message\StreamInterface |
||
198 | */ |
||
199 | public function getExpressCheckoutDetails($token) |
||
207 | |||
208 | /** |
||
209 | * Perform DoExpressCheckoutPayment API call on PayPal. |
||
210 | * |
||
211 | * @param array $data |
||
212 | * @param string $token |
||
213 | * @param string $payerid |
||
214 | * |
||
215 | * @throws \Exception |
||
216 | * |
||
217 | * @return array|\Psr\Http\Message\StreamInterface |
||
218 | */ |
||
219 | public function doExpressCheckoutPayment($data, $token, $payerid) |
||
239 | |||
240 | /** |
||
241 | * Perform a DoAuthorization API call on PayPal. |
||
242 | * |
||
243 | * @param string $authorization_id Transaction ID |
||
244 | * @param float $amount Amount to capture |
||
245 | * @param array $data Optional request fields |
||
246 | * |
||
247 | * @throws \Exception |
||
248 | * |
||
249 | * @return array|\Psr\Http\Message\StreamInterface |
||
250 | */ |
||
251 | public function doAuthorization($authorization_id, $amount, $data = []) |
||
262 | |||
263 | /** |
||
264 | * Perform a DoCapture API call on PayPal. |
||
265 | * |
||
266 | * @param string $authorization_id Transaction ID |
||
267 | * @param float $amount Amount to capture |
||
268 | * @param string $complete Indicates whether or not this is the last capture. |
||
269 | * @param array $data Optional request fields |
||
270 | * |
||
271 | * @throws \Exception |
||
272 | * |
||
273 | * @return array|\Psr\Http\Message\StreamInterface |
||
274 | */ |
||
275 | public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = []) |
||
288 | |||
289 | /** |
||
290 | * Perform a DoReauthorization API call on PayPal to reauthorize an existing authorization transaction. |
||
291 | * |
||
292 | * @param string $authorization_id |
||
293 | * @param float $amount |
||
294 | * @param array $data |
||
295 | * |
||
296 | * @throws \Exception |
||
297 | * |
||
298 | * @return array|\Psr\Http\Message\StreamInterface |
||
299 | */ |
||
300 | public function doReAuthorization($authorization_id, $amount, $data = []) |
||
311 | |||
312 | /** |
||
313 | * Perform a DoVoid API call on PayPal. |
||
314 | * |
||
315 | * @param string $authorization_id Transaction ID |
||
316 | * @param array $data Optional request fields |
||
317 | * |
||
318 | * @throws \Exception |
||
319 | * |
||
320 | * @return array|\Psr\Http\Message\StreamInterface |
||
321 | */ |
||
322 | public function doVoid($authorization_id, $data = []) |
||
332 | |||
333 | /** |
||
334 | * Perform a CreateBillingAgreement API call on PayPal. |
||
335 | * |
||
336 | * @param string $token |
||
337 | * |
||
338 | * @throws \Exception |
||
339 | * |
||
340 | * @return array|\Psr\Http\Message\StreamInterface |
||
341 | */ |
||
342 | public function createBillingAgreement($token) |
||
350 | |||
351 | /** |
||
352 | * Perform a CreateRecurringPaymentsProfile API call on PayPal. |
||
353 | * |
||
354 | * @param array $data |
||
355 | * @param string $token |
||
356 | * |
||
357 | * @throws \Exception |
||
358 | * |
||
359 | * @return array|\Psr\Http\Message\StreamInterface |
||
360 | */ |
||
361 | public function createRecurringPaymentsProfile($data, $token) |
||
369 | |||
370 | /** |
||
371 | * Perform a GetRecurringPaymentsProfileDetails API call on PayPal. |
||
372 | * |
||
373 | * @param string $id |
||
374 | * |
||
375 | * @throws \Exception |
||
376 | * |
||
377 | * @return array|\Psr\Http\Message\StreamInterface |
||
378 | */ |
||
379 | public function getRecurringPaymentsProfileDetails($id) |
||
387 | |||
388 | /** |
||
389 | * Perform a UpdateRecurringPaymentsProfile API call on PayPal. |
||
390 | * |
||
391 | * @param array $data |
||
392 | * @param string $id |
||
393 | * |
||
394 | * @throws \Exception |
||
395 | * |
||
396 | * @return array|\Psr\Http\Message\StreamInterface |
||
397 | */ |
||
398 | public function updateRecurringPaymentsProfile($data, $id) |
||
406 | |||
407 | /** |
||
408 | * Change Recurring payment profile status on PayPal. |
||
409 | * |
||
410 | * @param string $id |
||
411 | * @param string $status |
||
412 | * |
||
413 | * @throws \Exception |
||
414 | * |
||
415 | * @return array|\Psr\Http\Message\StreamInterface |
||
416 | */ |
||
417 | protected function manageRecurringPaymentsProfileStatus($id, $status) |
||
426 | |||
427 | /** |
||
428 | * Perform a ManageRecurringPaymentsProfileStatus API call on PayPal to cancel a RecurringPaymentsProfile. |
||
429 | * |
||
430 | * @param string $id |
||
431 | * |
||
432 | * @throws \Exception |
||
433 | * |
||
434 | * @return array|\Psr\Http\Message\StreamInterface |
||
435 | */ |
||
436 | public function cancelRecurringPaymentsProfile($id) |
||
440 | |||
441 | /** |
||
442 | * Perform a ManageRecurringPaymentsProfileStatus API call on PayPal to suspend a RecurringPaymentsProfile. |
||
443 | * |
||
444 | * @param string $id |
||
445 | * |
||
446 | * @throws \Exception |
||
447 | * |
||
448 | * @return array|\Psr\Http\Message\StreamInterface |
||
449 | */ |
||
450 | public function suspendRecurringPaymentsProfile($id) |
||
454 | |||
455 | /** |
||
456 | * Perform a ManageRecurringPaymentsProfileStatus API call on PayPal to reactivate a RecurringPaymentsProfile. |
||
457 | * |
||
458 | * @param string $id |
||
459 | * |
||
460 | * @throws \Exception |
||
461 | * |
||
462 | * @return array|\Psr\Http\Message\StreamInterface |
||
463 | */ |
||
464 | public function reactivateRecurringPaymentsProfile($id) |
||
468 | } |
||
469 |
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.