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 |
||
12 | class Simulate extends ApiCore |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $amount; |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $number; |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $command = 'CustomerPayBillOnline'; |
||
26 | //CustomerBuyGoodsOnline |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $reference = 'Testing'; |
||
31 | |||
32 | /** |
||
33 | * Set the request amount to be deducted. |
||
34 | * |
||
35 | * @param int $amount |
||
36 | * |
||
37 | * @return $this |
||
38 | * @throws \Exception |
||
39 | * @throws MpesaException |
||
40 | */ |
||
41 | View Code Duplication | public function request($amount): self |
|
49 | |||
50 | /** |
||
51 | * Set the Mobile Subscriber Number to deduct the amount from. |
||
52 | * Must be in format 2547XXXXXXXX. |
||
53 | * |
||
54 | * @param string $number |
||
55 | * |
||
56 | * @return $this |
||
57 | * @throws MpesaException |
||
58 | */ |
||
59 | public function from($number): self |
||
67 | |||
68 | /** |
||
69 | * Set the product reference number to bill the account. |
||
70 | * |
||
71 | * @param int $reference |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function usingReference($reference): self |
||
80 | |||
81 | /** |
||
82 | * Set the unique command for this transaction type. |
||
83 | * |
||
84 | * @param string $command |
||
85 | * |
||
86 | * @return $this |
||
87 | * @throws MpesaException |
||
88 | */ |
||
89 | public function setCommand($command): self |
||
97 | |||
98 | /** |
||
99 | * Prepare the transaction simulation request |
||
100 | * |
||
101 | * @param int $amount |
||
102 | * @param int $number |
||
103 | * @param string $reference |
||
104 | * @param string $command |
||
105 | * @return mixed |
||
106 | * @throws MpesaException |
||
107 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
108 | */ |
||
109 | public function push($number = null, $amount = null, $reference = null, $command = null) |
||
129 | } |
||
130 |
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.