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() { |
||
173 | |||
174 | /** |
||
175 | * Define WC Constants. |
||
176 | */ |
||
177 | private function define_constants() { |
||
191 | |||
192 | /** |
||
193 | * Define constant if not already set. |
||
194 | * |
||
195 | * @param string $name |
||
196 | * @param string|bool $value |
||
197 | */ |
||
198 | private function define( $name, $value ) { |
||
203 | |||
204 | /** |
||
205 | * What type of request is this? |
||
206 | * |
||
207 | * @param string $type admin, ajax, cron or frontend. |
||
208 | * @return bool |
||
209 | */ |
||
210 | private function is_request( $type ) { |
||
222 | |||
223 | /** |
||
224 | * Include required core files used in admin and on the frontend. |
||
225 | */ |
||
226 | public function includes() { |
||
280 | |||
281 | /** |
||
282 | * Include required frontend files. |
||
283 | */ |
||
284 | public function frontend_includes() { |
||
298 | |||
299 | /** |
||
300 | * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes. |
||
301 | */ |
||
302 | public function include_template_functions() { |
||
305 | |||
306 | /** |
||
307 | * Init WooCommerce when WordPress Initialises. |
||
308 | */ |
||
309 | public function init() { |
||
339 | |||
340 | /** |
||
341 | * Load Localisation files. |
||
342 | * |
||
343 | * Note: the first-loaded translation file overrides any following ones if the same translation is present. |
||
344 | * |
||
345 | * Locales found in: |
||
346 | * - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo |
||
347 | * - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo |
||
348 | */ |
||
349 | public function load_plugin_textdomain() { |
||
355 | |||
356 | /** |
||
357 | * Ensure theme and server variable compatibility and setup image sizes. |
||
358 | */ |
||
359 | public function setup_environment() { |
||
368 | |||
369 | /** |
||
370 | * Ensure post thumbnail support is turned on. |
||
371 | */ |
||
372 | private function add_thumbnail_support() { |
||
378 | |||
379 | /** |
||
380 | * Add WC Image sizes to WP. |
||
381 | * |
||
382 | * @since 2.3 |
||
383 | */ |
||
384 | private function add_image_sizes() { |
||
393 | |||
394 | /** |
||
395 | * Get the plugin url. |
||
396 | * @return string |
||
397 | */ |
||
398 | public function plugin_url() { |
||
401 | |||
402 | /** |
||
403 | * Get the plugin path. |
||
404 | * @return string |
||
405 | */ |
||
406 | public function plugin_path() { |
||
409 | |||
410 | /** |
||
411 | * Get the template path. |
||
412 | * @return string |
||
413 | */ |
||
414 | public function template_path() { |
||
417 | |||
418 | /** |
||
419 | * Get Ajax URL. |
||
420 | * @return string |
||
421 | */ |
||
422 | public function ajax_url() { |
||
425 | |||
426 | /** |
||
427 | * Return the WC API URL for a given request. |
||
428 | * |
||
429 | * @param string $request |
||
430 | * @param mixed $ssl (default: null) |
||
431 | * @return string |
||
432 | */ |
||
433 | public function api_request_url( $request, $ssl = null ) { |
||
452 | |||
453 | /** |
||
454 | * Load & enqueue active webhooks. |
||
455 | * |
||
456 | * @since 2.2 |
||
457 | */ |
||
458 | private function load_webhooks() { |
||
473 | |||
474 | /** |
||
475 | * WooCommerce Payment Token Meta API and Term/Order item Meta - set table names. |
||
476 | */ |
||
477 | public function wpdb_table_fix() { |
||
486 | |||
487 | /** |
||
488 | * Get Checkout Class. |
||
489 | * @return WC_Checkout |
||
490 | */ |
||
491 | public function checkout() { |
||
494 | |||
495 | /** |
||
496 | * Get gateways class. |
||
497 | * @return WC_Payment_Gateways |
||
498 | */ |
||
499 | public function payment_gateways() { |
||
502 | |||
503 | /** |
||
504 | * Get shipping class. |
||
505 | * @return WC_Shipping |
||
506 | */ |
||
507 | public function shipping() { |
||
510 | |||
511 | /** |
||
512 | * Email Class. |
||
513 | * @return WC_Emails |
||
514 | */ |
||
515 | public function mailer() { |
||
518 | } |
||
519 | |||
536 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.