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 |
||
| 8 | class BraintreeForm extends Model |
||
| 9 | { |
||
| 10 | |||
| 11 | public $amount; |
||
|
|
|||
| 12 | public $orderId; |
||
| 13 | public $paymentMethodToken; |
||
| 14 | |||
| 15 | public $creditCard_number; |
||
| 16 | public $creditCard_cvv; |
||
| 17 | public $creditCard_expirationMonth; |
||
| 18 | public $creditCard_expirationYear; |
||
| 19 | public $creditCard_expirationDate; |
||
| 20 | public $creditCard_name; |
||
| 21 | |||
| 22 | public $customer_firstName; |
||
| 23 | public $customer_lastName; |
||
| 24 | public $customer_company; |
||
| 25 | public $customer_phone; |
||
| 26 | public $customer_fax; |
||
| 27 | public $customer_website; |
||
| 28 | public $customer_email; |
||
| 29 | |||
| 30 | public $billing_firstName; |
||
| 31 | public $billing_lastName; |
||
| 32 | public $billing_company; |
||
| 33 | public $billing_streetAddress; |
||
| 34 | public $billing_extendedAddress; |
||
| 35 | public $billing_locality; |
||
| 36 | public $billing_region; |
||
| 37 | public $billing_postalCode; |
||
| 38 | public $billing_countryCodeAlpha2; |
||
| 39 | |||
| 40 | public $shipping_firstName; |
||
| 41 | public $shipping_lastName; |
||
| 42 | public $shipping_company; |
||
| 43 | public $shipping_streetAddress; |
||
| 44 | public $shipping_extendedAddress; |
||
| 45 | public $shipping_locality; |
||
| 46 | public $shipping_region; |
||
| 47 | public $shipping_postalCode; |
||
| 48 | public $shipping_countryCodeAlpha2; |
||
| 49 | |||
| 50 | public $customerId; |
||
| 51 | |||
| 52 | public function rules() |
||
| 91 | |||
| 92 | public function attributeLabels() |
||
| 130 | |||
| 131 | public function getValuesFromAttributes() |
||
| 146 | |||
| 147 | public function send() |
||
| 154 | |||
| 155 | View Code Duplication | public function sale() |
|
| 156 | { |
||
| 157 | $return = \Yii::$app->get('braintree')->singleCharge(); |
||
| 158 | if ($return['status'] === false) { |
||
| 159 | $this->addErrorFromResponse($return['result']); |
||
| 160 | return false; |
||
| 161 | } else { |
||
| 162 | return $return; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | View Code Duplication | public function saleFromVault() |
|
| 167 | { |
||
| 168 | $return = \Yii::$app->get('braintree')->singleCharge(); |
||
| 169 | if ($return['status'] === false) { |
||
| 170 | $this->addErrorFromResponse($return['result']); |
||
| 171 | return false; |
||
| 172 | } else { |
||
| 173 | return $return; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | View Code Duplication | public function customer() |
|
| 178 | { |
||
| 179 | $return = \Yii::$app->get('braintree')->saveCustomer(); |
||
| 180 | if ($return['status'] === false) { |
||
| 181 | $this->addErrorFromResponse($return['result']); |
||
| 182 | return false; |
||
| 183 | } else { |
||
| 184 | return $return; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | View Code Duplication | public function creditCard() |
|
| 189 | { |
||
| 190 | $return = \Yii::$app->get('braintree')->saveCreditCard(); |
||
| 191 | if ($return['status'] === false) { |
||
| 192 | $this->addErrorFromResponse($return['result']); |
||
| 193 | return false; |
||
| 194 | } else { |
||
| 195 | return $return; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | View Code Duplication | public function address() |
|
| 200 | { |
||
| 201 | $return = \Yii::$app->get('braintree')->saveAddress(); |
||
| 202 | if ($return['status'] === false) { |
||
| 203 | $this->addErrorFromResponse($return['result']); |
||
| 204 | return false; |
||
| 205 | } else { |
||
| 206 | return $return; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * This add error from braintree response. |
||
| 212 | * @param $result \Braintree_Result_Error |
||
| 213 | */ |
||
| 214 | public function addErrorFromResponse($result) |
||
| 228 | |||
| 229 | } |