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 |
||
| 7 | class PaymentVerification |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Command to Pass to the PayuService. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $command = 'verify_payment'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Merchant Key. |
||
| 18 | * |
||
| 19 | * @var string|null |
||
| 20 | */ |
||
| 21 | protected $key = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Merchant Salt. |
||
| 25 | * |
||
| 26 | * @var string|null |
||
| 27 | */ |
||
| 28 | protected $salt = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Payu Service URL. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $url = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Original Response. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $response = []; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * PaymentVerification constructor. |
||
| 47 | * |
||
| 48 | * @param $txn_id |
||
| 49 | */ |
||
| 50 | public function __construct($txn_id) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Request for Verification Status. |
||
| 65 | * |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | public function request() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Simple data accessor. |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function simple() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Full Data Accessor. |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function full() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the Request Params for Verification. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getVerificationPostFields() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set the Verification Response. |
||
| 114 | * |
||
| 115 | * @throws \Exception |
||
| 116 | */ |
||
| 117 | protected function sendRequest() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Update Payu Payments Table Entries. |
||
| 141 | * |
||
| 142 | * @return boolean |
||
| 143 | */ |
||
| 144 | protected function updatePayuTransaction() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get Full Response for user. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | protected function getFullResponseData() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get Simple Response for user. |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | protected function getSimpleResponseData() |
||
| 217 | |||
| 218 | } |
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: