Conditions | 13 |
Paths | 72 |
Total Lines | 52 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
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 |
||
193 | public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) { |
||
194 | _deprecated_function( 'get_discount_amount', '2.7', 'WC_Cart_Totals::get_discounted_price' ); |
||
195 | $discount = 0; |
||
196 | $cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity']; |
||
197 | |||
198 | if ( $this->is_type( array( 'percent_product', 'percent' ) ) ) { |
||
199 | $discount = $this->get_amount() * ( $discounting_amount / 100 ); |
||
200 | } elseif ( $this->is_type( 'fixed_cart' ) && ! is_null( $cart_item ) && WC()->cart->get_subtotal() ) { |
||
201 | /** |
||
202 | * This is the most complex discount - we need to divide the discount between rows based on their price in. |
||
203 | * proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows. |
||
204 | * with no price (free) don't get discounted. |
||
205 | * |
||
206 | * Get item discount by dividing item cost by subtotal to get a %. |
||
207 | * |
||
208 | * Uses price inc tax if prices include tax to work around https://github.com/woothemes/woocommerce/issues/7669 and https://github.com/woothemes/woocommerce/issues/8074. |
||
209 | */ |
||
210 | if ( wc_prices_include_tax() ) { |
||
211 | $discount_percent = ( $cart_item['data']->get_price_including_tax() * $cart_item_qty ) / WC()->cart->get_subtotal(); |
||
212 | } else { |
||
213 | $discount_percent = ( $cart_item['data']->get_price_excluding_tax() * $cart_item_qty ) / WC()->cart->get_subtotal( false ); |
||
214 | } |
||
215 | $discount = ( $this->get_amount() * $discount_percent ) / $cart_item_qty; |
||
216 | |||
217 | } elseif ( $this->is_type( 'fixed_product' ) ) { |
||
218 | $discount = min( $this->get_amount(), $discounting_amount ); |
||
219 | $discount = $single ? $discount : $discount * $cart_item_qty; |
||
220 | } |
||
221 | |||
222 | $discount = min( $discount, $discounting_amount ); |
||
223 | |||
224 | // Handle the limit_usage_to_x_items option |
||
225 | if ( $this->is_type( array( 'percent_product', 'fixed_product' ) ) ) { |
||
226 | if ( $discounting_amount ) { |
||
227 | if ( '' === $this->get_limit_usage_to_x_items() ) { |
||
228 | $limit_usage_qty = $cart_item_qty; |
||
229 | } else { |
||
230 | $limit_usage_qty = min( $this->get_limit_usage_to_x_items(), $cart_item_qty ); |
||
231 | $this->set_limit_usage_to_x_items( max( 0, $this->get_limit_usage_to_x_items() - $limit_usage_qty ) ); |
||
232 | } |
||
233 | if ( $single ) { |
||
234 | $discount = ( $discount * $limit_usage_qty ) / $cart_item_qty; |
||
235 | } else { |
||
236 | $discount = ( $discount / $cart_item_qty ) * $limit_usage_qty; |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | $discount = round( $discount, wc_get_rounding_precision() ); |
||
242 | |||
243 | return apply_filters( 'woocommerce_coupon_get_discount_amount', $discount, $discounting_amount, $cart_item, $single, $this ); |
||
244 | } |
||
245 | } |
||
246 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.