Complex classes like WC_Shipping_Zone 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_Zone, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class WC_Shipping_Zone extends WC_Data { |
||
| 17 | |||
| 18 | protected $id = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Zone Data |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $data = array( |
||
| 25 | 'zone_name' => '', |
||
| 26 | 'zone_order' => 0, |
||
| 27 | 'zone_locations' => array(), |
||
| 28 | ); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * True when location data needs to be re-saved |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $locations_changed = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor for zones |
||
| 38 | * @param int|object $zone Zone ID to load from the DB (optional) or already queried data. |
||
| 39 | */ |
||
| 40 | public function __construct( $zone = null ) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns all data for this object. |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function get_data() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Insert zone into the database |
||
| 67 | */ |
||
| 68 | public function create() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Read zone. |
||
| 79 | * @param int ID to read from DB |
||
| 80 | */ |
||
| 81 | public function read( $id ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Update zone in the database |
||
| 94 | */ |
||
| 95 | public function update() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Delete a zone. |
||
| 108 | * @since 2.6.0 |
||
| 109 | */ |
||
| 110 | public function delete() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Save zone data to the database. |
||
| 123 | */ |
||
| 124 | public function save() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set ID. |
||
| 146 | * @param int|null $id |
||
| 147 | */ |
||
| 148 | public function set_id( $id ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get ID |
||
| 154 | * @return int|null Null if the zone does not exist. 0 is the default zone. |
||
| 155 | */ |
||
| 156 | public function get_id() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get zone ID |
||
| 162 | * @return int|null Null if the zone does not exist. 0 is the default zone. |
||
| 163 | */ |
||
| 164 | public function get_zone_id() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get zone name |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function get_zone_name() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get zone order |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function get_zone_order() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get zone locations |
||
| 186 | * @return array of zone objects |
||
| 187 | */ |
||
| 188 | public function get_zone_locations() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Generate a zone name based on location. |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | protected function generate_zone_name() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return a text string representing what this zone is for. |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function get_formatted_location( $max = 10 ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get shipping methods linked to this zone |
||
| 255 | * @param bool Only return enabled methods. |
||
| 256 | * @return array of objects |
||
| 257 | */ |
||
| 258 | public function get_shipping_methods( $enabled_only = false ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Location type detection |
||
| 305 | * @param object $location |
||
| 306 | * @return boolean |
||
| 307 | */ |
||
| 308 | private function location_is_continent( $location ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Location type detection |
||
| 314 | * @param object $location |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | private function location_is_country( $location ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Location type detection |
||
| 323 | * @param object $location |
||
| 324 | * @return boolean |
||
| 325 | */ |
||
| 326 | private function location_is_state( $location ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Location type detection |
||
| 332 | * @param object $location |
||
| 333 | * @return boolean |
||
| 334 | */ |
||
| 335 | private function location_is_postcode( $location ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set zone name |
||
| 341 | * @param string $set |
||
| 342 | */ |
||
| 343 | public function set_zone_name( $set ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Set zone order |
||
| 349 | * @param int $set |
||
| 350 | */ |
||
| 351 | public function set_zone_order( $set ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Is passed location type valid? |
||
| 357 | * @param string $type |
||
| 358 | * @return boolean |
||
| 359 | */ |
||
| 360 | public function is_valid_location_type( $type ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Add location (state or postcode) to a zone. |
||
| 366 | * @param string $code |
||
| 367 | * @param string $type state or postcode |
||
| 368 | */ |
||
| 369 | public function add_location( $code, $type ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Clear all locations for this zone. |
||
| 385 | * @param array|string $types of location to clear |
||
| 386 | */ |
||
| 387 | public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Set locations |
||
| 401 | * @param array $locations Array of locations |
||
| 402 | */ |
||
| 403 | public function set_locations( $locations = array() ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Read location data from the database |
||
| 415 | * @param int $zone_id |
||
| 416 | */ |
||
| 417 | private function read_zone_locations( $zone_id ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Save locations to the DB. |
||
| 430 | * |
||
| 431 | * This function clears old locations, then re-inserts new if any changes are found. |
||
| 432 | */ |
||
| 433 | private function save_locations() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Add a shipping method to this zone. |
||
| 451 | * @param string $type shipping method type |
||
| 452 | * @return int new instance_id, 0 on failure |
||
| 453 | */ |
||
| 454 | public function add_shipping_method( $type ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Delete a shipping method from a zone. |
||
| 494 | * @param int $instance_id |
||
| 495 | * @return True on success, false on failure |
||
| 496 | */ |
||
| 497 | public function delete_shipping_method( $instance_id ) { |
||
| 511 | } |
||
| 512 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: