From 73350e5418a2f84b80db5746c8b03f75acac1d07 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 9 Aug 2017 11:59:50 +0100 Subject: [PATCH 1/2] Fake round half down in PHP 5,2 Fixes unit tests. --- includes/wc-cart-functions.php | 14 ++++++++++++- includes/wc-formatting-functions.php | 31 +++++++++++++++++++--------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/includes/wc-cart-functions.php b/includes/wc-cart-functions.php index c9b44b75dd0..45fe70639da 100644 --- a/includes/wc-cart-functions.php +++ b/includes/wc-cart-functions.php @@ -369,7 +369,19 @@ function wc_cart_round_discount( $value, $precision ) { if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) { return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE ); } else { - return round( $value, $precision ); + // Fake it in PHP 5.2. + if ( 2 === WC_DISCOUNT_ROUNDING_MODE ) { + $value = (string) $value; + $value = explode( '.', $value ); + $value[1] = substr( $value[1], 0, $precision + 1 ); + $value = implode( '.', $value ); + + if ( substr( $value, -1 ) === '5' ) { + $value = substr( $value, 0, -1 ) . '4'; + } + $value = floatval( $value ); + } + return round( $value, $precision ); } } diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index c0d1ba2ff70..2e82cc325c8 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -216,21 +216,32 @@ function wc_trim_zeros( $price ) { /** * Round a tax amount. * - * @param double $tax Amount to round. - * @param int $dp DP to round. Defaults to wc_get_price_decimals. + * @param double $value Amount to round. + * @param int $precision DP to round. Defaults to wc_get_price_decimals. * @return double */ -function wc_round_tax_total( $tax, $dp = null ) { - $dp = is_null( $dp ) ? wc_get_price_decimals() : absint( $dp ); +function wc_round_tax_total( $value, $precision = null ) { + $precision = is_null( $precision ) ? wc_get_price_decimals() : absint( $precision ); - // @codeCoverageIgnoreStart - if ( version_compare( phpversion(), '5.3', '<' ) ) { - $rounded_tax = round( $tax, $dp ); + if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) { + $rounded_tax = round( $value, $precision, WC_TAX_ROUNDING_MODE ); } else { - // @codeCoverageIgnoreEnd - $rounded_tax = round( $tax, $dp, WC_TAX_ROUNDING_MODE ); + // Fake it in PHP 5.2. + if ( 2 === WC_TAX_ROUNDING_MODE ) { + $value = (string) $value; + $value = explode( '.', $value ); + $value[1] = substr( $value[1], 0, $precision + 1 ); + $value = implode( '.', $value ); + + if ( substr( $value, -1 ) === '5' ) { + $value = substr( $value, 0, -1 ) . '4'; + } + $value = floatval( $value ); + } + $rounded_tax = round( $value, $precision ); } - return apply_filters( 'wc_round_tax_total', $rounded_tax, $tax, $dp, WC_TAX_ROUNDING_MODE ); + + return apply_filters( 'wc_round_tax_total', $rounded_tax, $value, $precision, WC_TAX_ROUNDING_MODE ); } /** From aa82a54f477e9b3925acd8bf39f4d903b7c04956 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 9 Aug 2017 19:27:19 +0100 Subject: [PATCH 2/2] Prevent notices --- includes/wc-cart-functions.php | 2 +- includes/wc-formatting-functions.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/wc-cart-functions.php b/includes/wc-cart-functions.php index 45fe70639da..90dde98081a 100644 --- a/includes/wc-cart-functions.php +++ b/includes/wc-cart-functions.php @@ -370,7 +370,7 @@ function wc_cart_round_discount( $value, $precision ) { return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE ); } else { // Fake it in PHP 5.2. - if ( 2 === WC_DISCOUNT_ROUNDING_MODE ) { + if ( 2 === WC_DISCOUNT_ROUNDING_MODE && strstr( $value, '.' ) ) { $value = (string) $value; $value = explode( '.', $value ); $value[1] = substr( $value[1], 0, $precision + 1 ); diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index 2e82cc325c8..f49199bcde6 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -227,7 +227,7 @@ function wc_round_tax_total( $value, $precision = null ) { $rounded_tax = round( $value, $precision, WC_TAX_ROUNDING_MODE ); } else { // Fake it in PHP 5.2. - if ( 2 === WC_TAX_ROUNDING_MODE ) { + if ( 2 === WC_TAX_ROUNDING_MODE && strstr( $value, '.' ) ) { $value = (string) $value; $value = explode( '.', $value ); $value[1] = substr( $value[1], 0, $precision + 1 );