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 |
||
15 | class Transactor |
||
16 | { |
||
17 | protected $bearer; |
||
18 | /** |
||
19 | * The hashed password. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $password; |
||
24 | /** |
||
25 | * The transaction timestamp. |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $timestamp; |
||
30 | /** |
||
31 | * The transaction reference id |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $referenceId; |
||
36 | /** |
||
37 | * The amount to be deducted |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $amount; |
||
42 | /** |
||
43 | * The Mobile Subscriber number to be billed. |
||
44 | * Must be in format 2547XXXXXXXX. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $number; |
||
49 | /** |
||
50 | * The keys and data to fill in the request body. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $keys; |
||
55 | /** |
||
56 | * The request to be sent to the endpoint |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $request; |
||
61 | /** |
||
62 | * The generated transaction number by the Transactable implementer. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $transactionNumber; |
||
67 | /** |
||
68 | * The Guzzle Client used to make the request to the endpoint. |
||
69 | * |
||
70 | * @var Client |
||
71 | */ |
||
72 | private $client; |
||
73 | /** |
||
74 | * @var EquityRepository |
||
75 | */ |
||
76 | private $repository; |
||
77 | |||
78 | /** |
||
79 | * Transactor constructor. |
||
80 | * @param EquityRepository $repository |
||
81 | */ |
||
82 | public function __construct(EquityRepository $repository) |
||
92 | |||
93 | /** |
||
94 | * Process the transaction request. |
||
95 | * |
||
96 | * @param $amount |
||
97 | * @param $number |
||
98 | * @param $referenceId |
||
99 | * |
||
100 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
101 | */ |
||
102 | public function process($amount, $number, $referenceId) |
||
110 | |||
111 | /** |
||
112 | * Initialize the transaction. |
||
113 | */ |
||
114 | protected function initialize() |
||
121 | |||
122 | /** |
||
123 | * @throws EquityException |
||
124 | */ |
||
125 | private function setBearer() |
||
141 | |||
142 | /** |
||
143 | * Validate and execute the transaction. |
||
144 | * @todo Check Payment Status |
||
145 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
146 | */ |
||
147 | protected function handle() |
||
153 | |||
154 | public function checkPaymentStatus($result) |
||
162 | |||
163 | /** |
||
164 | * Create Payment Payload |
||
165 | */ |
||
166 | private function createPayment() |
||
186 | |||
187 | /** |
||
188 | * Set the transaction timestamp. |
||
189 | */ |
||
190 | private function setTimestamp() |
||
199 | |||
200 | /** |
||
201 | * Override the config pay bill number and pass key. |
||
202 | * |
||
203 | * @param $payBillNumber |
||
204 | * @param $payBillPassKey |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setPayBill($payBillNumber, $payBillPassKey) |
||
214 | |||
215 | |||
216 | /** |
||
217 | * Map the document fields with the transaction details. |
||
218 | * @throws \Exception |
||
219 | */ |
||
220 | protected function setupKeys() |
||
234 | |||
235 | /** |
||
236 | * Get the transaction number from the implementer. |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | private function getTransactionNumber(): string |
||
245 | |||
246 | /** |
||
247 | * @return string |
||
248 | */ |
||
249 | View Code Duplication | private function generateTransactionNumber(): string |
|
259 | |||
260 | /** |
||
261 | * Validate the required fields |
||
262 | */ |
||
263 | private function validateKeys() |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Execute the request. |
||
271 | * |
||
272 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
273 | */ |
||
274 | private function send() |
||
282 | |||
283 | /** |
||
284 | * Validate the response is a success, throw error if not. |
||
285 | * |
||
286 | * @param Response $response |
||
287 | * |
||
288 | * @throws TransactionException |
||
289 | * @throws EquityException |
||
290 | */ |
||
291 | private function validateResponse($response) |
||
306 | } |
||
307 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.