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:
Complex classes like Customer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Customer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Customer extends Base |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @return Customer[] |
||
| 42 | */ |
||
| 43 | public static function all() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @param string $query |
||
| 51 | * @param int[] $ids |
||
| 52 | * @return Customer|Customer[] |
||
| 53 | */ |
||
| 54 | public static function fetch($query, $ids) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * |
||
| 61 | * @param array $attribs |
||
| 62 | * @return Result\Successful|Result\Error |
||
| 63 | */ |
||
| 64 | public static function create($attribs = []) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @param array $attribs |
||
| 72 | * @return Customer |
||
| 73 | */ |
||
| 74 | public static function createNoValidate($attribs = []) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @deprecated since version 2.3.0 |
||
| 81 | * @param string $queryString |
||
| 82 | * @return Result\Successful |
||
| 83 | */ |
||
| 84 | public static function createFromTransparentRedirect($queryString) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @deprecated since version 2.3.0 |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public static function createCustomerUrl() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * |
||
| 100 | * @throws Exception\NotFound |
||
| 101 | * @param string $id customer id |
||
| 102 | * @return Customer |
||
| 103 | */ |
||
| 104 | public static function find($id) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * |
||
| 111 | * @param int $customerId |
||
| 112 | * @param array $transactionAttribs |
||
| 113 | * @return Result\Successful|Result\Error |
||
| 114 | */ |
||
| 115 | public static function credit($customerId, $transactionAttribs) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * @throws Exception\ValidationError |
||
| 123 | * @param type $customerId |
||
| 124 | * @param type $transactionAttribs |
||
| 125 | * @return Transaction |
||
| 126 | */ |
||
| 127 | public static function creditNoValidate($customerId, $transactionAttribs) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * |
||
| 134 | * @throws Exception on invalid id or non-200 http response code |
||
| 135 | * @param int $customerId |
||
| 136 | * @return Result\Successful |
||
| 137 | */ |
||
| 138 | public static function delete($customerId) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * |
||
| 145 | * @param int $customerId |
||
| 146 | * @param array $transactionAttribs |
||
| 147 | * @return Transaction |
||
| 148 | */ |
||
| 149 | public static function sale($customerId, $transactionAttribs) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * |
||
| 156 | * @param int $customerId |
||
| 157 | * @param array $transactionAttribs |
||
| 158 | * @return Transaction |
||
| 159 | */ |
||
| 160 | public static function saleNoValidate($customerId, $transactionAttribs) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * |
||
| 167 | * @throws InvalidArgumentException |
||
| 168 | * @param string $query |
||
| 169 | * @return ResourceCollection |
||
| 170 | */ |
||
| 171 | public static function search($query) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * |
||
| 178 | * @throws Exception\Unexpected |
||
| 179 | * @param int $customerId |
||
| 180 | * @param array $attributes |
||
| 181 | * @return Result\Successful|Result\Error |
||
| 182 | */ |
||
| 183 | public static function update($customerId, $attributes) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * |
||
| 190 | * @throws Exception\Unexpected |
||
| 191 | * @param int $customerId |
||
| 192 | * @param array $attributes |
||
| 193 | * @return CustomerGateway |
||
| 194 | */ |
||
| 195 | public static function updateNoValidate($customerId, $attributes) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * |
||
| 202 | * @deprecated since version 2.3.0 |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public static function updateCustomerUrl() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * |
||
| 212 | * @deprecated since version 2.3.0 |
||
| 213 | * @param string $queryString |
||
| 214 | * @return Result\Successful|Result\Error |
||
| 215 | */ |
||
| 216 | public static function updateFromTransparentRedirect($queryString) |
||
| 220 | |||
| 221 | /* instance methods */ |
||
| 222 | |||
| 223 | /** |
||
| 224 | * sets instance properties from an array of values |
||
| 225 | * |
||
| 226 | * @ignore |
||
| 227 | * @access protected |
||
| 228 | * @param array $customerAttribs array of customer data |
||
| 229 | */ |
||
| 230 | protected function _initialize($customerAttribs) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * returns a string representation of the customer |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function __toString() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * returns false if comparing object is not a Customer, |
||
| 322 | * or is a Customer with a different id |
||
| 323 | * |
||
| 324 | * @param object $otherCust customer to compare against |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | public function isEqual($otherCust) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * returns an array containt all of the customer's payment methods |
||
| 334 | * |
||
| 335 | * @deprecated since version 3.1.0 - use the paymentMethods property directly |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | public function paymentMethods() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * returns the customer's default payment method |
||
| 346 | * |
||
| 347 | * @return CreditCard|PayPalAccount |
||
| 348 | */ |
||
| 349 | public function defaultPaymentMethod() |
||
| 354 | |||
| 355 | public static function _defaultPaymentMethodFilter($paymentMethod) |
||
| 359 | |||
| 360 | /* private class properties */ |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @access protected |
||
| 364 | * @var array registry of customer data |
||
| 365 | */ |
||
| 366 | protected $_attributes = [ |
||
| 367 | 'addresses' => '', |
||
| 368 | 'company' => '', |
||
| 369 | 'creditCards' => '', |
||
| 370 | 'email' => '', |
||
| 371 | 'fax' => '', |
||
| 372 | 'firstName' => '', |
||
| 373 | 'id' => '', |
||
| 374 | 'lastName' => '', |
||
| 375 | 'phone' => '', |
||
| 376 | 'createdAt' => '', |
||
| 377 | 'updatedAt' => '', |
||
| 378 | 'website' => '', |
||
| 379 | ]; |
||
| 380 | |||
| 381 | /** |
||
| 382 | * factory method: returns an instance of Customer |
||
| 383 | * to the requesting method, with populated properties |
||
| 384 | * |
||
| 385 | * @ignore |
||
| 386 | * @param array $attributes |
||
| 387 | * @return Customer |
||
| 388 | */ |
||
| 389 | public static function factory($attributes) |
||
| 395 | } |
||
| 396 | class_alias('Braintree\Customer', 'Braintree_Customer'); |
||
| 397 |