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 WC_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 WC_Stripe_Customer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class WC_Stripe_Customer { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Stripe customer ID |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $id = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * WP User ID |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | private $user_id = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Data from API |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $customer_data = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constructor |
||
| 33 | * @param int $user_id The WP user ID |
||
| 34 | */ |
||
| 35 | public function __construct( $user_id = 0 ) { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get Stripe customer ID. |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function get_id() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set Stripe customer ID. |
||
| 52 | * @param [type] $id [description] |
||
|
|
|||
| 53 | */ |
||
| 54 | public function set_id( $id ) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * User ID in WordPress. |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | public function get_user_id() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set User ID used by WordPress. |
||
| 75 | * @param int $user_id |
||
| 76 | */ |
||
| 77 | public function set_user_id( $user_id ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get user object. |
||
| 83 | * @return WP_User |
||
| 84 | */ |
||
| 85 | protected function get_user() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Store data from the Stripe API about this customer |
||
| 91 | */ |
||
| 92 | public function set_customer_data( $data ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Generates the customer request, used for both creating and updating customers. |
||
| 98 | * |
||
| 99 | * @param array $args Additional arguments (optional). |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | protected function generate_customer_request( $args = array() ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Create a customer via API. |
||
| 158 | * @param array $args |
||
| 159 | * @return WP_Error|int |
||
| 160 | */ |
||
| 161 | public function create_customer( $args = array() ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Updates the Stripe customer through the API. |
||
| 184 | * |
||
| 185 | * @param array $args Additional arguments for the request (optional). |
||
| 186 | * @param bool $is_retry Whether the current call is a retry (optional, defaults to false). If true, then an exception will be thrown instead of further retries on error. |
||
| 187 | * |
||
| 188 | * @return string Customer ID |
||
| 189 | * |
||
| 190 | * @throws WC_Stripe_Exception |
||
| 191 | */ |
||
| 192 | public function update_customer( $args = array(), $is_retry = false ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Checks to see if error is of invalid request |
||
| 222 | * error and it is no such customer. |
||
| 223 | * |
||
| 224 | * @since 4.1.2 |
||
| 225 | * @param array $error |
||
| 226 | */ |
||
| 227 | public function is_no_such_customer_error( $error ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add a source for this stripe customer. |
||
| 237 | * @param string $source_id |
||
| 238 | * @return WP_Error|int |
||
| 239 | */ |
||
| 240 | public function add_source( $source_id ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get a customers saved sources using their Stripe ID. |
||
| 316 | * |
||
| 317 | * @param string $customer_id |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function get_sources() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Delete a source from stripe. |
||
| 352 | * @param string $source_id |
||
| 353 | */ |
||
| 354 | public function delete_source( $source_id ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set default source in Stripe |
||
| 374 | * @param string $source_id |
||
| 375 | */ |
||
| 376 | public function set_default_source( $source_id ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Deletes caches for this users cards. |
||
| 398 | */ |
||
| 399 | public function clear_cache() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Retrieves the Stripe Customer ID from the user meta. |
||
| 407 | * |
||
| 408 | * @param int $user_id The ID of the WordPress user. |
||
| 409 | * @return string|bool Either the Stripe ID or false. |
||
| 410 | */ |
||
| 411 | public function get_id_from_meta( $user_id ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Updates the current user with the right Stripe ID in the meta table. |
||
| 417 | * |
||
| 418 | * @param string $id The Stripe customer ID. |
||
| 419 | */ |
||
| 420 | public function update_id_in_meta( $id ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Deletes the user ID from the meta table with the right key. |
||
| 426 | */ |
||
| 427 | public function delete_id_from_meta() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Recreates the customer for this user. |
||
| 433 | * |
||
| 434 | * @return string ID of the new Customer object. |
||
| 435 | */ |
||
| 436 | private function recreate_customer() { |
||
| 440 | } |
||
| 441 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.