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 Mpesa |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The common part of the MPesa API endpoints |
||
| 13 | * @var string $base_url |
||
| 14 | */ |
||
| 15 | private $base_url; |
||
| 16 | /** |
||
| 17 | * The consumer key |
||
| 18 | * @var string $consumer_key |
||
| 19 | */ |
||
| 20 | public $consumer_key; |
||
| 21 | /** |
||
| 22 | * The consumer key secret |
||
| 23 | * @var string $consumer_secret |
||
| 24 | */ |
||
| 25 | public $consumer_secret; |
||
| 26 | /** |
||
| 27 | * The MPesa Paybill number |
||
| 28 | * @var int $paybill |
||
| 29 | */ |
||
| 30 | public $paybill; |
||
| 31 | /** |
||
| 32 | * The Lipa Na MPesa paybill number |
||
| 33 | * @var int $lipa_na_mpesa |
||
| 34 | */ |
||
| 35 | public $lipa_na_mpesa; |
||
| 36 | /** |
||
| 37 | * The Lipa Na MPesa paybill number SAG Key |
||
| 38 | * @var string $lipa_na_mpesa_key |
||
| 39 | */ |
||
| 40 | public $lipa_na_mpesa_key; |
||
| 41 | /** |
||
| 42 | * The Mpesa portal Username |
||
| 43 | * @var string $initiator_username |
||
| 44 | */ |
||
| 45 | public $initiator_username; |
||
| 46 | /** |
||
| 47 | * The Mpesa portal Password |
||
| 48 | * @var string $initiator_password |
||
| 49 | */ |
||
| 50 | public $initiator_password; |
||
| 51 | /** |
||
| 52 | * The Callback common part of the URL eg "https://domain.com/callbacks/" |
||
| 53 | * @var string $initiator_password |
||
| 54 | */ |
||
| 55 | private $callback_baseurl; |
||
| 56 | /** |
||
| 57 | * The test phone number provided by safaricom. For developers |
||
| 58 | * @var string $test_msisdn |
||
| 59 | */ |
||
| 60 | private $test_msisdn; |
||
| 61 | /** |
||
| 62 | * The signed API credentials |
||
| 63 | * @var string $cred |
||
| 64 | */ |
||
| 65 | private $cred; |
||
| 66 | private $access_token; |
||
| 67 | |||
| 68 | /*Callbacks*/ |
||
| 69 | public $bctimeout; |
||
| 70 | public $bcresult; |
||
| 71 | public $bbtimeout; |
||
| 72 | public $bbresult; |
||
| 73 | public $baltimeout; |
||
| 74 | public $balresult; |
||
| 75 | public $statustimeout; |
||
| 76 | public $statusresult; |
||
| 77 | public $reversetimeout; |
||
| 78 | public $reverseresult; |
||
| 79 | public $cbvalidate; |
||
| 80 | public $cbconfirm; |
||
| 81 | public $lnmocallback; |
||
| 82 | /** |
||
| 83 | * Construct method |
||
| 84 | * |
||
| 85 | * Initializes the class with an array of API values. |
||
| 86 | * |
||
| 87 | * @param array $config |
||
| 88 | * @return void |
||
| 89 | * @throws exception if the values array is not valid |
||
| 90 | */ |
||
|
|
|||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | public function __construct() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Submit Request |
||
| 128 | * |
||
| 129 | * Handles submission of all API endpoints queries |
||
| 130 | * |
||
| 131 | * @param string $url The API endpoint URL |
||
| 132 | * @param json $data The data to POST to the endpoint $url |
||
| 133 | * @return object|boolean Curl response or FALSE on failure |
||
| 134 | * @throws exception if the Access Token is not valid |
||
| 135 | */ |
||
| 136 | |||
| 137 | public function setCred() |
||
| 148 | |||
| 149 | |||
| 150 | public function getAccessToken() |
||
| 171 | |||
| 172 | private function submit_request($url, $data) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Business to Client |
||
| 200 | * |
||
| 201 | * This method is used to send money to the clients Mpesa account. |
||
| 202 | * |
||
| 203 | * @param int $amount The amount to send to the client |
||
| 204 | * @param int $phone The phone number of the client in the format 2547xxxxxxxx |
||
| 205 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 206 | */ |
||
| 207 | |||
| 208 | public function b2c($amount, $phone, $command_id, $remarks) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Business to Business |
||
| 232 | * |
||
| 233 | * This method is used to send money to other business Mpesa paybills. |
||
| 234 | * |
||
| 235 | * @param int $amount The amount to send to the business |
||
| 236 | * @param int $shortcode The shortcode of the business to send to |
||
| 237 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 238 | */ |
||
| 239 | |||
| 240 | public function b2b($amount, $shortcode) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Client to Business |
||
| 264 | * |
||
| 265 | * This method is used to register URLs for callbacks when money is sent from the MPesa toolkit menu |
||
| 266 | * |
||
| 267 | * @param string $confirmURL The local URL that MPesa calls to confirm a payment |
||
| 268 | * @param string $ValidationURL The local URL that MPesa calls to validate a payment |
||
| 269 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 270 | */ |
||
| 271 | |||
| 272 | public function c2bRegisterUrls() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * C2B Simulation |
||
| 291 | * |
||
| 292 | * This method is used to simulate a C2B Transaction to test your ConfirmURL and ValidationURL in the Client to Business method |
||
| 293 | * |
||
| 294 | * @param int $amount The amount to send to Paybill number |
||
| 295 | * @param int $msisdn A dummy Safaricom phone number to simulate transaction in the format 2547xxxxxxxx |
||
| 296 | * @param string $ref A reference name for the transaction |
||
| 297 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 298 | */ |
||
| 299 | |||
| 300 | public function simulateC2B($amount, $msisdn, $ref) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check Balance |
||
| 317 | * |
||
| 318 | * Check Paybill balance |
||
| 319 | * |
||
| 320 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 321 | */ |
||
| 322 | public function check_balance() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Transaction status request |
||
| 342 | * |
||
| 343 | * This method is used to check a transaction status |
||
| 344 | * |
||
| 345 | * @param string $transaction ID eg LH7819VXPE |
||
| 346 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 347 | */ |
||
| 348 | |||
| 349 | View Code Duplication | public function status_request($transaction = 'LH7819VXPE') |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Transaction Reversal |
||
| 371 | * |
||
| 372 | * This method is used to reverse a transaction |
||
| 373 | * |
||
| 374 | * @param int $receiver Phone number in the format 2547xxxxxxxx |
||
| 375 | * @param string $trx_id Transaction ID of the Transaction you want to reverse eg LH7819VXPE |
||
| 376 | * @param int $amount The amount from the transaction to reverse |
||
| 377 | * @return object Curl Response from submit_request, FALSE on failure |
||
| 378 | */ |
||
| 379 | |||
| 380 | View Code Duplication | public function reverse_transaction($receiver, $trx_id, $amount) |
|
| 399 | |||
| 400 | /********************************************************************* |
||
| 401 | * |
||
| 402 | * LNMO APIs |
||
| 403 | * |
||
| 404 | * *******************************************************************/ |
||
| 405 | |||
| 406 | public function express($amount, $phone, $ref = "Payment", $desc = "Payment") |
||
| 442 | |||
| 443 | private function lnmoQuery($checkoutRequestID = null) |
||
| 464 | } |
||
| 465 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.