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 | * Checks to see if error is of invalid request |
||
237 | * error and it is no such customer. |
||
238 | * |
||
239 | * @since 4.5.6 |
||
240 | * @param array $error |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function is_source_already_attached_error( $error ) { |
||
250 | |||
251 | /** |
||
252 | * Add a source for this stripe customer. |
||
253 | * @param string $source_id |
||
254 | * @return WP_Error|int |
||
255 | */ |
||
256 | public function add_source( $source_id ) { |
||
310 | |||
311 | /** |
||
312 | * Attaches a source to the Stripe customer. |
||
313 | * |
||
314 | * @param string $source_id The ID of the new source. |
||
315 | * @return object|WP_Error Either a source object, or a WP error. |
||
316 | */ |
||
317 | public function attach_source( $source_id ) { |
||
343 | |||
344 | /** |
||
345 | * Get a customers saved sources using their Stripe ID. |
||
346 | * |
||
347 | * @param string $customer_id |
||
348 | * @return array |
||
349 | */ |
||
350 | public function get_sources() { |
||
379 | |||
380 | /** |
||
381 | * Delete a source from stripe. |
||
382 | * @param string $source_id |
||
383 | */ |
||
384 | public function delete_source( $source_id ) { |
||
401 | |||
402 | /** |
||
403 | * Set default source in Stripe |
||
404 | * @param string $source_id |
||
405 | */ |
||
406 | public function set_default_source( $source_id ) { |
||
425 | |||
426 | /** |
||
427 | * Deletes caches for this users cards. |
||
428 | */ |
||
429 | public function clear_cache() { |
||
434 | |||
435 | /** |
||
436 | * Retrieves the Stripe Customer ID from the user meta. |
||
437 | * |
||
438 | * @param int $user_id The ID of the WordPress user. |
||
439 | * @return string|bool Either the Stripe ID or false. |
||
440 | */ |
||
441 | public function get_id_from_meta( $user_id ) { |
||
444 | |||
445 | /** |
||
446 | * Updates the current user with the right Stripe ID in the meta table. |
||
447 | * |
||
448 | * @param string $id The Stripe customer ID. |
||
449 | */ |
||
450 | public function update_id_in_meta( $id ) { |
||
453 | |||
454 | /** |
||
455 | * Deletes the user ID from the meta table with the right key. |
||
456 | */ |
||
457 | public function delete_id_from_meta() { |
||
460 | |||
461 | /** |
||
462 | * Recreates the customer for this user. |
||
463 | * |
||
464 | * @return string ID of the new Customer object. |
||
465 | */ |
||
466 | private function recreate_customer() { |
||
470 | } |
||
471 |
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.