Complex classes like WooCommerce 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 WooCommerce, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | final class WooCommerce { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * WooCommerce version. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | public $version = '2.6.0'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The single instance of the class. |
||
| 42 | * |
||
| 43 | * @var WooCommerce |
||
| 44 | * @since 2.1 |
||
| 45 | */ |
||
| 46 | protected static $_instance = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Session instance. |
||
| 50 | * |
||
| 51 | * @var WC_Session |
||
| 52 | */ |
||
| 53 | public $session = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Query instance. |
||
| 57 | * |
||
| 58 | * @var WC_Query |
||
| 59 | */ |
||
| 60 | public $query = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Product factory instance. |
||
| 64 | * |
||
| 65 | * @var WC_Product_Factory |
||
| 66 | */ |
||
| 67 | public $product_factory = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Countries instance. |
||
| 71 | * |
||
| 72 | * @var WC_Countries |
||
| 73 | */ |
||
| 74 | public $countries = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Integrations instance. |
||
| 78 | * |
||
| 79 | * @var WC_Integrations |
||
| 80 | */ |
||
| 81 | public $integrations = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Cart instance. |
||
| 85 | * |
||
| 86 | * @var WC_Cart |
||
| 87 | */ |
||
| 88 | public $cart = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Customer instance. |
||
| 92 | * |
||
| 93 | * @var WC_Customer |
||
| 94 | */ |
||
| 95 | public $customer = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Order factory instance. |
||
| 99 | * |
||
| 100 | * @var WC_Order_Factory |
||
| 101 | */ |
||
| 102 | public $order_factory = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Main WooCommerce Instance. |
||
| 106 | * |
||
| 107 | * Ensures only one instance of WooCommerce is loaded or can be loaded. |
||
| 108 | * |
||
| 109 | * @since 2.1 |
||
| 110 | * @static |
||
| 111 | * @see WC() |
||
| 112 | * @return WooCommerce - Main instance. |
||
| 113 | */ |
||
| 114 | public static function instance() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Cloning is forbidden. |
||
| 123 | * @since 2.1 |
||
| 124 | */ |
||
| 125 | public function __clone() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Unserializing instances of this class is forbidden. |
||
| 131 | * @since 2.1 |
||
| 132 | */ |
||
| 133 | public function __wakeup() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Auto-load in-accessible properties on demand. |
||
| 139 | * @param mixed $key |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | public function __get( $key ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * WooCommerce Constructor. |
||
| 150 | */ |
||
| 151 | public function __construct() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Hook into actions and filters. |
||
| 161 | * @since 2.3 |
||
| 162 | */ |
||
| 163 | private function init_hooks() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Define WC Constants. |
||
| 175 | */ |
||
| 176 | private function define_constants() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Define constant if not already set. |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | * @param string|bool $value |
||
| 196 | */ |
||
| 197 | private function define( $name, $value ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * What type of request is this? |
||
| 205 | * |
||
| 206 | * @param string $type admin, ajax, cron or frontend. |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | private function is_request( $type ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Include required core files used in admin and on the frontend. |
||
| 224 | */ |
||
| 225 | public function includes() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Include required frontend files. |
||
| 282 | */ |
||
| 283 | public function frontend_includes() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes. |
||
| 300 | */ |
||
| 301 | public function include_template_functions() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Init WooCommerce when WordPress Initialises. |
||
| 307 | */ |
||
| 308 | public function init() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Load Localisation files. |
||
| 341 | * |
||
| 342 | * Note: the first-loaded translation file overrides any following ones if the same translation is present. |
||
| 343 | * |
||
| 344 | * Locales found in: |
||
| 345 | * - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo |
||
| 346 | * - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo |
||
| 347 | */ |
||
| 348 | public function load_plugin_textdomain() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Ensure theme and server variable compatibility and setup image sizes. |
||
| 357 | */ |
||
| 358 | public function setup_environment() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Ensure post thumbnail support is turned on. |
||
| 370 | */ |
||
| 371 | private function add_thumbnail_support() { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Add WC Image sizes to WP. |
||
| 380 | * |
||
| 381 | * @since 2.3 |
||
| 382 | */ |
||
| 383 | private function add_image_sizes() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get the plugin url. |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function plugin_url() { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the plugin path. |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function plugin_path() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the template path. |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function template_path() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get Ajax URL. |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function ajax_url() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Return the WC API URL for a given request. |
||
| 427 | * |
||
| 428 | * @param string $request |
||
| 429 | * @param mixed $ssl (default: null) |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function api_request_url( $request, $ssl = null ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Load & enqueue active webhooks. |
||
| 454 | * |
||
| 455 | * @since 2.2 |
||
| 456 | */ |
||
| 457 | private function load_webhooks() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * WooCommerce Payment Token Meta API - set table name |
||
| 475 | */ |
||
| 476 | function payment_token_metadata_wpdbfix() { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get Checkout Class. |
||
| 484 | * @return WC_Checkout |
||
| 485 | */ |
||
| 486 | public function checkout() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get gateways class. |
||
| 492 | * @return WC_Payment_Gateways |
||
| 493 | */ |
||
| 494 | public function payment_gateways() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get shipping class. |
||
| 500 | * @return WC_Shipping |
||
| 501 | */ |
||
| 502 | public function shipping() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Email Class. |
||
| 508 | * @return WC_Emails |
||
| 509 | */ |
||
| 510 | public function mailer() { |
||
| 513 | } |
||
| 514 | |||
| 531 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.