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 { |
||
17 | |||
18 | /** |
||
19 | * Zone Data |
||
20 | * @var array |
||
21 | */ |
||
22 | private $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 ) { |
||
52 | |||
53 | /** |
||
54 | * Get class data array |
||
55 | * @return array |
||
56 | */ |
||
57 | public function get_data() { |
||
60 | |||
61 | /** |
||
62 | * Get zone ID |
||
63 | * @return int |
||
64 | */ |
||
65 | public function get_zone_id() { |
||
68 | |||
69 | /** |
||
70 | * Get zone name |
||
71 | * @return string |
||
72 | */ |
||
73 | public function get_zone_name() { |
||
76 | |||
77 | /** |
||
78 | * Get zone order |
||
79 | * @return int |
||
80 | */ |
||
81 | public function get_zone_order() { |
||
84 | |||
85 | /** |
||
86 | * Get zone locations |
||
87 | * @return array of zone objects |
||
88 | */ |
||
89 | public function get_zone_locations() { |
||
92 | |||
93 | /** |
||
94 | * Return a text string representing what this zone is for. |
||
95 | * @return string |
||
96 | */ |
||
97 | public function get_formatted_location( $max = 10 ) { |
||
132 | |||
133 | /** |
||
134 | * Get shipping methods linked to this zone |
||
135 | * @return array of objects |
||
136 | */ |
||
137 | public function get_shipping_methods() { |
||
158 | |||
159 | /** |
||
160 | * Location type detection |
||
161 | * @param object $location |
||
162 | * @return boolean |
||
163 | */ |
||
164 | private function location_is_continent( $location ) { |
||
167 | |||
168 | /** |
||
169 | * Location type detection |
||
170 | * @param object $location |
||
171 | * @return boolean |
||
172 | */ |
||
173 | private function location_is_country( $location ) { |
||
176 | |||
177 | /** |
||
178 | * Location type detection |
||
179 | * @param object $location |
||
180 | * @return boolean |
||
181 | */ |
||
182 | private function location_is_state( $location ) { |
||
185 | |||
186 | /** |
||
187 | * Location type detection |
||
188 | * @param object $location |
||
189 | * @return boolean |
||
190 | */ |
||
191 | private function location_is_postcode( $location ) { |
||
194 | |||
195 | /** |
||
196 | * Set zone ID |
||
197 | * @access private |
||
198 | * @param int $set |
||
199 | */ |
||
200 | private function set_zone_id( $set ) { |
||
203 | |||
204 | /** |
||
205 | * Set zone name |
||
206 | * @param string $set |
||
207 | */ |
||
208 | public function set_zone_name( $set ) { |
||
211 | |||
212 | /** |
||
213 | * Set zone order |
||
214 | * @param int $set |
||
215 | */ |
||
216 | public function set_zone_order( $set ) { |
||
219 | |||
220 | /** |
||
221 | * Insert zone into the database |
||
222 | * @access private |
||
223 | * @param int Read zone data from DB |
||
224 | */ |
||
225 | private function read( $zone_id ) { |
||
235 | |||
236 | /** |
||
237 | * Is passed location type valid? |
||
238 | * @param string $type |
||
239 | * @return boolean |
||
240 | */ |
||
241 | public function is_valid_location_type( $type ) { |
||
244 | |||
245 | /** |
||
246 | * Add location (state or postcode) to a zone. |
||
247 | * @param string $code |
||
248 | * @param string $type state or postcode |
||
249 | */ |
||
250 | public function add_location( $code, $type ) { |
||
260 | |||
261 | /** |
||
262 | * Clear all locations for this zone. |
||
263 | * @param array|string $types of location to clear |
||
264 | */ |
||
265 | public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) { |
||
276 | |||
277 | /** |
||
278 | * Set locations |
||
279 | * @param array $locations Array of locations |
||
280 | */ |
||
281 | public function set_locations( $locations = array() ) { |
||
290 | |||
291 | /** |
||
292 | * Read location data from the database |
||
293 | * @param int $zone_id |
||
294 | */ |
||
295 | private function read_zone_locations( $zone_id ) { |
||
305 | |||
306 | /** |
||
307 | * Save zone data to the database |
||
308 | * @param array data to save for this zone |
||
309 | */ |
||
310 | public function save() { |
||
325 | |||
326 | /** |
||
327 | * Save locations to the DB |
||
328 | * |
||
329 | * This function clears old locations, then re-inserts new if any changes are found. |
||
330 | */ |
||
331 | private function save_locations() { |
||
346 | |||
347 | /** |
||
348 | * Insert zone into the database |
||
349 | * @access private |
||
350 | * @param array $zone_data data to save for this zone |
||
351 | */ |
||
352 | private function create( $zone_data ) { |
||
357 | |||
358 | /** |
||
359 | * Update zone in the database |
||
360 | * @access private |
||
361 | * @param array $zone_data data to save for this zone |
||
362 | */ |
||
363 | public function update( $zone_data ) { |
||
367 | |||
368 | /** |
||
369 | * Add a shipping method to this zone. |
||
370 | * @param string $type shipping method type |
||
371 | * @return int new instance_id, 0 on failure |
||
372 | */ |
||
373 | public function add_shipping_method( $type ) { |
||
399 | } |
||
400 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.