From 22468a79650462e3ece129718228892ecb066463 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 12 May 2021 18:01:48 +0200 Subject: [PATCH] Some initial grouping of the refactored MathTrig functions into small group classes, and renaming of methods --- .../Calculation/Calculation.php | 12 +-- src/PhpSpreadsheet/Calculation/MathTrig.php | 32 +++--- .../Calculation/MathTrig/Ceiling.php | 74 +++++++++++++- .../Calculation/MathTrig/CeilingMath.php | 50 ---------- .../Calculation/MathTrig/CeilingPrecise.php | 38 ------- .../Calculation/MathTrig/Floor.php | 99 ++++++++++++++++++- .../Calculation/MathTrig/FloorMath.php | 64 ------------ .../Calculation/MathTrig/FloorPrecise.php | 51 ---------- 8 files changed, 193 insertions(+), 227 deletions(-) delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/CeilingMath.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/CeilingPrecise.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/FloorMath.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/FloorPrecise.php diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index e32a7c6f..36ce3c54 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -466,17 +466,17 @@ class Calculation ], 'CEILING' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Ceiling::class, 'evaluate'], + 'functionCall' => [MathTrig\Ceiling::class, 'ceiling'], 'argumentCount' => '1-2', // 2 for Excel, 1-2 for Ods/Gnumeric ], 'CEILING.MATH' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\CeilingMath::class, 'evaluate'], + 'functionCall' => [MathTrig\Ceiling::class, 'math'], 'argumentCount' => '1-3', ], 'CEILING.PRECISE' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\CeilingPrecise::class, 'evaluate'], + 'functionCall' => [MathTrig\Ceiling::class, 'precise'], 'argumentCount' => '1,2', ], 'CELL' => [ @@ -1073,17 +1073,17 @@ class Calculation ], 'FLOOR' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Floor::class, 'evaluate'], + 'functionCall' => [MathTrig\Floor::class, 'floor'], 'argumentCount' => '1-2', // Excel requries 2, Ods/Gnumeric 1-2 ], 'FLOOR.MATH' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\FloorMath::class, 'evaluate'], + 'functionCall' => [MathTrig\Floor::class, 'math'], 'argumentCount' => '1-3', ], 'FLOOR.PRECISE' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\FloorPrecise::class, 'evaluate'], + 'functionCall' => [MathTrig\Floor::class, 'precise'], 'argumentCount' => '1-2', ], 'FORECAST' => [ diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index fad381b3..903178cc 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -97,17 +97,17 @@ class MathTrig * * @Deprecated 1.17.0 * - * @see MathTrig\Ceiling::evaluate() - * Use the evaluate() method in the MathTrig\Ceiling class instead - * * @param float $number the number you want to round * @param float $significance the multiple to which you want to round * * @return float|string Rounded Number, or a string containing an error + * + * @see MathTrig\Ceiling::ceiling() + * Use the ceiling() method in the MathTrig\Ceiling class instead */ public static function CEILING($number, $significance = null) { - return MathTrig\Ceiling::evaluate($number, $significance); + return MathTrig\Ceiling::ceiling($number, $significance); } /** @@ -228,17 +228,17 @@ class MathTrig * * @Deprecated 1.17.0 * - * @see MathTrig\Floor::evaluate() - * Use the evaluate() method in the MathTrig\Floor class instead - * * @param float $number Number to round * @param float $significance Significance * * @return float|string Rounded Number, or a string containing an error + * + *@see MathTrig\Floor::floor() + * Use the evaluate() method in the MathTrig\Floor class instead */ public static function FLOOR($number, $significance = null) { - return MathTrig\Floor::evaluate($number, $significance); + return MathTrig\Floor::floor($number, $significance); } /** @@ -251,18 +251,18 @@ class MathTrig * * @Deprecated 1.17.0 * - * @see MathTrig\FloorMath::evaluate() - * Use the evaluate() method in the MathTrig\FloorMath class instead - * * @param float $number Number to round * @param float $significance Significance * @param int $mode direction to round negative numbers * * @return float|string Rounded Number, or a string containing an error + * + *@see MathTrig\Floor::math() + * Use the math() method in the MathTrig\Floor class instead */ public static function FLOORMATH($number, $significance = null, $mode = 0) { - return MathTrig\FloorMath::evaluate($number, $significance, $mode); + return MathTrig\Floor::math($number, $significance, $mode); } /** @@ -275,17 +275,17 @@ class MathTrig * * @Deprecated 1.17.0 * - * @see MathTrig\FloorPrecise::evaluate() - * Use the evaluate() method in the MathTrig\FloorPrecise class instead - * * @param float $number Number to round * @param float $significance Significance * * @return float|string Rounded Number, or a string containing an error + * + *@see MathTrig\Floor::precise() + * Use the precise() method in the MathTrig\Floor class instead */ public static function FLOORPRECISE($number, $significance = 1) { - return MathTrig\FloorPrecise::evaluate($number, $significance); + return MathTrig\Floor::precise($number, $significance); } /** diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php b/src/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php index 8dde1e74..bb6248ba 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php @@ -23,7 +23,7 @@ class Ceiling * * @return float|string Rounded Number, or a string containing an error */ - public static function evaluate($number, $significance = null) + public static function ceiling($number, $significance = null) { if ($significance === null) { self::floorCheck1Arg(); @@ -39,6 +39,78 @@ class Ceiling return self::argumentsOk((float) $number, (float) $significance); } + /** + * CEILING.MATH. + * + * Round a number down to the nearest integer or to the nearest multiple of significance. + * + * Excel Function: + * CEILING.MATH(number[,significance[,mode]]) + * + * @param mixed $number Number to round + * @param mixed $significance Significance + * @param int $mode direction to round negative numbers + * + * @return float|string Rounded Number, or a string containing an error + */ + public static function math($number, $significance = null, $mode = 0) + { + try { + $number = Helpers::validateNumericNullBool($number); + $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1); + $mode = Helpers::validateNumericNullSubstitution($mode, null); + } catch (Exception $e) { + return $e->getMessage(); + } + + if (empty($significance * $number)) { + return 0.0; + } + if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) { + return floor($number / $significance) * $significance; + } + + return ceil($number / $significance) * $significance; + } + + /** + * CEILING.PRECISE. + * + * Rounds number up, away from zero, to the nearest multiple of significance. + * + * Excel Function: + * CEILING.PRECISE(number[,significance]) + * + * @param mixed $number the number you want to round + * @param float $significance the multiple to which you want to round + * + * @return float|string Rounded Number, or a string containing an error + */ + public static function precise($number, $significance = 1) + { + try { + $number = Helpers::validateNumericNullBool($number); + $significance = Helpers::validateNumericNullSubstitution($significance, null); + } catch (Exception $e) { + return $e->getMessage(); + } + + if (!$significance) { + return 0.0; + } + $result = $number / abs($significance); + + return ceil($result) * $significance * (($significance < 0) ? -1 : 1); + } + + /** + * Let CEILINGMATH complexity pass Scrutinizer. + */ + private static function ceilingMathTest(float $significance, float $number, int $mode): bool + { + return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode)); + } + /** * Avoid Scrutinizer problems concerning complexity. * diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/CeilingMath.php b/src/PhpSpreadsheet/Calculation/MathTrig/CeilingMath.php deleted file mode 100644 index 7214b3c5..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/CeilingMath.php +++ /dev/null @@ -1,50 +0,0 @@ -getMessage(); - } - - if (empty($significance * $number)) { - return 0.0; - } - if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) { - return floor($number / $significance) * $significance; - } - - return ceil($number / $significance) * $significance; - } - - /** - * Let CEILINGMATH complexity pass Scrutinizer. - */ - private static function ceilingMathTest(float $significance, float $number, int $mode): bool - { - return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode)); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/CeilingPrecise.php b/src/PhpSpreadsheet/Calculation/MathTrig/CeilingPrecise.php deleted file mode 100644 index 1573af5a..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/CeilingPrecise.php +++ /dev/null @@ -1,38 +0,0 @@ -getMessage(); - } - - if (!$significance) { - return 0.0; - } - $result = $number / abs($significance); - - return ceil($result) * $significance * (($significance < 0) ? -1 : 1); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Floor.php b/src/PhpSpreadsheet/Calculation/MathTrig/Floor.php index b3db0adf..4b84c24a 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Floor.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Floor.php @@ -28,7 +28,7 @@ class Floor * * @return float|string Rounded Number, or a string containing an error */ - public static function evaluate($number, $significance = null) + public static function floor($number, $significance = null) { if ($significance === null) { self::floorCheck1Arg(); @@ -44,6 +44,103 @@ class Floor return self::argumentsOk((float) $number, (float) $significance); } + /** + * FLOOR.MATH. + * + * Round a number down to the nearest integer or to the nearest multiple of significance. + * + * Excel Function: + * FLOOR.MATH(number[,significance[,mode]]) + * + * @param mixed $number Number to round + * @param mixed $significance Significance + * @param mixed $mode direction to round negative numbers + * + * @return float|string Rounded Number, or a string containing an error + */ + public static function math($number, $significance = null, $mode = 0) + { + try { + $number = Helpers::validateNumericNullBool($number); + $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1); + $mode = Helpers::validateNumericNullSubstitution($mode, null); + } catch (Exception $e) { + return $e->getMessage(); + } + + return self::argsOk((float) $number, (float) $significance, (int) $mode); + } + + /** + * FLOOR.PRECISE. + * + * Rounds number down, toward zero, to the nearest multiple of significance. + * + * Excel Function: + * FLOOR.PRECISE(number[,significance]) + * + * @param float $number Number to round + * @param float $significance Significance + * + * @return float|string Rounded Number, or a string containing an error + */ + public static function precise($number, $significance = 1) + { + try { + $number = Helpers::validateNumericNullBool($number); + $significance = Helpers::validateNumericNullSubstitution($significance, null); + } catch (Exception $e) { + return $e->getMessage(); + } + + return self::argumentsOkPrecise((float) $number, (float) $significance); + } + + /** + * Avoid Scrutinizer problems concerning complexity. + * + * @return float|string + */ + private static function argumentsOkPrecise(float $number, float $significance) + { + if ($significance == 0.0) { + return Functions::DIV0(); + } + if ($number == 0.0) { + return 0.0; + } + + return floor($number / abs($significance)) * abs($significance); + } + + /** + * Avoid Scrutinizer complexity problems. + * + * @return float|string Rounded Number, or a string containing an error + */ + private static function argsOk(float $number, float $significance, int $mode) + { + if (!$significance) { + return Functions::DIV0(); + } + if (!$number) { + return 0.0; + } + if (self::floorMathTest($number, $significance, $mode)) { + return ceil($number / $significance) * $significance; + } + + return floor($number / $significance) * $significance; + } + + /** + * Let FLOORMATH complexity pass Scrutinizer. + */ + private static function floorMathTest(float $number, float $significance, int $mode): bool + { + return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode)); + } + /** * Avoid Scrutinizer problems concerning complexity. * diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/FloorMath.php b/src/PhpSpreadsheet/Calculation/MathTrig/FloorMath.php deleted file mode 100644 index 75a99cb2..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/FloorMath.php +++ /dev/null @@ -1,64 +0,0 @@ -getMessage(); - } - - return self::argsOk((float) $number, (float) $significance, (int) $mode); - } - - /** - * Avoid Scrutinizer complexity problems. - * - * @return float|string Rounded Number, or a string containing an error - */ - private static function argsOk(float $number, float $significance, int $mode) - { - if (!$significance) { - return Functions::DIV0(); - } - if (!$number) { - return 0.0; - } - if (self::floorMathTest($number, $significance, $mode)) { - return ceil($number / $significance) * $significance; - } - - return floor($number / $significance) * $significance; - } - - /** - * Let FLOORMATH complexity pass Scrutinizer. - */ - private static function floorMathTest(float $number, float $significance, int $mode): bool - { - return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode)); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/FloorPrecise.php b/src/PhpSpreadsheet/Calculation/MathTrig/FloorPrecise.php deleted file mode 100644 index 1c719d39..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/FloorPrecise.php +++ /dev/null @@ -1,51 +0,0 @@ -getMessage(); - } - - return self::argumentsOk((float) $number, (float) $significance); - } - - /** - * Avoid Scrutinizer problems concerning complexity. - * - * @return float|string - */ - private static function argumentsOk(float $number, float $significance) - { - if ($significance == 0.0) { - return Functions::DIV0(); - } - if ($number == 0.0) { - return 0.0; - } - - return floor($number / abs($significance)) * abs($significance); - } -}