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 |
||
27 | class Payant { |
||
28 | |||
29 | |||
30 | /** |
||
31 | * @var $private_key |
||
32 | */ |
||
33 | protected $private_key; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var $base_uri |
||
39 | * |
||
40 | */ |
||
41 | protected $base_uri = 'https://api.demo.payant.ng'; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * @var $client |
||
46 | * |
||
47 | */ |
||
48 | protected $client; |
||
49 | |||
50 | |||
51 | /** |
||
52 | * [__construct sets the needed variables got from Payant config file] |
||
53 | */ |
||
54 | public function __construct() |
||
60 | |||
61 | /** |
||
62 | * Get Base Url from Payant config file |
||
63 | */ |
||
64 | public function setBaseUrl() |
||
71 | |||
72 | /** |
||
73 | * Get private key from Payant config file |
||
74 | */ |
||
75 | public function setKey() |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Set options for making the Client request |
||
83 | */ |
||
84 | private function setRequestOptions() |
||
99 | |||
100 | |||
101 | /** |
||
102 | * [getStates Get States in a country (Nigeria)] |
||
103 | * @return [object] [list of banks and their respective bank_ids] |
||
|
|||
104 | */ |
||
105 | public function getBanks(){ |
||
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * [resolveAccount description] |
||
113 | * @param array $client_data [description] |
||
114 | * Required fields - 'settlement_bank', 'account_number' |
||
115 | */ |
||
116 | public function resolveAccount( array $client_data){ |
||
128 | |||
129 | |||
130 | |||
131 | |||
132 | /** |
||
133 | * [addClient description] |
||
134 | * @param array $client_data [description] |
||
135 | * Required fields - 'first_name', 'last_name', 'email', 'phone' |
||
136 | * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number' |
||
137 | */ |
||
138 | public function addClient( array $client_data){ |
||
150 | |||
151 | |||
152 | |||
153 | |||
154 | /** |
||
155 | * [getClient Get client Details] |
||
156 | * @param [string] $client_id |
||
157 | * @return [object] |
||
158 | */ |
||
159 | View Code Duplication | public function getClient($client_id = null){ |
|
168 | |||
169 | |||
170 | |||
171 | |||
172 | |||
173 | /** |
||
174 | * [editClient - Edit Existing Client] |
||
175 | * @param [string] $client_id |
||
176 | * @param [array] $client_data |
||
177 | * Required fields - 'first_name', 'last_name', 'email', 'phone' |
||
178 | * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number' |
||
179 | */ |
||
180 | public function editClient( $client_id, array $client_data){ |
||
196 | |||
197 | |||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * [deleteClient] |
||
203 | * @param [string] $client_id [description] |
||
204 | */ |
||
205 | View Code Duplication | public function deleteClient($client_id = null){ |
|
214 | |||
215 | |||
216 | |||
217 | |||
218 | |||
219 | /** |
||
220 | * [addInvoice description] |
||
221 | * @param [string] $client_id [Optional - if client_data is supplied] |
||
222 | * @param array|null $client_data [Optional - if client_id is supplied] |
||
223 | * Required Keys - 'first_name', 'last_name', 'email', 'phone' |
||
224 | * Optional - 'address', 'company_name', 'lga', 'state' |
||
225 | * @param [string] $due_date [Mandatory, Format - DD/MM/YYYY] |
||
226 | * @param [string] $fee_bearer [Mandatory] |
||
227 | * @param array $items [Mandatory] |
||
228 | */ |
||
229 | public function addInvoice($client_id, array $client_data, $due_date, $fee_bearer, array $items){ |
||
270 | |||
271 | |||
272 | |||
273 | |||
274 | /** |
||
275 | * [getInvoice ] |
||
276 | * @param [string] $reference_code [Mandatory - Invoice Reference Code] |
||
277 | * @return [object] |
||
278 | */ |
||
279 | View Code Duplication | public function getInvoice($reference_code){ |
|
288 | |||
289 | /** |
||
290 | * [sendInvoice] |
||
291 | * @param [type] $reference_code [Mandatory - Invoice Reference Code] |
||
292 | * @return [object] |
||
293 | */ |
||
294 | View Code Duplication | public function sendInvoice($reference_code = null){ |
|
303 | |||
304 | |||
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * [getInvoiceHistory] |
||
310 | * @param [string] $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
||
311 | * @param [string] $start [Format - DD/MM/YYYY] |
||
312 | * @param [string] $end [Format - DD/MM/YYYY] |
||
313 | * @return [object] |
||
314 | */ |
||
315 | View Code Duplication | public function getInvoiceHistory($period, $start = null, $end = null){ |
|
343 | |||
344 | |||
345 | |||
346 | |||
347 | |||
348 | /** |
||
349 | * [deleteInvoice] |
||
350 | * @param [string] $reference_code [Mandatory - Invoice Reference Code] |
||
351 | * @return [object] |
||
352 | */ |
||
353 | View Code Duplication | public function deleteInvoice($reference_code){ |
|
362 | |||
363 | |||
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * [addTransfer description] |
||
369 | * @param array $client_data [description] |
||
370 | * Required fields - 'first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number', |
||
371 | * Optional - 'address', 'company_name', 'type', |
||
372 | * @param [string] $amount [Mandatory] |
||
373 | */ |
||
374 | public function addTransfer(array $client_data, string $amount){ |
||
395 | |||
396 | |||
397 | |||
398 | |||
399 | |||
400 | /** |
||
401 | * [getTransfer ] |
||
402 | * @param [string] $reference_code [Mandatory - Transfer Reference Code] |
||
403 | * @return [object] |
||
404 | */ |
||
405 | View Code Duplication | public function getTransfer($reference_code){ |
|
414 | |||
415 | |||
416 | |||
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * [getTransferHistory] |
||
422 | * @param [string] $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
||
423 | * @param [string] $start [Format - DD/MM/YYYY] |
||
424 | * @param [string] $end [Format - DD/MM/YYYY] |
||
425 | * @return [object] |
||
426 | */ |
||
427 | View Code Duplication | public function getTransferHistory($period, $start = null, $end = null){ |
|
455 | |||
456 | |||
457 | |||
458 | |||
459 | |||
460 | /** |
||
461 | * [deleteTransfer] |
||
462 | * @param [string] $reference_code [Mandatory - Invoice Reference Code] |
||
463 | * @return [object] |
||
464 | */ |
||
465 | View Code Duplication | public function deleteTransfer($reference_code){ |
|
474 | |||
475 | |||
476 | |||
477 | |||
478 | |||
479 | |||
480 | /** |
||
481 | * [addPayment] |
||
482 | * @param [string] $reference_code [Mandatory - Invoice Reference Code] |
||
483 | * @param [string] $date [Mandatory - [Format - DD/MM/YYYY]] |
||
484 | * @param [string] $amount [Mandatory] |
||
485 | * @param [string] $channel [Mandatory - valid ["Cash", "BankTransfer", "POS", "Cheque"]] |
||
486 | */ |
||
487 | View Code Duplication | public function addPayment(string $reference_code, string $date, string $amount, string $channel){ |
|
519 | |||
520 | |||
521 | |||
522 | |||
523 | |||
524 | /** |
||
525 | * [getPayment] |
||
526 | * @param [string] $reference_code [Mandatory - Invoice Reference Code] |
||
527 | */ |
||
528 | View Code Duplication | public function getPayment($reference_code){ |
|
537 | |||
538 | |||
539 | |||
540 | |||
541 | |||
542 | /** |
||
543 | * [getPaymentHistory] |
||
544 | * @param [string] $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
||
545 | * @param [string] $start [Format - DD/MM/YYYY || Optional if $period !== 'custom'] |
||
546 | * @param [string] $end [Format - DD/MM/YYYY || Optional if $period !== 'custom'] |
||
547 | * @return [object] |
||
548 | */ |
||
549 | View Code Duplication | public function getPaymentHistory(string $period, string $start, string $end){ |
|
577 | |||
578 | |||
579 | |||
580 | |||
581 | |||
582 | /** |
||
583 | * [addProduct] |
||
584 | * @param string $name [Mandatory - Product's name] |
||
585 | * @param string $description [Mandatory - Product's description] |
||
586 | * @param string $unit_cost [Mandatory - Product's unit cost] |
||
587 | * @param string $type [Mandatory - Product type 'product' or 'service'] |
||
588 | */ |
||
589 | View Code Duplication | public function addProduct(string $name, string $description, string $unit_cost, string $type){ |
|
622 | |||
623 | |||
624 | |||
625 | |||
626 | |||
627 | /** |
||
628 | * [getProduct] |
||
629 | * @param [int] $product_id [Mandatory - Product ID] |
||
630 | * @return [object] |
||
631 | */ |
||
632 | View Code Duplication | public function getProduct($product_id){ |
|
641 | |||
642 | |||
643 | |||
644 | |||
645 | |||
646 | /** |
||
647 | * [editProduct] |
||
648 | * @param string $product_id [Mandatory - Product ID] |
||
649 | * @param array $product_data [description] |
||
650 | * @return object |
||
651 | */ |
||
652 | public function editProduct($product_id, array $product_data){ |
||
679 | |||
680 | |||
681 | |||
682 | |||
683 | /** |
||
684 | * [getProducts] |
||
685 | * @return object |
||
686 | */ |
||
687 | public function getProducts(){ |
||
692 | |||
693 | |||
694 | |||
695 | |||
696 | /** |
||
697 | * [deleteProduct] |
||
698 | * @param $product_id [Mandatory - Product ID] |
||
699 | * @return object |
||
700 | */ |
||
701 | View Code Duplication | public function deleteProduct($product_id){ |
|
710 | |||
711 | |||
712 | |||
713 | /** |
||
714 | * [addPayment] |
||
715 | * @param [string] $method [Mandatory - request method <get | post | put | delete> ] |
||
716 | * @param [string] $url [Mandatory - url to send request to] |
||
717 | * @param [array] $params [data to post to request url] |
||
718 | */ |
||
719 | public function sendRequest($method, $url, $params=[]) |
||
738 | } |
||
739 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.