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 | /** |
||
| 19 | * Zone Data |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $_data = array( |
||
| 23 | 'zone_id' => null, |
||
| 24 | 'zone_name' => '', |
||
| 25 | 'zone_order' => 0, |
||
| 26 | 'zone_locations' => array() |
||
| 27 | ); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * True when location data needs to be re-saved |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | private $_locations_changed = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor for zones |
||
| 37 | * @param int|object $zone Zone ID to load from the DB (optional) or already queried data. |
||
| 38 | */ |
||
| 39 | public function __construct( $zone = null ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Insert zone into the database |
||
| 58 | */ |
||
| 59 | public function create() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Read zone. |
||
| 70 | * @param int ID to read from DB |
||
| 71 | */ |
||
| 72 | public function read( $id ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Update zone in the database |
||
| 85 | */ |
||
| 86 | public function update() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Delete a zone. |
||
| 99 | * @since 2.6.0 |
||
| 100 | */ |
||
| 101 | public function delete() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Save zone data to the database. |
||
| 114 | */ |
||
| 115 | public function save() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get ID |
||
| 137 | * @return int|null Null if the zone does not exist. 0 is the default zone. |
||
| 138 | */ |
||
| 139 | public function get_id() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get zone ID |
||
| 145 | * @return int|null Null if the zone does not exist. 0 is the default zone. |
||
| 146 | */ |
||
| 147 | public function get_zone_id() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get zone name |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function get_zone_name() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get zone order |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | public function get_zone_order() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get zone locations |
||
| 169 | * @return array of zone objects |
||
| 170 | */ |
||
| 171 | public function get_zone_locations() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Generate a zone name based on location. |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | protected function generate_zone_name() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return a text string representing what this zone is for. |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function get_formatted_location( $max = 10 ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get shipping methods linked to this zone |
||
| 236 | * @param bool Only return enabled methods. |
||
| 237 | * @return array of objects |
||
| 238 | */ |
||
| 239 | public function get_shipping_methods( $enabled_only = false ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Location type detection |
||
| 286 | * @param object $location |
||
| 287 | * @return boolean |
||
| 288 | */ |
||
| 289 | private function location_is_continent( $location ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Location type detection |
||
| 295 | * @param object $location |
||
| 296 | * @return boolean |
||
| 297 | */ |
||
| 298 | private function location_is_country( $location ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Location type detection |
||
| 304 | * @param object $location |
||
| 305 | * @return boolean |
||
| 306 | */ |
||
| 307 | private function location_is_state( $location ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Location type detection |
||
| 313 | * @param object $location |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | private function location_is_postcode( $location ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Set zone ID |
||
| 322 | * @access private |
||
| 323 | * @param int $set |
||
| 324 | */ |
||
| 325 | private function set_id( $set ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set zone ID |
||
| 331 | * @access private |
||
| 332 | * @param int $set |
||
| 333 | */ |
||
| 334 | private function set_zone_id( $set ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set zone name |
||
| 340 | * @param string $set |
||
| 341 | */ |
||
| 342 | public function set_zone_name( $set ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set zone order |
||
| 348 | * @param int $set |
||
| 349 | */ |
||
| 350 | public function set_zone_order( $set ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Is passed location type valid? |
||
| 356 | * @param string $type |
||
| 357 | * @return boolean |
||
| 358 | */ |
||
| 359 | public function is_valid_location_type( $type ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Add location (state or postcode) to a zone. |
||
| 365 | * @param string $code |
||
| 366 | * @param string $type state or postcode |
||
| 367 | */ |
||
| 368 | public function add_location( $code, $type ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Clear all locations for this zone. |
||
| 384 | * @param array|string $types of location to clear |
||
| 385 | */ |
||
| 386 | public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Set locations |
||
| 400 | * @param array $locations Array of locations |
||
| 401 | */ |
||
| 402 | public function set_locations( $locations = array() ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Read location data from the database |
||
| 414 | * @param int $zone_id |
||
| 415 | */ |
||
| 416 | private function read_zone_locations( $zone_id ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Save locations to the DB. |
||
| 429 | * |
||
| 430 | * This function clears old locations, then re-inserts new if any changes are found. |
||
| 431 | */ |
||
| 432 | private function save_locations() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Add a shipping method to this zone. |
||
| 450 | * @param string $type shipping method type |
||
| 451 | * @return int new instance_id, 0 on failure |
||
| 452 | */ |
||
| 453 | public function add_shipping_method( $type ) { |
||
| 490 | } |
||
| 491 |
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: