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 integer $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 ) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * User ID in WordPress. |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public function get_user_id() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set User ID used by WordPress. |
||
| 68 | * @param int $user_id |
||
| 69 | */ |
||
| 70 | public function set_user_id( $user_id ) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get user object. |
||
| 76 | * @return WP_User |
||
| 77 | */ |
||
| 78 | protected function get_user() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Store data from the Stripe API about this customer |
||
| 84 | */ |
||
| 85 | public function set_customer_data( $data ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get data from the Stripe API about this customer |
||
| 91 | */ |
||
| 92 | public function get_customer_data() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get default card/source |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function get_default_card() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create a customer via API. |
||
| 121 | * @param array $args |
||
| 122 | * @return WP_Error|int |
||
| 123 | */ |
||
| 124 | public function create_customer( $args = array() ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add a card for this stripe customer. |
||
| 168 | * @param string $token |
||
| 169 | * @param bool $retry |
||
| 170 | * @return WP_Error|int |
||
| 171 | */ |
||
| 172 | public function add_card( $token, $retry = true ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get a customers saved cards using their Stripe ID. Cached. |
||
| 223 | * |
||
| 224 | * @param string $customer_id |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function get_cards() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Delete a card from stripe. |
||
| 251 | * @param string $card_id |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function delete_card( $card_id ) { |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Set default card in Stripe |
||
| 269 | * @param string $card_id |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function set_default_card( $card_id ) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Deletes caches for this users cards. |
||
| 289 | */ |
||
| 290 | public function clear_cache() { |
||
| 295 | } |
||
| 296 |
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.