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:
Complex classes like Payant often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Payant, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Payant { |
||
27 | |||
28 | |||
29 | /** |
||
30 | * @var $private_key |
||
31 | */ |
||
32 | protected $private_key; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var $base_uri |
||
38 | * |
||
39 | */ |
||
40 | protected $base_uri = 'https://api.demo.payant.ng'; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * @var $client |
||
45 | * |
||
46 | */ |
||
47 | protected $client; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * [__construct sets the needed variables got from Payant config file] |
||
52 | */ |
||
53 | public function __construct() |
||
54 | { |
||
55 | $this->setKey(); |
||
56 | $this->setBaseUrl(); |
||
57 | $this->setRequestOptions(); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get Base Url from Payant config file |
||
62 | */ |
||
63 | public function setBaseUrl() |
||
64 | { |
||
65 | if(Config::get('payant.mode') == 'LIVE') |
||
66 | { |
||
67 | $this->base_uri = "https://api.payant.ng"; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get private key from Payant config file |
||
73 | */ |
||
74 | public function setKey() |
||
75 | { |
||
76 | $this->private_key = Config::get('payant.private_key'); |
||
77 | } |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Set options for making the Client request |
||
82 | */ |
||
83 | private function setRequestOptions() |
||
84 | { |
||
85 | $authorization_string = 'Bearer '. $this->private_key; |
||
86 | |||
87 | //Set up Guzzle |
||
88 | $this->client = new Client( [ |
||
89 | 'base_uri' => $this->base_uri, |
||
90 | 'protocols' => ['https'], |
||
91 | 'headers' => [ |
||
92 | 'Authorization' => $authorization_string, |
||
93 | 'Content-Type' => 'application/json', |
||
94 | 'Accept' => 'application/json' |
||
95 | ] |
||
96 | ]); |
||
97 | } |
||
98 | |||
99 | |||
100 | /** |
||
101 | * [getStates Get States in a country (Nigeria)] |
||
102 | * @return object [list of banks and their respective bank_ids] |
||
103 | */ |
||
104 | public function getBanks(){ |
||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * [resolveAccount description] |
||
112 | * @param array $client_data [description] |
||
113 | * Required fields - 'settlement_bank', 'account_number' |
||
114 | */ |
||
115 | public function resolveAccount( array $client_data){ |
||
127 | |||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * [addClient description] |
||
133 | * @param array $client_data [description] |
||
134 | * Required fields - 'first_name', 'last_name', 'email', 'phone' |
||
135 | * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number' |
||
136 | */ |
||
137 | public function addClient( array $client_data){ |
||
149 | |||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * [getClient Get client Details] |
||
155 | * @param string $client_id |
||
156 | * @return object |
||
157 | */ |
||
158 | View Code Duplication | public function getClient($client_id = null){ |
|
167 | |||
168 | |||
169 | |||
170 | |||
171 | |||
172 | /** |
||
173 | * [editClient - Edit Existing Client] |
||
174 | * @param string $client_id |
||
175 | * @param array $client_data |
||
176 | * Required fields - 'first_name', 'last_name', 'email', 'phone' |
||
177 | * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number' |
||
178 | */ |
||
179 | public function editClient( $client_id, array $client_data){ |
||
195 | |||
196 | |||
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * [deleteClient] |
||
202 | * @param string $client_id [description] |
||
203 | */ |
||
204 | View Code Duplication | public function deleteClient($client_id = null){ |
|
213 | |||
214 | |||
215 | |||
216 | |||
217 | |||
218 | /** |
||
219 | * [addInvoice description] |
||
220 | * @param string $client_id [Optional - if client_data is supplied] |
||
221 | * @param array|null $client_data [Optional - if client_id is supplied] |
||
222 | * Required Keys - 'first_name', 'last_name', 'email', 'phone' |
||
223 | * Optional - 'address', 'company_name', 'lga', 'state' |
||
224 | * @param string $due_date [Mandatory, Format - DD/MM/YYYY] |
||
225 | * @param string $fee_bearer [Mandatory] |
||
226 | * @param array $items [Mandatory] |
||
227 | */ |
||
228 | public function addInvoice($client_id, array $client_data, $due_date, $fee_bearer, array $items){ |
||
269 | |||
270 | |||
271 | |||
272 | |||
273 | /** |
||
274 | * [getInvoice ] |
||
275 | * @param string $reference_code [Mandatory - Invoice Reference Code] |
||
276 | * @return object |
||
277 | */ |
||
278 | View Code Duplication | public function getInvoice($reference_code){ |
|
287 | |||
288 | /** |
||
289 | * [sendInvoice] |
||
290 | * @param string $reference_code [Mandatory - Invoice Reference Code] |
||
291 | * @return object |
||
292 | */ |
||
293 | View Code Duplication | public function sendInvoice($reference_code = null){ |
|
302 | |||
303 | |||
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * [getInvoiceHistory] |
||
309 | * @param string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
||
310 | * @param string $start [Format - DD/MM/YYYY] |
||
311 | * @param string $end [Format - DD/MM/YYYY] |
||
312 | * @return object |
||
313 | */ |
||
314 | View Code Duplication | public function getInvoiceHistory($period, $start = null, $end = null){ |
|
325 | |||
326 | |||
327 | |||
328 | |||
329 | |||
330 | /** |
||
331 | * [deleteInvoice] |
||
332 | * @param string $reference_code [Mandatory - Invoice Reference Code] |
||
333 | * @return object |
||
334 | */ |
||
335 | View Code Duplication | public function deleteInvoice($reference_code){ |
|
344 | |||
345 | |||
346 | |||
347 | |||
348 | |||
349 | /** |
||
350 | * [addTransfer description] |
||
351 | * @param array $client_data [description] |
||
352 | * Required fields - 'first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number', |
||
353 | * Optional - 'address', 'company_name', 'type', |
||
354 | * @param string $amount [Mandatory] |
||
355 | */ |
||
356 | public function addTransfer(array $client_data, string $amount){ |
||
377 | |||
378 | |||
379 | |||
380 | |||
381 | |||
382 | /** |
||
383 | * [getTransfer ] |
||
384 | * @param string $reference_code [Mandatory - Transfer Reference Code] |
||
385 | * @return object |
||
386 | */ |
||
387 | View Code Duplication | public function getTransfer($reference_code){ |
|
396 | |||
397 | |||
398 | |||
399 | |||
400 | |||
401 | |||
402 | /** |
||
403 | * [getTransferHistory] |
||
404 | * @param string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
||
405 | * @param string $start [Format - DD/MM/YYYY] |
||
406 | * @param string $end [Format - DD/MM/YYYY] |
||
407 | * @return object |
||
408 | */ |
||
409 | View Code Duplication | public function getTransferHistory($period, $start = null, $end = null){ |
|
421 | |||
422 | |||
423 | |||
424 | |||
425 | |||
426 | /** |
||
427 | * [deleteTransfer] |
||
428 | * @param string $reference_code [Mandatory - Invoice Reference Code] |
||
429 | * @return object |
||
430 | */ |
||
431 | View Code Duplication | public function deleteTransfer($reference_code){ |
|
440 | |||
441 | |||
442 | |||
443 | |||
444 | |||
445 | |||
446 | /** |
||
447 | * [addPayment] |
||
448 | * @param string $reference_code [Mandatory - Invoice Reference Code] |
||
449 | * @param string $due_date [Mandatory - [Format - DD/MM/YYYY]] |
||
450 | * @param string $amount [Mandatory] |
||
451 | * @param string $channel [Mandatory - valid ["Cash", "BankTransfer", "POS", "Cheque"]] |
||
452 | */ |
||
453 | View Code Duplication | public function addPayment(string $reference_code, string $due_date, string $amount, string $channel){ |
|
485 | |||
486 | |||
487 | |||
488 | |||
489 | |||
490 | /** |
||
491 | * [getPayment] |
||
492 | * @param string $reference_code [Mandatory - Invoice Reference Code] |
||
493 | */ |
||
494 | View Code Duplication | public function getPayment($reference_code){ |
|
503 | |||
504 | |||
505 | |||
506 | |||
507 | |||
508 | /** |
||
509 | * [getPaymentHistory] |
||
510 | * @param string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
||
511 | * @param string $start [Format - DD/MM/YYYY || Optional if $period !== 'custom'] |
||
512 | * @param string $end [Format - DD/MM/YYYY || Optional if $period !== 'custom'] |
||
513 | * @return object |
||
514 | */ |
||
515 | View Code Duplication | public function getPaymentHistory(string $period, string $start, string $end){ |
|
527 | |||
528 | |||
529 | |||
530 | |||
531 | |||
532 | /** |
||
533 | * [addProduct] |
||
534 | * @param string $name [Mandatory - Product's name] |
||
535 | * @param string $description [Mandatory - Product's description] |
||
536 | * @param string $unit_cost [Mandatory - Product's unit cost] |
||
537 | * @param string $type [Mandatory - Product type 'product' or 'service'] |
||
538 | */ |
||
539 | View Code Duplication | public function addProduct(string $name, string $description, string $unit_cost, string $type){ |
|
572 | |||
573 | |||
574 | |||
575 | |||
576 | |||
577 | /** |
||
578 | * [getProduct] |
||
579 | * @param int $product_id [Mandatory - Product ID] |
||
580 | * @return object |
||
581 | */ |
||
582 | View Code Duplication | public function getProduct($product_id){ |
|
591 | |||
592 | |||
593 | |||
594 | |||
595 | |||
596 | /** |
||
597 | * [editProduct] |
||
598 | * @param string $product_id [Mandatory - Product ID] |
||
599 | * @param array $product_data [description] |
||
600 | * @return object |
||
601 | */ |
||
602 | public function editProduct($product_id, array $product_data){ |
||
629 | |||
630 | |||
631 | |||
632 | |||
633 | /** |
||
634 | * [getProducts] |
||
635 | * @return object |
||
636 | */ |
||
637 | public function getProducts(){ |
||
642 | |||
643 | |||
644 | |||
645 | |||
646 | /** |
||
647 | * [deleteProduct] |
||
648 | * @param $product_id [Mandatory - Product ID] |
||
649 | * @return object |
||
650 | */ |
||
651 | View Code Duplication | public function deleteProduct($product_id){ |
|
660 | |||
661 | |||
662 | |||
663 | /** |
||
664 | * [addPayment] |
||
665 | * @param string $method [Mandatory - request method <get | post | put | delete> ] |
||
666 | * @param string $url [Mandatory - url to send request to] |
||
667 | * @param array $params [data to post to request url] |
||
668 | */ |
||
669 | public function sendRequest($method, $url, $params=[]) |
||
688 | } |
||
689 |
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.