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 |
||
| 32 | class WC_Stripe { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Singleton The reference the *Singleton* instance of this class |
||
| 36 | */ |
||
| 37 | private static $instance; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Reference to logging class. |
||
| 41 | */ |
||
| 42 | private static $log; |
||
|
|
|||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the *Singleton* instance of this class. |
||
| 46 | * |
||
| 47 | * @return Singleton The *Singleton* instance. |
||
| 48 | */ |
||
| 49 | public static function get_instance() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Private clone method to prevent cloning of the instance of the |
||
| 58 | * *Singleton* instance. |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | private function __clone() {} |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Private unserialize method to prevent unserializing of the *Singleton* |
||
| 66 | * instance. |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | private function __wakeup() {} |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Notices (array) |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | public $notices = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Protected constructor to prevent creating a new instance of the |
||
| 80 | * *Singleton* via the `new` operator from outside of this class. |
||
| 81 | */ |
||
| 82 | private function __construct() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Init the plugin after plugins_loaded so environment variables are set. |
||
| 91 | * |
||
| 92 | * @since 1.0.0 |
||
| 93 | * @version 4.0.0 |
||
| 94 | */ |
||
| 95 | public function init() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Hides any admin notices. |
||
| 137 | * |
||
| 138 | * @since 4.0.0 |
||
| 139 | * @version 4.0.0 |
||
| 140 | */ |
||
| 141 | public function hide_notices() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Allow this class and other classes to add slug keyed notices (to avoid duplication). |
||
| 166 | * |
||
| 167 | * @since 1.0.0 |
||
| 168 | * @version 4.0.0 |
||
| 169 | */ |
||
| 170 | public function add_admin_notice( $slug, $class, $message, $dismissible = false ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Display any notices we've collected thus far (e.g. for connection, disconnection). |
||
| 180 | * |
||
| 181 | * @since 1.0.0 |
||
| 182 | * @version 4.0.0 |
||
| 183 | */ |
||
| 184 | public function admin_notices() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Checks the environment for compatibility problems. Returns a string with the first incompatibility |
||
| 206 | * found or false if the environment has no problems. |
||
| 207 | * |
||
| 208 | * @since 1.0.0 |
||
| 209 | * @version 4.0.0 |
||
| 210 | */ |
||
| 211 | public function get_environment_warning() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get setting link. |
||
| 239 | * |
||
| 240 | * @since 1.0.0 |
||
| 241 | * |
||
| 242 | * @return string Setting link |
||
| 243 | */ |
||
| 244 | public function get_setting_link() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * The backup sanity check, in case the plugin is activated in a weird way, |
||
| 254 | * or the environment changes after activation. Also handles upgrade routines. |
||
| 255 | * |
||
| 256 | * @since 1.0.0 |
||
| 257 | * @version 4.0.0 |
||
| 258 | */ |
||
| 259 | public function check_environment() { |
||
| 260 | if ( ! defined( 'IFRAME_REQUEST' ) && ( WC_STRIPE_VERSION !== get_option( 'wc_stripe_version' ) ) ) { |
||
| 261 | $this->install(); |
||
| 262 | |||
| 263 | do_action( 'woocommerce_stripe_updated' ); |
||
| 264 | } |
||
| 265 | |||
| 266 | $environment_warning = $this->get_environment_warning(); |
||
| 267 | |||
| 268 | if ( $environment_warning && is_plugin_active( plugin_basename( __FILE__ ) ) ) { |
||
| 269 | $this->add_admin_notice( 'bad_environment', 'error', $environment_warning ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $show_ssl_notice = get_option( 'wc_stripe_show_ssl_notice' ); |
||
| 273 | $show_keys_notice = get_option( 'wc_stripe_show_keys_notice' ); |
||
| 274 | $options = get_option( 'woocommerce_stripe_settings' ); |
||
| 275 | $testmode = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false; |
||
| 276 | $test_pub_key = isset( $options['test_publishable_key'] ) ? $options['test_publishable_key'] : ''; |
||
| 277 | $test_secret_key = isset( $options['test_secret_key'] ) ? $options['test_secret_key'] : ''; |
||
| 278 | $live_pub_key = isset( $options['publishable_key'] ) ? $options['publishable_key'] : ''; |
||
| 279 | $live_secret_key = isset( $options['secret_key'] ) ? $options['secret_key'] : ''; |
||
| 280 | |||
| 281 | if ( isset( $options['enabled'] ) && 'yes' === $options['enabled'] && empty( $show_keys_notice ) ) { |
||
| 282 | $secret = WC_Stripe_API::get_secret_key(); |
||
| 283 | |||
| 284 | if ( empty( $secret ) && ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 'stripe' === $_GET['section'] ) ) { |
||
| 285 | $setting_link = $this->get_setting_link(); |
||
| 286 | /* translators: 1) link */ |
||
| 287 | $this->add_admin_notice( 'keys', '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 ), true ); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Check if keys are entered properly per live/test mode. |
||
| 291 | if ( $testmode ) { |
||
| 292 | View Code Duplication | if ( |
|
| 293 | ! empty( $test_pub_key ) && ! preg_match( '/^pk_test_/', $test_pub_key ) |
||
| 294 | || ! empty( $test_secret_key ) && ! preg_match( '/^sk_test_/', $test_secret_key ) ) |
||
| 295 | { |
||
| 296 | $setting_link = $this->get_setting_link(); |
||
| 297 | /* translators: 1) link */ |
||
| 298 | $this->add_admin_notice( 'keys', 'notice notice-error', sprintf( __( 'Stripe is in test mode however your test keys may not be valid. Test keys start with pk_test and sk_test. Please go to your settings and, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ), true ); |
||
| 299 | } |
||
| 300 | View Code Duplication | } else { |
|
| 301 | if ( |
||
| 302 | ! empty( $live_pub_key ) && ! preg_match( '/^pk_live_/', $live_pub_key ) |
||
| 303 | || ! empty( $live_secret_key ) && ! preg_match( '/^sk_live_/', $live_secret_key ) ) |
||
| 304 | { |
||
| 305 | $setting_link = $this->get_setting_link(); |
||
| 306 | /* translators: 1) link */ |
||
| 307 | $this->add_admin_notice( 'keys', 'notice notice-error', sprintf( __( 'Stripe is in live mode however your test keys may not be valid. Live keys start with pk_live and sk_live. Please go to your settings and, <a href="%s">set your Stripe account keys</a>.', 'woocommerce-gateway-stripe' ), $setting_link ), true ); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | if ( empty( $show_ssl_notice ) && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) { |
||
| 313 | // Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected. |
||
| 314 | if ( ( function_exists( 'wc_site_is_https' ) && ! wc_site_is_https() ) && ( 'no' === get_option( 'woocommerce_force_ssl_checkout' ) && ! class_exists( 'WordPressHTTPS' ) ) ) { |
||
| 315 | /* translators: 1) link 2) link */ |
||
| 316 | $this->add_admin_notice( 'ssl', 'notice notice-warning', sprintf( __( 'Stripe is enabled, but the <a href="%1$s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid <a href="%2$s" target="_blank">SSL certificate</a> - Stripe will only work in test mode.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ), 'https://en.wikipedia.org/wiki/Transport_Layer_Security' ), true ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Updates the plugin version in db |
||
| 323 | * |
||
| 324 | * @since 3.1.0 |
||
| 325 | * @version 4.0.0 |
||
| 326 | */ |
||
| 327 | public function update_plugin_version() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Handles upgrade routines. |
||
| 334 | * |
||
| 335 | * @since 3.1.0 |
||
| 336 | * @version 3.1.0 |
||
| 337 | */ |
||
| 338 | public function install() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Adds plugin action links. |
||
| 348 | * |
||
| 349 | * @since 1.0.0 |
||
| 350 | * @version 4.0.0 |
||
| 351 | */ |
||
| 352 | public function plugin_action_links( $links ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Add the gateways to WooCommerce. |
||
| 363 | * |
||
| 364 | * @since 1.0.0 |
||
| 365 | * @version 4.0.0 |
||
| 366 | */ |
||
| 367 | public function add_gateways( $methods ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Modifies the order of the gateways displayed in admin. |
||
| 389 | * |
||
| 390 | * @since 4.0.0 |
||
| 391 | * @version 4.0.0 |
||
| 392 | */ |
||
| 393 | public function filter_gateway_order_admin( $sections ) { |
||
| 416 | } |
||
| 417 | |||
| 421 |
This check marks private properties in classes that are never used. Those properties can be removed.