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() ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create a customer via API. |
||
| 148 | * @param array $args |
||
| 149 | * @return WP_Error|int |
||
| 150 | */ |
||
| 151 | public function create_customer( $args = array() ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Updates the Stripe customer through the API. |
||
| 174 | * |
||
| 175 | * @param array $args Additional arguments for the request (optional). |
||
| 176 | * @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. |
||
| 177 | * |
||
| 178 | * @return string Customer ID |
||
| 179 | * |
||
| 180 | * @throws WC_Stripe_Exception |
||
| 181 | */ |
||
| 182 | public function update_customer( $args = array(), $is_retry = false ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Checks to see if error is of invalid request |
||
| 212 | * error and it is no such customer. |
||
| 213 | * |
||
| 214 | * @since 4.1.2 |
||
| 215 | * @param array $error |
||
| 216 | */ |
||
| 217 | public function is_no_such_customer_error( $error ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Add a source for this stripe customer. |
||
| 227 | * @param string $source_id |
||
| 228 | * @return WP_Error|int |
||
| 229 | */ |
||
| 230 | public function add_source( $source_id ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get a customers saved sources using their Stripe ID. |
||
| 306 | * |
||
| 307 | * @param string $customer_id |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function get_sources() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Delete a source from stripe. |
||
| 338 | * @param string $source_id |
||
| 339 | */ |
||
| 340 | public function delete_source( $source_id ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set default source in Stripe |
||
| 360 | * @param string $source_id |
||
| 361 | */ |
||
| 362 | public function set_default_source( $source_id ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Deletes caches for this users cards. |
||
| 384 | */ |
||
| 385 | public function clear_cache() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Retrieves the Stripe Customer ID from the user meta. |
||
| 393 | * |
||
| 394 | * @param int $user_id The ID of the WordPress user. |
||
| 395 | * @return string|bool Either the Stripe ID or false. |
||
| 396 | */ |
||
| 397 | public function get_id_from_meta( $user_id ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Updates the current user with the right Stripe ID in the meta table. |
||
| 403 | * |
||
| 404 | * @param string $id The Stripe customer ID. |
||
| 405 | */ |
||
| 406 | public function update_id_in_meta( $id ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Deletes the user ID from the meta table with the right key. |
||
| 412 | */ |
||
| 413 | public function delete_id_from_meta() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Recreates the customer for this user. |
||
| 419 | * |
||
| 420 | * @return string ID of the new Customer object. |
||
| 421 | */ |
||
| 422 | private function recreate_customer() { |
||
| 426 | } |
||
| 427 |
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.