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|null 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 | * Initialize shipping. |
||
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 | * Loads all shipping methods which are hooked in. If a $package is passed some methods may add themselves conditionally. |
||
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() ) { |
||
134 | if ( ! empty( $package ) ) { |
||
135 | $status_options = get_option( 'woocommerce_status_options', array() ); |
||
136 | $shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package ); |
||
|
|||
137 | $this->shipping_methods = $shipping_zone->get_shipping_methods( true ); |
||
138 | |||
139 | // Debug output |
||
140 | if ( ! empty( $status_options['shipping_debug_mode'] ) && ! defined( 'WOOCOMMERCE_CHECKOUT' ) && ! wc_has_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' ) ) { |
||
141 | wc_add_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' ); |
||
142 | } |
||
143 | } else { |
||
144 | $this->shipping_methods = array(); |
||
145 | } |
||
146 | |||
147 | // For the settings in the backend, and for non-shipping zone methods, we still need to load any registered classes here. |
||
148 | foreach ( $this->get_shipping_method_class_names() as $method_id => $method_class ) { |
||
149 | $this->register_shipping_method( $method_class ); |
||
150 | } |
||
151 | |||
152 | // Methods can register themselves manually through this hook if necessary. |
||
153 | do_action( 'woocommerce_load_shipping_methods', $package ); |
||
154 | |||
155 | // Return loaded methods |
||
156 | return $this->get_shipping_methods(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Register a shipping method. |
||
161 | * |
||
162 | * @param object|string $method Either the name of the method's class, or an instance of the method's class. |
||
163 | */ |
||
164 | public function register_shipping_method( $method ) { |
||
165 | if ( ! is_object( $method ) ) { |
||
166 | if ( ! class_exists( $method ) ) { |
||
167 | return false; |
||
168 | } |
||
169 | $method = new $method(); |
||
170 | } |
||
171 | if ( is_null( $this->shipping_methods ) ) { |
||
172 | $this->shipping_methods = array(); |
||
173 | } |
||
174 | $this->shipping_methods[ $method->id ] = $method; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Unregister shipping methods. |
||
179 | */ |
||
180 | public function unregister_shipping_methods() { |
||
183 | |||
184 | /** |
||
185 | * Returns all registered shipping methods for usage. |
||
186 | * |
||
187 | * @access public |
||
188 | * @return array |
||
189 | */ |
||
190 | public function get_shipping_methods() { |
||
196 | |||
197 | /** |
||
198 | * Get an array of shipping classes. |
||
199 | * |
||
200 | * @access public |
||
201 | * @return array |
||
202 | */ |
||
203 | public function get_shipping_classes() { |
||
210 | |||
211 | /** |
||
212 | * Get the default method. |
||
213 | * @param array $available_methods |
||
214 | * @param boolean $current_chosen_method |
||
215 | * @return string |
||
216 | */ |
||
217 | private function get_default_method( $available_methods, $current_chosen_method = false ) { |
||
234 | |||
235 | /** |
||
236 | * Calculate shipping for (multiple) packages of cart items. |
||
237 | * |
||
238 | * @param array $packages multi-dimensional array of cart items to calc shipping for |
||
239 | */ |
||
240 | public function calculate_shipping( $packages = array() ) { |
||
318 | |||
319 | /** |
||
320 | * See if package is shippable. |
||
321 | * @param array $package |
||
322 | * @return boolean |
||
323 | */ |
||
324 | protected function is_package_shippable( $package ) { |
||
328 | |||
329 | /** |
||
330 | * Calculate shipping rates for a package, |
||
331 | * |
||
332 | * Calculates each shipping methods cost. Rates are stored in the session based on the package hash to avoid re-calculation every page load. |
||
333 | * |
||
334 | * @param array $package cart items |
||
335 | * @param int $package_key Index of the package being calculated. Used to cache multiple package rates. |
||
336 | * @return array |
||
337 | */ |
||
338 | public function calculate_shipping_for_package( $package = array(), $package_key = 0 ) { |
||
381 | |||
382 | /** |
||
383 | * Get packages. |
||
384 | * @return array |
||
385 | */ |
||
386 | public function get_packages() { |
||
389 | |||
390 | /** |
||
391 | * Reset shipping. |
||
392 | * |
||
393 | * Reset the totals for shipping as a whole. |
||
394 | */ |
||
395 | public function reset_shipping() { |
||
401 | |||
402 | /** |
||
403 | * @deprecated 2.6.0 Was previously used to determine sort order of methods, but this is now controlled by zones and thus unused. |
||
404 | */ |
||
405 | public function sort_shipping_methods() { |
||
409 | } |
||
410 |
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: