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 | * Insert zone into the database |
||
| 59 | */ |
||
| 60 | public function create() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Read zone. |
||
| 71 | * @param int ID to read from DB |
||
| 72 | */ |
||
| 73 | public function read( $id ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Update zone in the database |
||
| 86 | */ |
||
| 87 | public function update() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Delete a zone. |
||
| 100 | * @since 2.6.0 |
||
| 101 | */ |
||
| 102 | public function delete() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Save zone data to the database. |
||
| 115 | */ |
||
| 116 | public function save() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set ID. |
||
| 138 | * @param int|null $id |
||
| 139 | */ |
||
| 140 | public function set_id( $id ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get ID |
||
| 146 | * @return int|null Null if the zone does not exist. 0 is the default zone. |
||
| 147 | */ |
||
| 148 | public function get_id() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get zone ID |
||
| 154 | * @return int|null Null if the zone does not exist. 0 is the default zone. |
||
| 155 | */ |
||
| 156 | public function get_zone_id() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get zone name |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function get_zone_name() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get zone order |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | public function get_zone_order() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get zone locations |
||
| 178 | * @return array of zone objects |
||
| 179 | */ |
||
| 180 | public function get_zone_locations() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Generate a zone name based on location. |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | protected function generate_zone_name() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return a text string representing what this zone is for. |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function get_formatted_location( $max = 10 ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get shipping methods linked to this zone |
||
| 247 | * @param bool Only return enabled methods. |
||
| 248 | * @return array of objects |
||
| 249 | */ |
||
| 250 | public function get_shipping_methods( $enabled_only = false ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Location type detection |
||
| 297 | * @param object $location |
||
| 298 | * @return boolean |
||
| 299 | */ |
||
| 300 | private function location_is_continent( $location ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Location type detection |
||
| 306 | * @param object $location |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | private function location_is_country( $location ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Location type detection |
||
| 315 | * @param object $location |
||
| 316 | * @return boolean |
||
| 317 | */ |
||
| 318 | private function location_is_state( $location ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Location type detection |
||
| 324 | * @param object $location |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | private function location_is_postcode( $location ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Set zone name |
||
| 333 | * @param string $set |
||
| 334 | */ |
||
| 335 | public function set_zone_name( $set ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set zone order |
||
| 341 | * @param int $set |
||
| 342 | */ |
||
| 343 | public function set_zone_order( $set ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Is passed location type valid? |
||
| 349 | * @param string $type |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | public function is_valid_location_type( $type ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Add location (state or postcode) to a zone. |
||
| 358 | * @param string $code |
||
| 359 | * @param string $type state or postcode |
||
| 360 | */ |
||
| 361 | public function add_location( $code, $type ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Clear all locations for this zone. |
||
| 377 | * @param array|string $types of location to clear |
||
| 378 | */ |
||
| 379 | public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set locations |
||
| 393 | * @param array $locations Array of locations |
||
| 394 | */ |
||
| 395 | public function set_locations( $locations = array() ) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Read location data from the database |
||
| 407 | * @param int $zone_id |
||
| 408 | */ |
||
| 409 | private function read_zone_locations( $zone_id ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Save locations to the DB. |
||
| 422 | * |
||
| 423 | * This function clears old locations, then re-inserts new if any changes are found. |
||
| 424 | */ |
||
| 425 | private function save_locations() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Add a shipping method to this zone. |
||
| 443 | * @param string $type shipping method type |
||
| 444 | * @return int new instance_id, 0 on failure |
||
| 445 | */ |
||
| 446 | public function add_shipping_method( $type ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Delete a shipping method from a zone. |
||
| 486 | * @param int $instance_id |
||
| 487 | * @return True on success, false on failure |
||
| 488 | */ |
||
| 489 | public function delete_shipping_method( $instance_id ) { |
||
| 503 | } |
||
| 504 |
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: