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' => 0, |
||
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 = 0 ) { |
||
54 | |||
55 | /** |
||
56 | * Get ID |
||
57 | * @return int |
||
58 | */ |
||
59 | public function get_id() { |
||
62 | |||
63 | /** |
||
64 | * Insert zone into the database |
||
65 | */ |
||
66 | public function create() { |
||
74 | |||
75 | /** |
||
76 | * Read zone. |
||
77 | * @param int ID to read from DB |
||
78 | */ |
||
79 | public function read( $id ) { |
||
89 | |||
90 | /** |
||
91 | * Update zone in the database |
||
92 | */ |
||
93 | public function update() { |
||
100 | |||
101 | /** |
||
102 | * Delete a zone. |
||
103 | * @since 2.6.0 |
||
104 | */ |
||
105 | public function delete() { |
||
115 | |||
116 | /** |
||
117 | * Save zone data to the database. |
||
118 | */ |
||
119 | public function save() { |
||
120 | $name = $this->get_zone_name(); |
||
121 | |||
122 | if ( empty( $name ) ) { |
||
123 | $this->set_zone_name( $this->generate_zone_name() ); |
||
124 | } |
||
125 | |||
126 | if ( ! $this->get_zone_id() ) { |
||
127 | $this->create(); |
||
128 | } else { |
||
129 | $this->update(); |
||
130 | } |
||
131 | |||
132 | $this->save_locations(); |
||
133 | WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' ); |
||
134 | |||
135 | // Increments the transient version to invalidate cache. |
||
136 | WC_Cache_Helper::get_transient_version( 'shipping', true ); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get zone ID |
||
141 | * @return int |
||
142 | */ |
||
143 | public function get_zone_id() { |
||
146 | |||
147 | /** |
||
148 | * Get zone name |
||
149 | * @return string |
||
150 | */ |
||
151 | public function get_zone_name() { |
||
154 | |||
155 | /** |
||
156 | * Get zone order |
||
157 | * @return int |
||
158 | */ |
||
159 | public function get_zone_order() { |
||
162 | |||
163 | /** |
||
164 | * Get zone locations |
||
165 | * @return array of zone objects |
||
166 | */ |
||
167 | public function get_zone_locations() { |
||
170 | |||
171 | /** |
||
172 | * Generate a zone name based on location. |
||
173 | * @return string |
||
174 | */ |
||
175 | protected function generate_zone_name() { |
||
184 | |||
185 | /** |
||
186 | * Return a text string representing what this zone is for. |
||
187 | * @return string |
||
188 | */ |
||
189 | public function get_formatted_location( $max = 10 ) { |
||
229 | |||
230 | /** |
||
231 | * Get shipping methods linked to this zone |
||
232 | * @param bool Only return enabled methods. |
||
233 | * @return array of objects |
||
234 | */ |
||
235 | public function get_shipping_methods( $enabled_only = false ) { |
||
275 | |||
276 | /** |
||
277 | * Location type detection |
||
278 | * @param object $location |
||
279 | * @return boolean |
||
280 | */ |
||
281 | private function location_is_continent( $location ) { |
||
284 | |||
285 | /** |
||
286 | * Location type detection |
||
287 | * @param object $location |
||
288 | * @return boolean |
||
289 | */ |
||
290 | private function location_is_country( $location ) { |
||
293 | |||
294 | /** |
||
295 | * Location type detection |
||
296 | * @param object $location |
||
297 | * @return boolean |
||
298 | */ |
||
299 | private function location_is_state( $location ) { |
||
302 | |||
303 | /** |
||
304 | * Location type detection |
||
305 | * @param object $location |
||
306 | * @return boolean |
||
307 | */ |
||
308 | private function location_is_postcode( $location ) { |
||
311 | |||
312 | /** |
||
313 | * Set zone ID |
||
314 | * @access private |
||
315 | * @param int $set |
||
316 | */ |
||
317 | private function set_zone_id( $set ) { |
||
320 | |||
321 | /** |
||
322 | * Set zone name |
||
323 | * @param string $set |
||
324 | */ |
||
325 | public function set_zone_name( $set ) { |
||
328 | |||
329 | /** |
||
330 | * Set zone order |
||
331 | * @param int $set |
||
332 | */ |
||
333 | public function set_zone_order( $set ) { |
||
336 | |||
337 | /** |
||
338 | * Is passed location type valid? |
||
339 | * @param string $type |
||
340 | * @return boolean |
||
341 | */ |
||
342 | public function is_valid_location_type( $type ) { |
||
345 | |||
346 | /** |
||
347 | * Add location (state or postcode) to a zone. |
||
348 | * @param string $code |
||
349 | * @param string $type state or postcode |
||
350 | */ |
||
351 | public function add_location( $code, $type ) { |
||
364 | |||
365 | /** |
||
366 | * Clear all locations for this zone. |
||
367 | * @param array|string $types of location to clear |
||
368 | */ |
||
369 | public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) { |
||
380 | |||
381 | /** |
||
382 | * Set locations |
||
383 | * @param array $locations Array of locations |
||
384 | */ |
||
385 | public function set_locations( $locations = array() ) { |
||
394 | |||
395 | /** |
||
396 | * Read location data from the database |
||
397 | * @param int $zone_id |
||
398 | */ |
||
399 | private function read_zone_locations( $zone_id ) { |
||
409 | |||
410 | /** |
||
411 | * Save locations to the DB. |
||
412 | * |
||
413 | * This function clears old locations, then re-inserts new if any changes are found. |
||
414 | */ |
||
415 | private function save_locations() { |
||
430 | |||
431 | /** |
||
432 | * Add a shipping method to this zone. |
||
433 | * @param string $type shipping method type |
||
434 | * @return int new instance_id, 0 on failure |
||
435 | */ |
||
436 | public function add_shipping_method( $type ) { |
||
469 | } |
||
470 |