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 |
||
17 | class StkPush extends ApiCore |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $number; |
||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $amount; |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $reference; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $description; |
||
35 | |||
36 | /** |
||
37 | * @param string $amount |
||
38 | * @return $this |
||
39 | * @throws \Exception |
||
40 | * @throws MpesaException |
||
41 | */ |
||
42 | View Code Duplication | public function request($amount): self |
|
50 | |||
51 | /** |
||
52 | * @param string $number |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function from($number): self |
||
60 | |||
61 | /** |
||
62 | * Set the mpesa reference |
||
63 | * |
||
64 | * @param string $reference |
||
65 | * @param string $description |
||
66 | * @return $this |
||
67 | * @throws \Exception |
||
68 | * @throws MpesaException |
||
69 | */ |
||
70 | public function usingReference($reference, $description): self |
||
80 | |||
81 | /** |
||
82 | * Send a payment request |
||
83 | * |
||
84 | * @param null|int $amount |
||
85 | * @param null|string $number |
||
86 | * @param null|string $reference |
||
87 | * @param null|string $description |
||
88 | * @return mixed |
||
89 | * @throws \Samerior\MobileMoney\Mpesa\Exceptions\MpesaException |
||
90 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | 1 | public function push($amount = null, $number = null, $reference = null, $description = null) |
|
117 | |||
118 | /** |
||
119 | * @param array $body |
||
120 | * @param array $response |
||
121 | * @return MpesaStkRequest|\Illuminate\Database\Eloquent\Model |
||
122 | * @throws \Exception |
||
123 | * @throws MpesaException |
||
124 | */ |
||
125 | private function saveStkRequest($body, $response) |
||
143 | |||
144 | /** |
||
145 | * Validate an initialized transaction. |
||
146 | * |
||
147 | * @param string|int $checkoutRequestID |
||
148 | * |
||
149 | * @return mixed |
||
150 | * @throws MpesaException |
||
151 | * @throws \Exception |
||
152 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
153 | */ |
||
154 | public function validate($checkoutRequestID) |
||
175 | } |
||
176 |
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.