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_Shipping 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_Shipping, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class WC_Shipping { |
||
22 | |||
23 | /** @var bool True if shipping is enabled. */ |
||
24 | public $enabled = false; |
||
25 | |||
26 | /** @var array Stores methods loaded into woocommerce. */ |
||
27 | public $shipping_methods = null; |
||
28 | |||
29 | /** @var float Stores the cost of shipping */ |
||
30 | public $shipping_total = 0; |
||
31 | |||
32 | /** @var array Stores an array of shipping taxes. */ |
||
33 | public $shipping_taxes = array(); |
||
34 | |||
35 | /** @var array Stores the shipping classes. */ |
||
36 | public $shipping_classes = array(); |
||
37 | |||
38 | /** @var array Stores packages to ship and to get quotes for. */ |
||
39 | public $packages = array(); |
||
40 | |||
41 | /** |
||
42 | * @var WC_Shipping The single instance of the class |
||
43 | * @since 2.1 |
||
44 | */ |
||
45 | protected static $_instance = null; |
||
46 | |||
47 | /** |
||
48 | * Main WC_Shipping Instance. |
||
49 | * |
||
50 | * Ensures only one instance of WC_Shipping is loaded or can be loaded. |
||
51 | * |
||
52 | * @since 2.1 |
||
53 | * @static |
||
54 | * @return WC_Shipping Main instance |
||
55 | */ |
||
56 | public static function instance() { |
||
62 | |||
63 | /** |
||
64 | * Cloning is forbidden. |
||
65 | * |
||
66 | * @since 2.1 |
||
67 | */ |
||
68 | public function __clone() { |
||
71 | |||
72 | /** |
||
73 | * Unserializing instances of this class is forbidden. |
||
74 | * |
||
75 | * @since 2.1 |
||
76 | */ |
||
77 | public function __wakeup() { |
||
80 | |||
81 | /** |
||
82 | * Initialize shipping. |
||
83 | */ |
||
84 | public function __construct() { |
||
91 | |||
92 | /** |
||
93 | * init function. |
||
94 | */ |
||
95 | public function init() { |
||
98 | |||
99 | /** |
||
100 | * Shipping methods register themselves by returning their main class name through the woocommerce_shipping_methods filter. |
||
101 | * @return array |
||
102 | */ |
||
103 | public function get_shipping_method_class_names() { |
||
123 | |||
124 | /** |
||
125 | * Load shipping methods. |
||
126 | * |
||
127 | * Loads all shipping methods which are hooked in. |
||
128 | * If a $package is passed some methods may add themselves conditionally and zones will be used. |
||
129 | * |
||
130 | * @param array $package |
||
131 | * @return array |
||
132 | */ |
||
133 | public function load_shipping_methods( $package = array() ) { |
||
152 | |||
153 | /** |
||
154 | * Register a shipping method. |
||
155 | * |
||
156 | * @param object|string $method Either the name of the method's class, or an instance of the method's class. |
||
157 | */ |
||
158 | public function register_shipping_method( $method ) { |
||
167 | |||
168 | /** |
||
169 | * Unregister shipping methods. |
||
170 | */ |
||
171 | public function unregister_shipping_methods() { |
||
174 | |||
175 | /** |
||
176 | * Returns all registered shipping methods. |
||
177 | * |
||
178 | * @access public |
||
179 | * @return array |
||
180 | */ |
||
181 | public function get_shipping_methods() { |
||
187 | |||
188 | /** |
||
189 | * Get an array of shipping classes. |
||
190 | * |
||
191 | * @access public |
||
192 | * @return array |
||
193 | */ |
||
194 | public function get_shipping_classes() { |
||
201 | |||
202 | /** |
||
203 | * Get the default method. |
||
204 | * @param array $available_methods |
||
205 | * @param boolean $current_chosen_method |
||
206 | * @return string |
||
207 | */ |
||
208 | private function get_default_method( $available_methods, $current_chosen_method = false ) { |
||
225 | |||
226 | /** |
||
227 | * Calculate shipping for (multiple) packages of cart items. |
||
228 | * |
||
229 | * @param array $packages multi-dimensional array of cart items to calc shipping for |
||
230 | */ |
||
231 | public function calculate_shipping( $packages = array() ) { |
||
292 | |||
293 | /** |
||
294 | * Calculate shipping rates for a package, |
||
295 | * |
||
296 | * Calculates each shipping methods cost. Rates are stored in the session based on the package hash to avoid re-calculation every page load. |
||
297 | * |
||
298 | * @param array $package cart items |
||
299 | * @return array |
||
300 | */ |
||
301 | public function calculate_shipping_for_package( $package = array() ) { |
||
336 | |||
337 | /** |
||
338 | * Get packages. |
||
339 | * @return array |
||
340 | */ |
||
341 | public function get_packages() { |
||
344 | |||
345 | /** |
||
346 | * Reset shipping. |
||
347 | * |
||
348 | * Reset the totals for shipping as a whole. |
||
349 | */ |
||
350 | public function reset_shipping() { |
||
356 | |||
357 | /** |
||
358 | * @deprecated 2.6.0 Was previously used to determine sort order of methods, but this is now controlled by zones and thus unused. |
||
359 | */ |
||
360 | public function sort_shipping_methods() { |
||
364 | } |
||
365 |
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.