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() { |
||
| 160 | if ( ! defined( 'IFRAME_REQUEST' ) && ( WC_STRIPE_VERSION !== get_option( 'woocommerce_stripe_version' ) ) ) { |
||
| 161 | $this->install(); |
||
| 162 | |||
| 163 | do_action( 'woocommerce_stripe_updated' ); |
||
| 164 | } |
||
| 165 | |||
| 166 | $environment_warning = self::get_environment_warning(); |
||
| 167 | |||
| 168 | if ( $environment_warning && is_plugin_active( plugin_basename( __FILE__ ) ) ) { |
||
| 169 | $this->add_admin_notice( 'bad_environment', 'error', $environment_warning ); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Check if secret key present. Otherwise prompt, via notice, to go to |
||
| 173 | // setting. |
||
| 174 | if ( ! class_exists( 'WC_Stripe_API' ) ) { |
||
| 175 | include_once( dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php' ); |
||
| 176 | } |
||
| 177 | |||
| 178 | $secret = WC_Stripe_API::get_secret_key(); |
||
| 179 | |||
| 180 | if ( empty( $secret ) && ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'] ) ) { |
||
| 181 | $setting_link = $this->get_setting_link(); |
||
| 182 | $this->add_admin_notice( 'prompt_connect', 'notice notice-warning', sprintf( __( 'Stripe is almost ready. To get started, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ) ); |
||
| 183 | } |
||
| 184 | } |
||
| 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() { |
||
| 194 | delete_option( 'wc_stripe_version' ); |
||
| 195 | add_option( 'wc_stripe_version', WC_STRIPE_VERSION ); |
||
| 196 | |||
| 197 | return true; |
||
| 198 | } |
||
| 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() { |
||
| 207 | update_option( 'wc_stripe_show_request_api_notice', 'no' ); |
||
| 208 | } |
||
| 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() { |
||
| 217 | update_option( 'wc_stripe_show_apple_pay_notice', 'no' ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Handles upgrade routines. |
||
| 222 | * |
||
| 223 | * @since 3.1.0 |
||
| 224 | * @version 3.1.0 |
||
| 225 | */ |
||
| 226 | public function install() { |
||
| 227 | if ( ! defined( 'WC_STRIPE_INSTALLING' ) ) { |
||
| 228 | define( 'WC_STRIPE_INSTALLING', true ); |
||
| 229 | } |
||
| 230 | |||
| 231 | // @TODO remove this in the future. |
||
| 232 | update_option( 'wc_stripe_show_features_notice', 'yes' ); |
||
| 233 | |||
| 234 | $this->_update_plugin_version(); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Checks the environment for compatibility problems. Returns a string with the first incompatibility |
||
| 239 | * found or false if the environment has no problems. |
||
| 240 | */ |
||
| 241 | static function get_environment_warning() { |
||
| 242 | View Code Duplication | if ( version_compare( phpversion(), WC_STRIPE_MIN_PHP_VER, '<' ) ) { |
|
| 243 | $message = __( 'WooCommerce Stripe - The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe' ); |
||
| 244 | |||
| 245 | return sprintf( $message, WC_STRIPE_MIN_PHP_VER, phpversion() ); |
||
| 246 | } |
||
| 247 | |||
| 248 | if ( ! defined( 'WC_VERSION' ) ) { |
||
| 249 | return __( 'WooCommerce Stripe requires WooCommerce to be activated to work.', 'woocommerce-gateway-stripe' ); |
||
| 250 | } |
||
| 251 | |||
| 252 | View Code Duplication | if ( version_compare( WC_VERSION, WC_STRIPE_MIN_WC_VER, '<' ) ) { |
|
| 253 | $message = __( 'WooCommerce Stripe - The minimum WooCommerce version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-stripe' ); |
||
| 254 | |||
| 255 | return sprintf( $message, WC_STRIPE_MIN_WC_VER, WC_VERSION ); |
||
| 256 | } |
||
| 257 | |||
| 258 | if ( ! function_exists( 'curl_init' ) ) { |
||
| 259 | return __( 'WooCommerce Stripe - cURL is not installed.', 'woocommerce-gateway-stripe' ); |
||
| 260 | } |
||
| 261 | |||
| 262 | return false; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Adds plugin action links |
||
| 267 | * |
||
| 268 | * @since 1.0.0 |
||
| 269 | */ |
||
| 270 | public function plugin_action_links( $links ) { |
||
| 271 | $setting_link = $this->get_setting_link(); |
||
| 272 | |||
| 273 | $plugin_links = array( |
||
| 274 | '<a href="' . $setting_link . '">' . __( 'Settings', 'woocommerce-gateway-stripe' ) . '</a>', |
||
| 275 | '<a href="https://docs.woothemes.com/document/stripe/">' . __( 'Docs', 'woocommerce-gateway-stripe' ) . '</a>', |
||
| 276 | '<a href="http://support.woothemes.com/">' . __( 'Support', 'woocommerce-gateway-stripe' ) . '</a>', |
||
| 277 | ); |
||
| 278 | return array_merge( $plugin_links, $links ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get setting link. |
||
| 283 | * |
||
| 284 | * @since 1.0.0 |
||
| 285 | * |
||
| 286 | * @return string Setting link |
||
| 287 | */ |
||
| 288 | public function get_setting_link() { |
||
| 289 | $use_id_as_section = function_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false; |
||
| 290 | |||
| 291 | $section_slug = $use_id_as_section ? 'stripe' : strtolower( 'WC_Gateway_Stripe' ); |
||
| 292 | |||
| 293 | return admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug ); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Display any notices we've collected thus far (e.g. for connection, disconnection) |
||
| 298 | */ |
||
| 299 | public function admin_notices() { |
||
| 300 | $show_request_api_notice = get_option( 'wc_stripe_show_request_api_notice' ); |
||
| 301 | $show_apple_pay_notice = get_option( 'wc_stripe_show_apple_pay_notice' ); |
||
| 302 | |||
| 303 | View Code Duplication | if ( empty( $show_apple_pay_notice ) || 'yes' === $show_apple_pay_notice ) { |
|
| 304 | // @TODO remove this notice in the future. |
||
| 305 | ?> |
||
| 306 | <div class="notice notice-warning wc-stripe-apple-pay-notice is-dismissible"><p><?php esc_html_e( 'New Feature! Stripe now supports Apple Pay. Your customers can now purchase your products even faster. Apple Pay has been enabled by default.', 'woocommerce-gateway-stripe' ); ?></p></div> |
||
| 307 | |||
| 308 | <script type="application/javascript"> |
||
| 309 | window.onload = function() { |
||
| 310 | jQuery( '.wc-stripe-apple-pay-notice' ).on( 'click', '.notice-dismiss', function () { |
||
| 311 | var data = { |
||
| 312 | action: 'stripe_dismiss_apple_pay_notice' |
||
| 313 | }; |
||
| 314 | |||
| 315 | jQuery.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', data ); |
||
| 316 | }); |
||
| 317 | } |
||
| 318 | </script> |
||
| 319 | |||
| 320 | <?php |
||
| 321 | } |
||
| 322 | |||
| 323 | View Code Duplication | if ( empty( $show_request_api_notice ) || 'yes' === $show_request_api_notice ) { |
|
| 324 | // @TODO remove this notice in the future. |
||
| 325 | ?> |
||
| 326 | <div class="notice notice-warning wc-stripe-request-api-notice is-dismissible"><p><?php esc_html_e( 'New Feature! Stripe now supports Google Payment Request. Your customers can now use mobile phones with supported browsers such as Chrome to make purchases easier and faster.', 'woocommerce-gateway-stripe' ); ?></p></div> |
||
| 327 | |||
| 328 | <script type="application/javascript"> |
||
| 329 | window.onload = function() { |
||
| 330 | jQuery( '.wc-stripe-request-api-notice' ).on( 'click', '.notice-dismiss', function () { |
||
| 331 | var data = { |
||
| 332 | action: 'stripe_dismiss_request_api_notice' |
||
| 333 | }; |
||
| 334 | |||
| 335 | jQuery.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', data ); |
||
| 336 | }); |
||
| 337 | } |
||
| 338 | </script> |
||
| 339 | |||
| 340 | <?php |
||
| 341 | } |
||
| 342 | |||
| 343 | foreach ( (array) $this->notices as $notice_key => $notice ) { |
||
| 344 | echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>"; |
||
| 345 | echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); |
||
| 346 | echo '</p></div>'; |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Initialize the gateway. Called very early - in the context of the plugins_loaded action |
||
| 352 | * |
||
| 353 | * @since 1.0.0 |
||
| 354 | */ |
||
| 355 | public function init_gateways() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Add the gateways to WooCommerce |
||
| 392 | * |
||
| 393 | * @since 1.0.0 |
||
| 394 | */ |
||
| 395 | public function add_gateways( $methods ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Capture payment when the order is changed from on-hold to complete or processing |
||
| 406 | * |
||
| 407 | * @param int $order_id |
||
| 408 | */ |
||
| 409 | public function capture_payment( $order_id ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Cancel pre-auth on refund/cancellation |
||
| 446 | * |
||
| 447 | * @param int $order_id |
||
| 448 | */ |
||
| 449 | public function cancel_payment( $order_id ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Gets saved tokens from API if they don't already exist in WooCommerce. |
||
| 473 | * @param array $tokens |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function woocommerce_get_customer_payment_tokens( $tokens, $customer_id, $gateway_id ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Delete token from Stripe |
||
| 506 | */ |
||
| 507 | public function woocommerce_payment_token_deleted( $token_id, $token ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Set as default in Stripe |
||
| 516 | */ |
||
| 517 | public function woocommerce_payment_token_set_default( $token_id ) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Checks Stripe minimum order value authorized per currency |
||
| 527 | */ |
||
| 528 | public static function get_minimum_amount() { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * What rolls down stairs |
||
| 568 | * alone or in pairs, |
||
| 569 | * and over your neighbor's dog? |
||
| 570 | * What's great for a snack, |
||
| 571 | * And fits on your back? |
||
| 572 | * It's log, log, log |
||
| 573 | */ |
||
| 574 | public static function log( $message ) { |
||
| 581 | } |
||
| 582 | |||
| 583 | $GLOBALS['wc_stripe'] = WC_Stripe::get_instance(); |
||
| 584 | |||
| 585 | endif; |
||
| 586 |
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..