Conditions | 24 |
Paths | 273 |
Total Lines | 71 |
Code Lines | 45 |
Lines | 10 |
Ratio | 14.08 % |
Changes | 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 |
||
145 | public function is_available( $package ) { |
||
146 | if ( 'no' == $this->enabled ) { |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | if ( 'specific' == $this->availability ) { |
||
151 | $ship_to_countries = $this->countries; |
||
152 | } else { |
||
153 | $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() ); |
||
154 | } |
||
155 | |||
156 | if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) { |
||
157 | return false; |
||
158 | } |
||
159 | |||
160 | // Enabled logic |
||
161 | $is_available = false; |
||
162 | $has_coupon = false; |
||
163 | $has_met_min_amount = false; |
||
164 | |||
165 | if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) { |
||
166 | |||
167 | if ( $coupons = WC()->cart->get_coupons() ) { |
||
168 | foreach ( $coupons as $code => $coupon ) { |
||
169 | if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) { |
||
170 | $has_coupon = true; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) { |
||
177 | if ( WC()->cart->prices_include_tax ) { |
||
178 | $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes ); |
||
179 | } else { |
||
180 | $total = WC()->cart->cart_contents_total; |
||
181 | } |
||
182 | |||
183 | if ( $total >= $this->min_amount ) { |
||
184 | $has_met_min_amount = true; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | switch ( $this->requires ) { |
||
189 | case 'min_amount' : |
||
190 | if ( $has_met_min_amount ) { |
||
191 | $is_available = true; |
||
192 | } |
||
193 | break; |
||
194 | case 'coupon' : |
||
195 | if ( $has_coupon ) { |
||
196 | $is_available = true; |
||
197 | } |
||
198 | break; |
||
199 | case 'both' : |
||
200 | if ( $has_met_min_amount && $has_coupon ) { |
||
201 | $is_available = true; |
||
202 | } |
||
203 | break; |
||
204 | case 'either' : |
||
205 | if ( $has_met_min_amount || $has_coupon ) { |
||
206 | $is_available = true; |
||
207 | } |
||
208 | break; |
||
209 | default : |
||
210 | $is_available = true; |
||
211 | break; |
||
212 | } |
||
213 | |||
214 | return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); |
||
215 | } |
||
216 | |||
232 |