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 Paystack { |
||
18 | |||
19 | /** |
||
20 | * Transaction Verification Successful |
||
21 | */ |
||
22 | const VS = 'Verification successful'; |
||
23 | |||
24 | /** |
||
25 | * Invalid Transaction reference |
||
26 | */ |
||
27 | const ITF = "Invalid transaction reference"; |
||
28 | |||
29 | /** |
||
30 | * Issue Secret Key from your Paystack Dashboard |
||
31 | * @var mixed |
||
32 | */ |
||
33 | protected $secretKey; |
||
34 | |||
35 | /** |
||
36 | * Instance of Client |
||
37 | * @var object |
||
38 | */ |
||
39 | protected $client; |
||
40 | |||
41 | /** |
||
42 | * Response from requests made to Paystack |
||
43 | * @var mixed |
||
44 | */ |
||
45 | protected $response; |
||
46 | |||
47 | /** |
||
48 | * Paystack API base Url |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $baseUrl; |
||
52 | |||
53 | /** |
||
54 | * Authorization Url - Paystack payment page |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $authorizationUrl; |
||
58 | |||
59 | public function __construct() |
||
65 | |||
66 | /** |
||
67 | * Get Base Url from Paystack config file |
||
68 | */ |
||
69 | public function setBaseUrl() |
||
73 | |||
74 | /** |
||
75 | * Get secret key from Paystack config file |
||
76 | * @return void |
||
77 | */ |
||
78 | public function setKey() |
||
82 | |||
83 | /** |
||
84 | * Set options for making the Client request |
||
85 | * @return void |
||
86 | */ |
||
87 | private function setRequestOptions() |
||
98 | |||
99 | /** |
||
100 | * Initiate a payment request to Paystack |
||
101 | */ |
||
102 | public function makePaymentRequest() |
||
114 | |||
115 | |||
116 | private function setHttpResponse($relativeUrl, $method, $body = []) |
||
125 | |||
126 | /** |
||
127 | * Get the authorization url from the callback response |
||
128 | */ |
||
129 | public function getAuthorizationUrl() |
||
137 | |||
138 | /** |
||
139 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
140 | * @return void |
||
141 | */ |
||
142 | private function verifyTransactionAtGateway() |
||
150 | |||
151 | /** |
||
152 | * True or false condition whether the transaction is verified |
||
153 | * @return boolean |
||
154 | */ |
||
155 | public function isTransactionVerificationValid() |
||
176 | |||
177 | /** |
||
178 | * Get Payment details if the transaction was verified successfully |
||
179 | * @return json |
||
180 | * @throws PaymentVerificationFailedException |
||
181 | */ |
||
182 | public function getPaymentData() |
||
190 | |||
191 | /** |
||
192 | * Fluent method to redirect to Paystack Payment Page |
||
193 | */ |
||
194 | public function redirectNow() |
||
198 | |||
199 | /** |
||
200 | * Get Access code from transaction callback respose |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getAccessCode() |
||
207 | |||
208 | /** |
||
209 | * Generate a Unique Transaction Reference |
||
210 | * @return string |
||
211 | */ |
||
212 | public function genTranxRef() |
||
216 | |||
217 | /** |
||
218 | * Get all the customers that have made transactions on your platform |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getAllCustomers() |
||
227 | |||
228 | /** |
||
229 | * Get all the plans that you have on Paystack |
||
230 | * @return array |
||
231 | */ |
||
232 | public function getAllPlans() |
||
238 | |||
239 | /** |
||
240 | * Get all the transactions that have happened overtime |
||
241 | * @return array |
||
242 | */ |
||
243 | public function getAllTransactions() |
||
249 | |||
250 | /** |
||
251 | * Get the whole response from a get operation |
||
252 | * @return array |
||
253 | */ |
||
254 | private function getResponse() |
||
258 | |||
259 | /** |
||
260 | * Get the data response from a get operation |
||
261 | * @return array |
||
262 | */ |
||
263 | private function getData() |
||
267 | |||
268 | /** |
||
269 | * Create a plan |
||
270 | */ |
||
271 | View Code Duplication | public function createPlan(){ |
|
288 | |||
289 | /** |
||
290 | * Fetch any plan based on its plan id or code |
||
291 | * @param $plan_code |
||
292 | * @return array |
||
293 | */ |
||
294 | public function fetchPlan($plan_code){ |
||
298 | |||
299 | /** |
||
300 | * Update any plan's details based on its id or code |
||
301 | * @param $plan_code |
||
302 | * @return array |
||
303 | */ |
||
304 | View Code Duplication | public function updatePlan($plan_code){ |
|
318 | |||
319 | /** |
||
320 | * Create a customer |
||
321 | * @return array |
||
322 | */ |
||
323 | View Code Duplication | public function createCustomer(){ |
|
336 | |||
337 | /** |
||
338 | * Fetch a customer based on id or code |
||
339 | * @param $customer_id |
||
340 | * @return array |
||
341 | */ |
||
342 | public function fetchCustomer($customer_id) |
||
347 | |||
348 | /** |
||
349 | * Update a customer's details based on their id or code |
||
350 | * @param $customer_id |
||
351 | * @return array |
||
352 | */ |
||
353 | View Code Duplication | public function updateCustomer($customer_id){ |
|
366 | |||
367 | /** |
||
368 | * Export tranactions in .CSV |
||
369 | * @return array |
||
370 | */ |
||
371 | public function exportTransactions(){ |
||
381 | |||
382 | /** |
||
383 | * Create a subscription to a plan from a customer. |
||
384 | * @return array |
||
385 | */ |
||
386 | View Code Duplication | public function createSubscription(){ |
|
396 | |||
397 | /** |
||
398 | * Enable a subscription using the subscription code and token |
||
399 | * @return array |
||
400 | */ |
||
401 | View Code Duplication | public function enableSubscription(){ |
|
410 | |||
411 | /** |
||
412 | * Disable a subscription using the subscription code and token |
||
413 | * @return array |
||
414 | */ |
||
415 | View Code Duplication | public function disableSubscription(){ |
|
424 | |||
425 | /** |
||
426 | * Fetch details about a certain subscription |
||
427 | * @param $subscription_id |
||
428 | * @return array |
||
429 | */ |
||
430 | public function fetchSubscription($subscription_id) |
||
435 | |||
436 | /** |
||
437 | * Create pages you can share with users using the returned slug |
||
438 | * @return array |
||
439 | */ |
||
440 | View Code Duplication | public function createPage(){ |
|
450 | |||
451 | /** |
||
452 | * Fetches all the pages the merchant has |
||
453 | * @return array |
||
454 | */ |
||
455 | public function getAllPages() |
||
460 | |||
461 | /** |
||
462 | * Fetch details about a certain page using its id or slug |
||
463 | * @param $page_id |
||
464 | * @return array |
||
465 | */ |
||
466 | public function fetchPage($page_id) |
||
471 | |||
472 | /** |
||
473 | * Update the details about a particular page |
||
474 | * @param $page_id |
||
475 | * @return array |
||
476 | */ |
||
477 | View Code Duplication | public function updatePage($page_id){ |
|
487 | |||
488 | } |
||
489 | |||
492 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: