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_Method 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_Method, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class WC_Shipping_Method extends WC_Settings_API { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Features this method supports. Possible features used by core: |
||
| 22 | * - shipping-zones Shipping zone functionality + instances |
||
| 23 | * - instance-settings Instance settings screens. |
||
| 24 | * - settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed. |
||
| 25 | * - instance-settings-modal Allows the instance settings to be loaded within a modal in the zones UI. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $supports = array( 'settings' ); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Unique ID for the shipping method - must be set. |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $id = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Method title. |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | public $method_title = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Method description. |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $method_description = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * yes or no based on whether the method is enabled. |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | public $enabled = 'yes'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Shipping method title for the frontend. |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $title; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * This is an array of rates - methods must populate this array to register shipping costs. |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | public $rates = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * If 'taxable' tax will be charged for this method (if applicable). |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $tax_status = 'taxable'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Fee for the method (if applicable). |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $fee = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Minimum fee for the method (if applicable). |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $minimum_fee = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Instance ID if used. |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | public $instance_id = 0; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Instance form fields. |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public $instance_form_fields = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Instance settings. |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | public $instance_settings = array(); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Availability - legacy. Used for method Availability. |
||
| 104 | * No longer useful for instance based shipping methods. |
||
| 105 | * @deprecated 2.6.0 |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | public $availability; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Availability countries - legacy. Used for method Availability. |
||
| 112 | * No longer useful for instance based shipping methods. |
||
| 113 | * @deprecated 2.6.0 |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | public $countries = array(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Constructor. |
||
| 120 | * @param int $instance_id |
||
| 121 | */ |
||
| 122 | public function __construct( $instance_id = 0 ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Check if a shipping method supports a given feature. |
||
| 128 | * |
||
| 129 | * Methods should override this to declare support (or lack of support) for a feature. |
||
| 130 | * |
||
| 131 | * @param $feature string The name of a feature to test support for. |
||
| 132 | * @return bool True if the shipping method supports the feature, false otherwise. |
||
| 133 | */ |
||
| 134 | public function supports( $feature ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method. |
||
| 140 | */ |
||
| 141 | public function calculate_shipping( $package = array() ) {} |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Whether or not we need to calculate tax on top of the shipping rate. |
||
| 145 | * @return boolean |
||
| 146 | */ |
||
| 147 | public function is_taxable() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Whether or not this method is enabled in settings. |
||
| 153 | * @since 2.6.0 |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | public function is_enabled() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Return the shipping method instance ID. |
||
| 162 | * @since 2.6.0 |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | public function get_instance_id() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return the shipping method title. |
||
| 171 | * @since 2.6.0 |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function get_method_title() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return the shipping method description. |
||
| 180 | * @since 2.6.0 |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function get_method_description() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return the shipping title which is user set. |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function get_title() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return calculated rates for a package. |
||
| 198 | * @since 2.6.0 |
||
| 199 | * @param object $package |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function get_rates_for_package( $package ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns a rate ID based on this methods ID and instance, with an optional |
||
| 212 | * suffix if distinguishing between multiple rates. |
||
| 213 | * @since 2.6.0 |
||
| 214 | * @param string $suffix |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function get_rate_id( $suffix = '' ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Add a shipping rate. If taxes are not set they will be calculated based on cost. |
||
| 233 | * @param array $args (default: array()) |
||
| 234 | */ |
||
| 235 | public function add_rate( $args = array() ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Calc taxes per item being shipping in costs array. |
||
| 287 | * @since 2.6.0 |
||
| 288 | * @access protected |
||
| 289 | * @param array $costs |
||
| 290 | * @return array of taxes |
||
| 291 | */ |
||
| 292 | protected function get_taxes_per_item( $costs ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Is this method available? |
||
| 329 | * @param array $package |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function is_available( $package ) { |
||
| 333 | $available = $this->is_enabled(); |
||
| 334 | |||
| 335 | // Country availability (legacy, for non-zone based methods) |
||
| 336 | if ( ! $this->instance_id && $available ) { |
||
| 337 | $countries = is_array( $this->countries ) ? $this->countries : array(); |
||
| 338 | |||
| 339 | switch ( $this->availability ) { |
||
| 340 | case 'specific' : |
||
| 341 | View Code Duplication | case 'including' : |
|
| 342 | $available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) ); |
||
| 343 | break; |
||
| 344 | View Code Duplication | case 'excluding' : |
|
| 345 | $available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) ); |
||
| 346 | break; |
||
| 347 | View Code Duplication | default : |
|
| 348 | $available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) ); |
||
| 349 | break; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package ); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get fee to add to shipping cost. |
||
| 358 | * @param string|float $fee |
||
| 359 | * @param float $total |
||
| 360 | * @return float |
||
| 361 | */ |
||
| 362 | public function get_fee( $fee, $total ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Does this method have a settings page? |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function has_settings() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Return admin options as a html string. |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function get_admin_options_html() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Output the shipping settings screen. |
||
| 396 | */ |
||
| 397 | public function admin_options() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * get_option function. |
||
| 407 | * |
||
| 408 | * Gets and option from the settings API, using defaults if necessary to prevent undefined notices. |
||
| 409 | * |
||
| 410 | * @param string $key |
||
| 411 | * @param mixed $empty_value |
||
| 412 | * @return mixed The value specified for the option or a default value for the option. |
||
| 413 | */ |
||
| 414 | public function get_option( $key, $empty_value = null ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets an option from the settings API, using defaults if necessary to prevent undefined notices. |
||
| 426 | * |
||
| 427 | * @param string $key |
||
| 428 | * @param mixed $empty_value |
||
| 429 | * @return mixed The value specified for the option or a default value for the option. |
||
| 430 | */ |
||
| 431 | View Code Duplication | public function get_instance_option( $key, $empty_value = null ) { |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Get settings fields for instances of this shipping method (within zones). |
||
| 451 | * Should be overridden by shipping methods to add options. |
||
| 452 | * @since 2.6.0 |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function get_instance_form_fields() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Return the name of the option in the WP DB. |
||
| 461 | * @since 2.6.0 |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public function get_instance_option_key() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Initialise Settings for instances. |
||
| 470 | * @since 2.6.0 |
||
| 471 | */ |
||
| 472 | View Code Duplication | public function init_instance_settings() { |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Processes and saves options. |
||
| 484 | * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out. |
||
| 485 | * @since 2.6.0 |
||
| 486 | * @return bool was anything saved? |
||
| 487 | */ |
||
| 488 | public function process_admin_options() { |
||
| 509 | } |
||
| 510 |
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: