Conditions | 13 |
Paths | 4 |
Total Lines | 78 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
143 | public static function get_zone_matching_package( $package ) { |
||
144 | global $wpdb; |
||
145 | |||
146 | $country = strtoupper( wc_clean( $package['destination']['country'] ) ); |
||
147 | $state = strtoupper( wc_clean( $package['destination']['state'] ) ); |
||
148 | $continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) ); |
||
149 | $postcode = strtoupper( wc_clean( $package['destination']['postcode'] ) ); |
||
150 | $valid_postcodes = array_map( 'wc_clean', self::_get_wildcard_postcodes( $postcode ) ); |
||
151 | $cache_key = WC_Cache_Helper::get_cache_prefix( 'shipping_zones' ) . 'wc_shipping_zone_' . md5( sprintf( '%s+%s+%s', $country, $state, $postcode ) ); |
||
152 | $matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' ); |
||
153 | |||
154 | if ( false === $matching_zone_id ) { |
||
155 | |||
156 | // Work out criteria for our zone search |
||
157 | $criteria = array(); |
||
158 | $criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country ); |
||
159 | $criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state ); |
||
160 | $criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s ) )", $continent ); |
||
161 | |||
162 | // Postcode range and wildcard matching |
||
163 | $postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" ); |
||
164 | |||
165 | if ( $postcode_locations ) { |
||
166 | $zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) ); |
||
167 | $zone_id_matches = array(); |
||
168 | |||
169 | foreach ( $postcode_locations as $postcode_location ) { |
||
170 | $postcode_to_match = trim( strtoupper( $postcode_location->location_code ) ); |
||
171 | |||
172 | // Ranges |
||
173 | if ( strstr( '-', $postcode_to_match ) ) { |
||
174 | $range = array_map( 'trim', explode( '-', $postcode_to_match ) ); |
||
175 | |||
176 | if ( sizeof( $range ) != 2 ) { |
||
177 | continue; |
||
178 | } |
||
179 | |||
180 | if ( is_numeric( $range[0] ) && is_numeric( $range[1] ) ) { |
||
181 | $encoded_postcode = $postcode; |
||
182 | $min = $range[0]; |
||
183 | $max = $range[1]; |
||
184 | } else { |
||
185 | $min = wc_make_numeric_postcode( $range[0] ); |
||
186 | $max = wc_make_numeric_postcode( $range[1] ); |
||
187 | $min = str_pad( $min, $encoded_postcode_len, '0' ); |
||
188 | $max = str_pad( $max, $encoded_postcode_len, '9' ); |
||
189 | } |
||
190 | |||
191 | if ( $encoded_postcode >= $min && $encoded_postcode <= $max ) { |
||
192 | $zone_id_matches[] = absint( $postcode_location->zone_id ); |
||
193 | } |
||
194 | |||
195 | // Wildcard/standard |
||
196 | } elseif ( in_array( $postcode_to_match, $valid_postcodes ) ) { |
||
197 | $zone_id_matches[] = absint( $postcode_location->zone_id ); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, $zone_id_matches ) ); |
||
202 | |||
203 | if ( $do_not_match ) { |
||
204 | $criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")"; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | // Get matching zones |
||
209 | $matching_zone_id = $wpdb->get_var( " |
||
210 | SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones |
||
211 | LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id |
||
212 | WHERE " . implode( ' ', $criteria ) . " |
||
213 | ORDER BY zone_order ASC LIMIT 1 |
||
214 | " ); |
||
215 | |||
216 | wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' ); |
||
217 | } |
||
218 | |||
219 | return new WC_Shipping_Zone( $matching_zone_id ? $matching_zone_id : 0 ); |
||
220 | } |
||
221 | } |
||
222 |
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.