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 Give_Stripe_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 Give_Stripe_Customer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Give_Stripe_Customer { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Stripe Customer ID. |
||
| 25 | * |
||
| 26 | * @since 2.5.0 |
||
| 27 | * @access private |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $id = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Stripe Source ID. |
||
| 35 | * |
||
| 36 | * @since 2.5.0 |
||
| 37 | * @access private |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $source_id = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Donor Email. |
||
| 45 | * |
||
| 46 | * @since 2.5.0 |
||
| 47 | * @access private |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $donor_email = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Stripe Customer Data. |
||
| 55 | * |
||
| 56 | * @since 2.5.0 |
||
| 57 | * @access private |
||
| 58 | * |
||
| 59 | * @var \Stripe\Customer |
||
| 60 | */ |
||
| 61 | public $customer_data = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Stripe Gateway Object. |
||
| 65 | * |
||
| 66 | * @since 2.5.0 |
||
| 67 | * @access public |
||
| 68 | * |
||
| 69 | * @var array|Give_Stripe_Gateway |
||
| 70 | */ |
||
| 71 | public $stripe_gateway = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Attached Stripe Source to Customer. |
||
| 75 | * |
||
| 76 | * @since 2.5.0 |
||
| 77 | * @access public |
||
| 78 | * |
||
| 79 | * @var array|\Stripe\Source |
||
| 80 | */ |
||
| 81 | public $attached_source = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Check for card existence for customer. |
||
| 85 | * |
||
| 86 | * @since 2.5.0 |
||
| 87 | * @access public |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | public $is_card_exists = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Give_Stripe_Customer constructor. |
||
| 95 | * |
||
| 96 | * @param string $email Donor Email. |
||
| 97 | * @param string $source_id Stripe Source ID. |
||
| 98 | * |
||
| 99 | * @since 2.5.0 |
||
| 100 | * @access public |
||
| 101 | */ |
||
| 102 | public function __construct( $email, $source_id = '' ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get Stripe customer ID. |
||
| 112 | * |
||
| 113 | * @since 2.5.0 |
||
| 114 | * @access public |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function get_id() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set Stripe customer ID. |
||
| 124 | * |
||
| 125 | * @param string $id Stripe Customer ID. |
||
| 126 | * |
||
| 127 | * @since 2.5.0 |
||
| 128 | * @access public |
||
| 129 | */ |
||
| 130 | public function set_id( $id ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Store data from the Stripe API about this customer |
||
| 136 | * |
||
| 137 | * @param /Stripe/Customer $data Stripe Customer Object. |
||
| 138 | * |
||
| 139 | * @since 2.5.0 |
||
| 140 | * @access public |
||
| 141 | */ |
||
| 142 | public function set_customer_data( $data ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the Stripe customer object. If not found, create the customer with Stripe's API. |
||
| 148 | * Save the customer ID appropriately in the database. |
||
| 149 | * |
||
| 150 | * @since 2.5.0 |
||
| 151 | * @access public |
||
| 152 | * |
||
| 153 | * @return bool|\Stripe\Customer |
||
| 154 | */ |
||
| 155 | public function get_or_create_customer() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Create a Customer in Stripe. |
||
| 214 | * |
||
| 215 | * @since 2.5.0 |
||
| 216 | * @access public |
||
| 217 | * |
||
| 218 | * @return bool|\Stripe\Customer |
||
| 219 | */ |
||
| 220 | public function create_customer() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * This function is used to attach source to the customer, if not exists. |
||
| 298 | * |
||
| 299 | * @since 2.5.0 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | public function attach_source() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * This function will check whether the error says no such customer. |
||
| 392 | * |
||
| 393 | * @param \Stripe\Error\InvalidRequest $error Invalid Request Error. |
||
| 394 | * |
||
| 395 | * @since 2.5.0 |
||
| 396 | * @access public |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function is_no_such_customer_error( $error ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * This function will check whether the ID provided is Card ID? |
||
| 410 | * |
||
| 411 | * @param string $id Card ID. |
||
| 412 | * |
||
| 413 | * @since 2.5.0 |
||
| 414 | * @access public |
||
| 415 | * |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | public function is_card( $id ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * This function will check whether the ID provided is Source ID? |
||
| 427 | * |
||
| 428 | * @param string $id Source ID. |
||
| 429 | * |
||
| 430 | * @since 2.5.0 |
||
| 431 | * @access public |
||
| 432 | * |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | public function is_source( $id ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * This function will check whether the ID provided is Bank Account ID? |
||
| 444 | * |
||
| 445 | * @param string $id Source ID. |
||
| 446 | * |
||
| 447 | * @since 2.5.0 |
||
| 448 | * @access public |
||
| 449 | * |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function is_bank_account( $id ) { |
||
| 458 | |||
| 459 | } |
||
| 460 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.