Complex classes like WCSATT_STT 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 WCSATT_STT, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WCSATT_STT { |
||
24 | |||
25 | /* Plugin version. */ |
||
26 | const VERSION = '1.0.0'; |
||
27 | |||
28 | /* Required WC version. */ |
||
29 | const REQ_WC_VERSION = '2.3.0'; |
||
30 | |||
31 | /* Required WCSATT version */ |
||
32 | const REQ_WCSATT_VERSION = '1.1.1'; |
||
33 | |||
34 | /* Text domain. */ |
||
35 | const TEXT_DOMAIN = 'wc-satt-stt'; |
||
36 | |||
37 | /** |
||
38 | * @var WCSATT_STT - the single instance of the class. |
||
39 | * |
||
40 | * @since 1.0.0 |
||
41 | */ |
||
42 | protected static $_instance = null; |
||
43 | |||
44 | /** |
||
45 | * Main WCSATT_STT Instance. |
||
46 | * |
||
47 | * Ensures only one instance of WCSATT_STT is loaded or can be loaded. |
||
48 | * |
||
49 | * @static |
||
50 | * @see WCSATT_STT() |
||
51 | * @return WCSATT_STT - Main instance |
||
52 | * @since 1.0.0 |
||
53 | */ |
||
54 | public static function instance() { |
||
60 | |||
61 | /** |
||
62 | * Cloning is forbidden. |
||
63 | * |
||
64 | * @since 1.0.0 |
||
65 | */ |
||
66 | public function __clone() { |
||
69 | |||
70 | /** |
||
71 | * Unserializing instances of this class is forbidden. |
||
72 | * |
||
73 | * @since 1.0.0 |
||
74 | */ |
||
75 | public function __wakeup() { |
||
78 | |||
79 | /** |
||
80 | * Load the plugin. |
||
81 | */ |
||
82 | public function __construct() { |
||
88 | |||
89 | public function plugin_path() { |
||
92 | |||
93 | /* |
||
94 | * Check requirements on activation. |
||
95 | */ |
||
96 | public function load_plugin() { |
||
97 | global $woocommerce; |
||
98 | |||
99 | // Check that the required WooCommerce is running. |
||
100 | if ( version_compare( $woocommerce->version, self::REQ_WC_VERSION, '<' ) ) { |
||
101 | add_action( 'admin_notices', array( $this, 'wcsatt_stt_wc_admin_notice' ) ); |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | // Checks that WooCommerce Subscribe All the Things is running or is less than the required version. |
||
106 | if ( ! class_exists( 'WCS_ATT' ) || version_compare( WCS_ATT::VERSION, self::REQ_WCSATT_VERSION, '<' ) ) { |
||
107 | add_action( 'admin_notices', array( $this, 'wcsatt_stt_admin_notice' ) ); |
||
108 | return false; |
||
109 | } |
||
110 | } // END load_plugin() |
||
111 | |||
112 | /** |
||
113 | * Display a warning message if minimum version of WooCommerce check fails. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function wcsatt_stt_wc_admin_notice() { |
||
120 | |||
121 | /** |
||
122 | * Display a warning message if minimum version of WooCommerce Subscribe to All the Things check fails. |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function wcsatt_stt_admin_notice() { |
||
129 | |||
130 | /** |
||
131 | * Initialize the plugin if ready. |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function init_plugin() { |
||
154 | |||
155 | /** |
||
156 | * Register the product meta data fields. |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function admin_wcsatt_stt_product_meta() { |
||
170 | |||
171 | /** |
||
172 | * Show row meta on the plugin screen. |
||
173 | * |
||
174 | * @param mixed $links Plugin Row Meta |
||
175 | * @param mixed $file Plugin Base file |
||
176 | * @return array |
||
177 | */ |
||
178 | public function plugin_meta_links( $links, $file, $data, $status ) { |
||
186 | |||
187 | /** |
||
188 | * Adds the default values for subscriptions schemes content. |
||
189 | * |
||
190 | * @param array $defaults |
||
191 | * @return void |
||
192 | */ |
||
193 | public static function add_default_subscription_schemes_content( $defaults ) { |
||
202 | |||
203 | /** |
||
204 | * Adds the trial and sign up fields under the subscription section. |
||
205 | * |
||
206 | * @param int $index |
||
207 | * @param array $scheme_data |
||
208 | * @param int $post_id |
||
209 | * @return void |
||
210 | */ |
||
211 | public function wcsatt_stt_fields( $index, $scheme_data, $post_id ) { |
||
263 | |||
264 | /** |
||
265 | * Filters the subscription scheme data to pass the |
||
266 | * sign up and trial options when saving. |
||
267 | * |
||
268 | * @param ini $posted_scheme |
||
269 | * @param string $product_type |
||
270 | * @return void |
||
271 | */ |
||
272 | public function wcsatt_stt_process_scheme_data( $posted_scheme, $product_type ) { |
||
308 | |||
309 | /** |
||
310 | * Adds the additional subscription scheme data for products with attached subscription schemes. |
||
311 | * |
||
312 | * @param object $_product |
||
313 | * @param array $subscription_scheme |
||
314 | * @param WC_Product $product |
||
315 | * @return string |
||
316 | */ |
||
317 | public function add_sub_scheme_data_price_html( $_product, $subscription_scheme, $product ) { |
||
332 | |||
333 | /** |
||
334 | * Adds the extra subscription scheme data to the product object. |
||
335 | * This allows the subscription price to change the initial and |
||
336 | * recurring subscriptions. |
||
337 | * |
||
338 | * @param object $_cloned |
||
339 | * @param array $subscription_scheme |
||
340 | * @return object |
||
341 | */ |
||
342 | public function sub_product_scheme_option( $_cloned, $subscription_scheme ) { |
||
354 | |||
355 | /** |
||
356 | * Filters the price string to include the sign up fee and/or trial |
||
357 | * to pass per subscription scheme option. |
||
358 | * |
||
359 | * @param array $prices |
||
360 | * @param array $subscription_scheme |
||
361 | * @return array |
||
362 | */ |
||
363 | public function get_price_string( $prices, $subscription_scheme ) { |
||
374 | |||
375 | /** |
||
376 | * Filter the suffix price string. |
||
377 | * |
||
378 | * @param object $_product |
||
379 | * @param array $subscription_scheme |
||
380 | * @param WC_Product $product |
||
381 | * @return string |
||
382 | */ |
||
383 | /*public function filter_suffix_price_html( $_product, $subscription_scheme, $product ) { |
||
384 | $subscription_string = ''; |
||
385 | |||
386 | if ( isset( $_product->subscription_trial_length ) && 0 != $_product->subscription_trial_length ) { |
||
387 | $trial_string = wcs_get_subscription_trial_period_strings( $_product->subscription_trial_length, $_product->subscription_trial_period ); |
||
388 | // translators: 1$: subscription string (e.g. "$15 on March 15th every 3 years for 6 years"), 2$: trial length (e.g.: "with 4 months free trial") |
||
389 | $subscription_string = sprintf( __( '%1$s with %2$s free trial', WCSATT_STT::TEXT_DOMAIN ), $subscription_string, $trial_string ); |
||
390 | } |
||
391 | |||
392 | $sign_up_fee = $_product->subscription_sign_up_fee; |
||
393 | |||
394 | if ( is_numeric( $sign_up_fee ) ) { |
||
395 | $sign_up_fee = wc_price( $sign_up_fee ); |
||
396 | } |
||
397 | |||
398 | if ( isset( $_product->subscription_sign_up_fee ) && $_product->subscription_sign_up_fee > 0 ) { |
||
399 | // translators: 1$: subscription string (e.g. "$15 on March 15th every 3 years for 6 years with 2 months free trial"), 2$: signup fee price (e.g. "and a $30 sign-up fee") |
||
400 | $subscription_string = sprintf( __( '%1$s and a %2$s sign-up fee', WCSATT_STT::TEXT_DOMAIN ), $subscription_string, $sign_up_fee ); |
||
401 | } |
||
402 | |||
403 | return $subscription_string; |
||
404 | }*/ |
||
405 | |||
406 | /** |
||
407 | * Converts a cart item if it's a subscription with |
||
408 | * a trial subscription or/and has a sign-up fee. |
||
409 | * |
||
410 | * @param array $cart_item |
||
411 | * @return array |
||
412 | */ |
||
413 | public function add_cart_item( $cart_item ) { |
||
453 | |||
454 | /** |
||
455 | * Get the item signup fee from the subscription scheme. |
||
456 | * |
||
457 | * @param int $product_id |
||
458 | * @param int $scheme_id |
||
459 | * @return int |
||
460 | */ |
||
461 | public function get_item_signup_fee( $product_id, $scheme_id ) { |
||
467 | |||
468 | /** |
||
469 | * Get the item trial length from the subscription scheme. |
||
470 | * |
||
471 | * @param int $product_id |
||
472 | * @param int $scheme_id |
||
473 | * @return int |
||
474 | */ |
||
475 | public function get_item_trial_length( $product_id, $scheme_id ) { |
||
481 | |||
482 | /** |
||
483 | * Get the item trial period from the subscription scheme. |
||
484 | * |
||
485 | * @param int $product_id |
||
486 | * @param int $scheme_id |
||
487 | * @return string |
||
488 | */ |
||
489 | public function get_item_trial_period( $product_id, $scheme_id ) { |
||
495 | |||
496 | } // END class |
||
497 | |||
500 | return WCSATT_STT::instance(); |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.