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 |
||
17 | class WC_Shipping { |
||
18 | |||
19 | /** @var bool True if shipping is enabled. */ |
||
20 | public $enabled = false; |
||
21 | |||
22 | /** @var array|null Stores methods loaded into woocommerce. */ |
||
23 | public $shipping_methods = null; |
||
24 | |||
25 | /** @var object|null Stores matching shipping zone. */ |
||
26 | protected $shipping_zone = null; |
||
27 | |||
28 | /** @var float Stores the cost of shipping */ |
||
29 | public $shipping_total = 0; |
||
30 | |||
31 | /** @var array Stores an array of shipping taxes. */ |
||
32 | public $shipping_taxes = array(); |
||
33 | |||
34 | /** @var array Stores the shipping classes. */ |
||
35 | public $shipping_classes = array(); |
||
36 | |||
37 | /** @var array Stores packages to ship and to get quotes for. */ |
||
38 | public $packages = array(); |
||
39 | |||
40 | /** |
||
41 | * @var WC_Shipping The single instance of the class |
||
42 | * @since 2.1 |
||
43 | */ |
||
44 | protected static $_instance = null; |
||
45 | |||
46 | /** |
||
47 | * Main WC_Shipping Instance. |
||
48 | * |
||
49 | * Ensures only one instance of WC_Shipping is loaded or can be loaded. |
||
50 | * |
||
51 | * @since 2.1 |
||
52 | * @static |
||
53 | * @return WC_Shipping Main instance |
||
54 | */ |
||
55 | public static function instance() { |
||
61 | |||
62 | /** |
||
63 | * Cloning is forbidden. |
||
64 | * |
||
65 | * @since 2.1 |
||
66 | */ |
||
67 | public function __clone() { |
||
70 | |||
71 | /** |
||
72 | * Unserializing instances of this class is forbidden. |
||
73 | * |
||
74 | * @since 2.1 |
||
75 | */ |
||
76 | public function __wakeup() { |
||
79 | |||
80 | /** |
||
81 | * Initialize shipping. |
||
82 | */ |
||
83 | public function __construct() { |
||
90 | |||
91 | /** |
||
92 | * Initialize shipping. |
||
93 | */ |
||
94 | public function init() { |
||
97 | |||
98 | /** |
||
99 | * Shipping methods register themselves by returning their main class name through the woocommerce_shipping_methods filter. |
||
100 | * @return array |
||
101 | */ |
||
102 | public function get_shipping_method_class_names() { |
||
122 | |||
123 | /** |
||
124 | * Get matching shipping zone. |
||
125 | * @return object|null |
||
126 | */ |
||
127 | public function get_shipping_zone() { |
||
130 | |||
131 | /** |
||
132 | * Loads all shipping methods which are hooked in. If a $package is passed some methods may add themselves conditionally. |
||
133 | * |
||
134 | * Loads all shipping methods which are hooked in. |
||
135 | * If a $package is passed some methods may add themselves conditionally and zones will be used. |
||
136 | * |
||
137 | * @param array $package |
||
138 | * @return array |
||
139 | */ |
||
140 | public function load_shipping_methods( $package = array() ) { |
||
160 | |||
161 | /** |
||
162 | * Register a shipping method. |
||
163 | * |
||
164 | * @param object|string $method Either the name of the method's class, or an instance of the method's class. |
||
165 | */ |
||
166 | public function register_shipping_method( $method ) { |
||
178 | |||
179 | /** |
||
180 | * Unregister shipping methods. |
||
181 | */ |
||
182 | public function unregister_shipping_methods() { |
||
185 | |||
186 | /** |
||
187 | * Returns all registered shipping methods for usage. |
||
188 | * |
||
189 | * @access public |
||
190 | * @return array |
||
191 | */ |
||
192 | public function get_shipping_methods() { |
||
198 | |||
199 | /** |
||
200 | * Get an array of shipping classes. |
||
201 | * |
||
202 | * @access public |
||
203 | * @return array |
||
204 | */ |
||
205 | public function get_shipping_classes() { |
||
212 | |||
213 | /** |
||
214 | * Calculate shipping for (multiple) packages of cart items. |
||
215 | * |
||
216 | * @param array $packages multi-dimensional array of cart items to calc shipping for. |
||
217 | * @return array Array of calculated packages. |
||
218 | */ |
||
219 | public function calculate_shipping( $packages = array() ) { |
||
246 | |||
247 | /** |
||
248 | * See if package is shippable. |
||
249 | * @param array $package |
||
250 | * @return boolean |
||
251 | */ |
||
252 | protected function is_package_shippable( $package ) { |
||
256 | |||
257 | /** |
||
258 | * Calculate shipping rates for a package, |
||
259 | * |
||
260 | * Calculates each shipping methods cost. Rates are stored in the session based on the package hash to avoid re-calculation every page load. |
||
261 | * |
||
262 | * @param array $package cart items |
||
263 | * @param int $package_key Index of the package being calculated. Used to cache multiple package rates. |
||
264 | * @return array |
||
265 | */ |
||
266 | public function calculate_shipping_for_package( $package = array(), $package_key = 0 ) { |
||
308 | |||
309 | /** |
||
310 | * Get packages. |
||
311 | * @return array |
||
312 | */ |
||
313 | public function get_packages() { |
||
316 | |||
317 | /** |
||
318 | * Reset shipping. |
||
319 | * |
||
320 | * Reset the totals for shipping as a whole. |
||
321 | */ |
||
322 | public function reset_shipping() { |
||
328 | } |
||
329 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: