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 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class WC_Stripe { |
||
45 | |||
46 | /** |
||
47 | * @var Singleton The reference the *Singleton* instance of this class |
||
48 | */ |
||
49 | private static $instance; |
||
50 | |||
51 | /** |
||
52 | * @var Reference to logging class. |
||
53 | */ |
||
54 | private static $log; |
||
55 | |||
56 | /** |
||
57 | * Returns the *Singleton* instance of this class. |
||
58 | * |
||
59 | * @return Singleton The *Singleton* instance. |
||
60 | */ |
||
61 | public static function get_instance() { |
||
67 | |||
68 | /** |
||
69 | * Private clone method to prevent cloning of the instance of the |
||
70 | * *Singleton* instance. |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | private function __clone() {} |
||
75 | |||
76 | /** |
||
77 | * Private unserialize method to prevent unserializing of the *Singleton* |
||
78 | * instance. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | private function __wakeup() {} |
||
83 | |||
84 | /** |
||
85 | * Flag to indicate whether or not we need to load code for / support subscriptions. |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | private $subscription_support_enabled = false; |
||
90 | |||
91 | /** |
||
92 | * Flag to indicate whether or not we need to load support for pre-orders. |
||
93 | * |
||
94 | * @since 3.0.3 |
||
95 | * |
||
96 | * @var bool |
||
97 | */ |
||
98 | private $pre_order_enabled = false; |
||
99 | |||
100 | /** |
||
101 | * Notices (array) |
||
102 | * @var array |
||
103 | */ |
||
104 | public $notices = array(); |
||
105 | |||
106 | /** |
||
107 | * Protected constructor to prevent creating a new instance of the |
||
108 | * *Singleton* via the `new` operator from outside of this class. |
||
109 | */ |
||
110 | protected function __construct() { |
||
115 | |||
116 | /** |
||
117 | * Init the plugin after plugins_loaded so environment variables are set. |
||
118 | */ |
||
119 | public function init() { |
||
144 | |||
145 | /** |
||
146 | * Allow this class and other classes to add slug keyed notices (to avoid duplication) |
||
147 | */ |
||
148 | public function add_admin_notice( $slug, $class, $message ) { |
||
154 | |||
155 | /** |
||
156 | * The backup sanity check, in case the plugin is activated in a weird way, |
||
157 | * or the environment changes after activation. Also handles upgrade routines. |
||
158 | */ |
||
159 | public function check_environment() { |
||
185 | |||
186 | /** |
||
187 | * Updates the plugin version in db |
||
188 | * |
||
189 | * @since 3.1.0 |
||
190 | * @version 3.1.0 |
||
191 | * @return bool |
||
192 | */ |
||
193 | private static function _update_plugin_version() { |
||
199 | |||
200 | /** |
||
201 | * Dismiss the Google Payment Request API Feature notice. |
||
202 | * |
||
203 | * @since 3.1.0 |
||
204 | * @version 3.1.0 |
||
205 | */ |
||
206 | public function dismiss_request_api_notice() { |
||
209 | |||
210 | /** |
||
211 | * Dismiss the Apple Pay Feature notice. |
||
212 | * |
||
213 | * @since 3.1.0 |
||
214 | * @version 3.1.0 |
||
215 | */ |
||
216 | public function dismiss_apple_pay_notice() { |
||
219 | |||
220 | /** |
||
221 | * Handles upgrade routines. |
||
222 | * |
||
223 | * @since 3.1.0 |
||
224 | * @version 3.1.0 |
||
225 | */ |
||
226 | public function install() { |
||
233 | |||
234 | /** |
||
235 | * Checks the environment for compatibility problems. Returns a string with the first incompatibility |
||
236 | * found or false if the environment has no problems. |
||
237 | */ |
||
238 | static function get_environment_warning() { |
||
261 | |||
262 | /** |
||
263 | * Adds plugin action links |
||
264 | * |
||
265 | * @since 1.0.0 |
||
266 | */ |
||
267 | public function plugin_action_links( $links ) { |
||
277 | |||
278 | /** |
||
279 | * Get setting link. |
||
280 | * |
||
281 | * @since 1.0.0 |
||
282 | * |
||
283 | * @return string Setting link |
||
284 | */ |
||
285 | public function get_setting_link() { |
||
292 | |||
293 | /** |
||
294 | * Display any notices we've collected thus far (e.g. for connection, disconnection) |
||
295 | */ |
||
296 | public function admin_notices() { |
||
342 | |||
343 | /** |
||
344 | * Initialize the gateway. Called very early - in the context of the plugins_loaded action |
||
345 | * |
||
346 | * @since 1.0.0 |
||
347 | */ |
||
348 | public function init_gateways() { |
||
382 | |||
383 | /** |
||
384 | * Add the gateways to WooCommerce |
||
385 | * |
||
386 | * @since 1.0.0 |
||
387 | */ |
||
388 | public function add_gateways( $methods ) { |
||
396 | |||
397 | /** |
||
398 | * List of currencies supported by Stripe that has no decimals. |
||
399 | * |
||
400 | * @return array $currencies |
||
401 | */ |
||
402 | public static function no_decimal_currencies() { |
||
421 | |||
422 | /** |
||
423 | * Stripe uses smallest denomination in currencies such as cents. |
||
424 | * We need to format the returned currency from Stripe into human readable form. |
||
425 | * |
||
426 | * @param object $balance_transaction |
||
427 | * @param string $type Type of number to format |
||
428 | */ |
||
429 | public static function format_number( $balance_transaction, $type = 'fee' ) { |
||
448 | |||
449 | /** |
||
450 | * Capture payment when the order is changed from on-hold to complete or processing |
||
451 | * |
||
452 | * @param int $order_id |
||
453 | */ |
||
454 | public function capture_payment( $order_id ) { |
||
455 | $order = wc_get_order( $order_id ); |
||
456 | |||
457 | if ( 'stripe' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->payment_method : $order->get_payment_method() ) ) { |
||
458 | $charge = get_post_meta( $order_id, '_stripe_charge_id', true ); |
||
459 | $captured = get_post_meta( $order_id, '_stripe_charge_captured', true ); |
||
460 | |||
461 | if ( $charge && 'no' === $captured ) { |
||
462 | $result = WC_Stripe_API::request( array( |
||
463 | 'amount' => $order->get_total() * 100, |
||
464 | 'expand[]' => 'balance_transaction', |
||
465 | ), 'charges/' . $charge . '/capture' ); |
||
466 | |||
467 | if ( is_wp_error( $result ) ) { |
||
468 | $order->add_order_note( __( 'Unable to capture charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() ); |
||
469 | } else { |
||
470 | $order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); |
||
471 | update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ); |
||
472 | |||
473 | // Store other data such as fees |
||
474 | update_post_meta( $order_id, 'Stripe Payment ID', $result->id ); |
||
475 | |||
476 | View Code Duplication | if ( isset( $result->balance_transaction ) && isset( $result->balance_transaction->fee ) ) { |
|
477 | // Fees and Net needs to both come from Stripe to be accurate as the returned |
||
478 | // values are in the local currency of the Stripe account, not from WC. |
||
479 | $fee = ! empty( $result->balance_transaction->fee ) ? self::format_number( $result->balance_transaction, 'fee' ) : 0; |
||
480 | $net = ! empty( $result->balance_transaction->net ) ? self::format_number( $result->balance_transaction, 'net' ) : 0; |
||
481 | update_post_meta( $order_id, 'Stripe Fee', $fee ); |
||
482 | update_post_meta( $order_id, 'Net Revenue From Stripe', $net ); |
||
483 | } |
||
484 | } |
||
485 | } |
||
486 | } |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Cancel pre-auth on refund/cancellation |
||
491 | * |
||
492 | * @param int $order_id |
||
493 | */ |
||
494 | public function cancel_payment( $order_id ) { |
||
495 | $order = wc_get_order( $order_id ); |
||
496 | |||
497 | if ( 'stripe' === ( version_compare( WC_VERSION, '3.0.0', '<' ) ? $order->payment_method : $order->get_payment_method() ) ) { |
||
498 | $charge = get_post_meta( $order_id, '_stripe_charge_id', true ); |
||
499 | |||
500 | if ( $charge ) { |
||
501 | $result = WC_Stripe_API::request( array( |
||
502 | 'amount' => $order->get_total() * 100, |
||
503 | ), 'charges/' . $charge . '/refund' ); |
||
504 | |||
505 | if ( is_wp_error( $result ) ) { |
||
506 | $order->add_order_note( __( 'Unable to refund charge!', 'woocommerce-gateway-stripe' ) . ' ' . $result->get_error_message() ); |
||
507 | } else { |
||
508 | $order->add_order_note( sprintf( __( 'Stripe charge refunded (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); |
||
509 | delete_post_meta( $order_id, '_stripe_charge_captured' ); |
||
510 | delete_post_meta( $order_id, '_stripe_charge_id' ); |
||
511 | } |
||
512 | } |
||
513 | } |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * Gets saved tokens from API if they don't already exist in WooCommerce. |
||
518 | * @param array $tokens |
||
519 | * @return array |
||
520 | */ |
||
521 | public function woocommerce_get_customer_payment_tokens( $tokens, $customer_id, $gateway_id ) { |
||
548 | |||
549 | /** |
||
550 | * Delete token from Stripe |
||
551 | */ |
||
552 | public function woocommerce_payment_token_deleted( $token_id, $token ) { |
||
558 | |||
559 | /** |
||
560 | * Set as default in Stripe |
||
561 | */ |
||
562 | public function woocommerce_payment_token_set_default( $token_id ) { |
||
569 | |||
570 | /** |
||
571 | * Checks Stripe minimum order value authorized per currency |
||
572 | */ |
||
573 | public static function get_minimum_amount() { |
||
610 | |||
611 | /** |
||
612 | * What rolls down stairs |
||
613 | * alone or in pairs, |
||
614 | * and over your neighbor's dog? |
||
615 | * What's great for a snack, |
||
616 | * And fits on your back? |
||
617 | * It's log, log, log |
||
618 | */ |
||
619 | public static function log( $message ) { |
||
626 | } |
||
627 | |||
628 | $GLOBALS['wc_stripe'] = WC_Stripe::get_instance(); |
||
631 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..