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 |
||
9 | class Gecharl |
||
10 | { |
||
11 | /** |
||
12 | * base url |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | private $baseUrl; |
||
17 | |||
18 | /** |
||
19 | * the cart session key |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $instanceName; |
||
24 | |||
25 | /** |
||
26 | * Flexible handle to the VTPass Configuration |
||
27 | * |
||
28 | * @var |
||
29 | */ |
||
30 | protected $config; |
||
31 | |||
32 | public function __construct($baseUrl, $instanceName, $config) |
||
38 | |||
39 | /** |
||
40 | * get instance name of the cart |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function getInstanceName() |
||
48 | |||
49 | private function withOAuth2() |
||
53 | |||
54 | /** |
||
55 | * Get Your wallet available balance, Wallet is identified by username set in gecharl config or environmental variable |
||
56 | * @return GecharlResponse |
||
57 | * @throws GecharlErrorException |
||
58 | */ |
||
59 | public function getWalletBalance(): GecharlResponse |
||
68 | |||
69 | /** |
||
70 | * Purchase Airtime with py specifying the network (i.e. mtn, glo, airtel, or 9mobile to buy airtime corresponding the provided telco service code) |
||
71 | * @param string $network Network ID e.g mtn, glo, airtel, or 9mobile |
||
72 | * @param int $amount The amount you wish to topup |
||
73 | * @param string $phoneNumber The phone number of the recipient of this service |
||
74 | * @return GecharlResponse |
||
75 | * |
||
76 | * @throws GecharlErrorException |
||
77 | */ |
||
78 | public function purchaseAirtime(string $network, int $amount, $phoneNumber): GecharlResponse |
||
91 | |||
92 | /** |
||
93 | * Get all data plan for all networks or for specified network only |
||
94 | * @param string|null $network |
||
95 | * @return GecharlResponse |
||
96 | * @throws GecharlErrorException |
||
97 | */ |
||
98 | public function getDataPlans(string $network = null): GecharlResponse |
||
109 | |||
110 | /** |
||
111 | * Buy data bundle |
||
112 | * @param string $network Unique network identification code e.g. mtn, glo, airtel... |
||
113 | * @param string $plan Plan code of data plan to subscribe to |
||
114 | * @param string $recipient Phone number to receive data subscription |
||
115 | * @return GecharlResponse |
||
116 | * @throws GecharlErrorException |
||
117 | */ |
||
118 | View Code Duplication | public function purchaseDataBundle(string $network, string $plan, string $recipient): GecharlResponse |
|
128 | |||
129 | /** |
||
130 | * You need to verify your meter number before purchasing. |
||
131 | * |
||
132 | * Please note the disco unique codes below: |
||
133 | * Ikaja Electricity = <strong>ie</strong> |
||
134 | * Eko Electricity = <strong>ekedc</strong> |
||
135 | * Enugu Electricity = <strong>eedc</strong> |
||
136 | * Kano Electricity = <strong>kano</strong> |
||
137 | * Port Harcourt Electricity = <strong>phed</strong> |
||
138 | * Abuja Electricity = <strong>abuja</strong> |
||
139 | * Ibadan Electricity = <strong>ibedc</strong> |
||
140 | * |
||
141 | * On successful validation for a prepaid meter number, a product_code key comes with it, which should be passed along other parameters when paying. |
||
142 | * |
||
143 | * |
||
144 | * @param string $disco Unique code of the Electricity distribution company the meter number is for |
||
145 | * @param string $meterNumber Meter Number to verify |
||
146 | * @param string $meterType Meter type i.e. <strong>prepaid</strong> or <strong>postpaid</strong> |
||
147 | * @return GecharlResponse |
||
148 | * @throws GecharlErrorException |
||
149 | */ |
||
150 | View Code Duplication | public function verifyMeterNumber(string $disco, string $meterNumber, string $meterType): GecharlResponse |
|
160 | |||
161 | /** |
||
162 | * Purchase Electricity |
||
163 | * |
||
164 | * Please note the disco unique codes below: |
||
165 | * Ikaja Electricity = <strong>ie</strong> |
||
166 | * Eko Electricity = <strong>ekedc</strong> |
||
167 | * Enugu Electricity = <strong>eedc</strong> |
||
168 | * Kano Electricity = <strong>kano</strong> |
||
169 | * Port Harcourt Electricity = <strong>phed</strong> |
||
170 | * Abuja Electricity = <strong>abuja</strong> |
||
171 | * Ibadan Electricity = <strong>ibedc</strong> |
||
172 | * |
||
173 | * |
||
174 | * @param string $disco Unique code of the Electricity distribution company the meter number is for |
||
175 | * @param string $meterNumber Meter Number to verify |
||
176 | * @param string $meterType Meter type i.e. <strong>prepaid</strong> or <strong>postpaid</strong> |
||
177 | * @param $amount |
||
178 | * @param string|null $productCode |
||
179 | * @return GecharlResponse |
||
180 | * @throws GecharlErrorException |
||
181 | */ |
||
182 | public function purchaseElectricity(string $disco, string $meterNumber, string $meterType, $amount, string $productCode = null): GecharlResponse |
||
198 | |||
199 | /** |
||
200 | * Multichoice(DSTV and GoTv) Smart Card Number/Decoder verification |
||
201 | * You need to verify your Smart card number before purchasing. |
||
202 | * |
||
203 | * @param string $multichoiceType DSTV|GOTV |
||
204 | * @param string $smartCardNumber Customer unique smart card number to subscribe |
||
205 | * @return GecharlResponse |
||
206 | * @throws GecharlErrorException |
||
207 | */ |
||
208 | View Code Duplication | public function verifySmartCardNumber(string $multichoiceType, string $smartCardNumber): GecharlResponse |
|
218 | |||
219 | /** |
||
220 | * Purchase DSTV or GoTv Cable Tv Plan |
||
221 | * @param string $multiChoiceType DSTV|GOTV |
||
222 | * @param string $smartCardNumber Customer unique smart card number to subscribe |
||
223 | * @param $amount |
||
224 | * @param string $productCode productCode as gotten from the verification call |
||
225 | * @param string $plan Unique product_code as gotten from the verification call for available plan for the provided SmartCard Number |
||
226 | * @param string $customerPhoneNumber |
||
227 | * @param string $customerName |
||
228 | * @param string|null $transactionId |
||
229 | * @return GecharlResponse |
||
230 | * @throws GecharlErrorException |
||
231 | */ |
||
232 | public function purchaseMultiChoice(string $multiChoiceType, string $smartCardNumber, $amount, string $productCode, string $plan, string $customerPhoneNumber = '', string $customerName = '', string $transactionId = null): GecharlResponse |
||
252 | |||
253 | |||
254 | /** |
||
255 | * StarTimes Smart Card Number/Decoder verification |
||
256 | * You need to verify your Smart card number before purchasing. |
||
257 | * |
||
258 | * @param string $smartCardNumber Customer unique smart card number to subscribe |
||
259 | * @return GecharlResponse |
||
260 | * @throws GecharlErrorException |
||
261 | */ |
||
262 | View Code Duplication | public function verifyStarTimesSmartCardNumber(string $smartCardNumber): GecharlResponse |
|
272 | |||
273 | /** |
||
274 | * Purchase StarTimes Cable Tv Plan |
||
275 | * The product_code is either <strong>NOVA</strong>, <strong>BASIC</strong>, <strong>SMART</strong>, <strong>CLASSIC</strong> or <strong>SUPER</strong> |
||
276 | * |
||
277 | * @param string $smartCardNumber Customer unique smart card number to subscribe |
||
278 | * @param $amount |
||
279 | * @param string $productCode productCode as gotten from the verification call |
||
280 | * @param string $plan Unique product_code as gotten from the verification call for available plan for the provided SmartCard Number |
||
281 | * @param string $customerPhoneNumber |
||
282 | * @param string $customerName |
||
283 | * @param string|null $transactionId |
||
284 | * @return GecharlResponse |
||
285 | * @throws GecharlErrorException |
||
286 | */ |
||
287 | public function purchaseStarTimes(string $smartCardNumber, $amount, string $productCode, string $plan, string $transactionId, string $customerPhoneNumber = '', string $customerName = ''): GecharlResponse |
||
306 | } |
||
307 |
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.