From 5173eaa3029b6359d7ddf369c97b8e8eadc12606 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Fri, 14 May 2021 12:43:52 +0200 Subject: [PATCH] More grouping and renaming --- .../Calculation/Calculation.php | 12 +- src/PhpSpreadsheet/Calculation/MathTrig.php | 34 ++--- .../MathTrig/{Degrees.php => Angle.php} | 24 +++- .../Calculation/MathTrig/Mod.php | 36 ----- .../Calculation/MathTrig/Operations.php | 136 ++++++++++++++++++ .../Calculation/MathTrig/Power.php | 42 ------ .../Calculation/MathTrig/Product.php | 47 ------ .../Calculation/MathTrig/Quotient.php | 35 ----- .../Calculation/MathTrig/Radians.php | 28 ---- .../Calculation/MathTrig/Subtotal.php | 2 +- 10 files changed, 182 insertions(+), 214 deletions(-) rename src/PhpSpreadsheet/Calculation/MathTrig/{Degrees.php => Angle.php} (50%) delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/Mod.php create mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/Operations.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/Power.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/Product.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/Quotient.php delete mode 100644 src/PhpSpreadsheet/Calculation/MathTrig/Radians.php diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 4249ea3f..31ed497e 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -838,7 +838,7 @@ class Calculation ], 'DEGREES' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Degrees::class, 'evaluate'], + 'functionCall' => [MathTrig\Angle::class, 'toDegrees'], 'argumentCount' => '1', ], 'DELTA' => [ @@ -1704,7 +1704,7 @@ class Calculation ], 'MOD' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Mod::class, 'evaluate'], + 'functionCall' => [MathTrig\Operations::class, 'mod'], 'argumentCount' => '2', ], 'MODE' => [ @@ -1976,7 +1976,7 @@ class Calculation ], 'POWER' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Power::class, 'evaluate'], + 'functionCall' => [MathTrig\Operations::class, 'power'], 'argumentCount' => '2', ], 'PPMT' => [ @@ -2006,7 +2006,7 @@ class Calculation ], 'PRODUCT' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Product::class, 'evaluate'], + 'functionCall' => [MathTrig\Operations::class, 'product'], 'argumentCount' => '1+', ], 'PROPER' => [ @@ -2036,12 +2036,12 @@ class Calculation ], 'QUOTIENT' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Quotient::class, 'evaluate'], + 'functionCall' => [MathTrig\Operations::class, 'quotient'], 'argumentCount' => '2', ], 'RADIANS' => [ 'category' => Category::CATEGORY_MATH_AND_TRIG, - 'functionCall' => [MathTrig\Radians::class, 'evaluate'], + 'functionCall' => [MathTrig\Angle::class, 'toRadians'], 'argumentCount' => '1', ], 'RAND' => [ diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index ea4c265d..ec251f6d 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -449,8 +449,8 @@ class MathTrig * * @Deprecated 1.18.0 * - * @see MathTrig\Mod::evaluate() - * Use the evaluate() method in the MathTrig\Mod class instead + * @see MathTrig\Operations::mod() + * Use the mod() method in the MathTrig\Operations class instead * * @param int $a Dividend * @param int $b Divisor @@ -459,7 +459,7 @@ class MathTrig */ public static function MOD($a = 1, $b = 1) { - return MathTrig\Mod::evaluate($a, $b); + return MathTrig\Operations::mod($a, $b); } /** @@ -527,7 +527,7 @@ class MathTrig * * @Deprecated 1.18.0 * - * @See MathTrig\Power::evaluate() + * @See MathTrig\Operations::power() * Use the evaluate method in the MathTrig\Power class instead * * @param float $x @@ -537,7 +537,7 @@ class MathTrig */ public static function POWER($x = 0, $y = 2) { - return MathTrig\Power::evaluate($x, $y); + return MathTrig\Operations::power($x, $y); } /** @@ -547,8 +547,8 @@ class MathTrig * * @Deprecated 1.18.0 * - * @See MathTrig\Product::evaluate() - * Use the evaluate method in the MathTrig\Product class instead + * @See MathTrig\Operations::product() + * Use the product method in the MathTrig\Operations class instead * * Excel Function: * PRODUCT(value1[,value2[, ...]]) @@ -559,7 +559,7 @@ class MathTrig */ public static function PRODUCT(...$args) { - return MathTrig\Product::evaluate(...$args); + return MathTrig\Operations::product(...$args); } /** @@ -570,8 +570,8 @@ class MathTrig * * @Deprecated 1.18.0 * - * @See MathTrig\Quotient::evaluate() - * Use the evaluate method in the MathTrig\Quotient class instead + * @See MathTrig\Operations::quotient() + * Use the quotient method in the MathTrig\Operations class instead * * Excel Function: * QUOTIENT(value1[,value2[, ...]]) @@ -583,7 +583,7 @@ class MathTrig */ public static function QUOTIENT($numerator, $denominator) { - return MathTrig\Quotient::evaluate($numerator, $denominator); + return MathTrig\Operations::quotient($numerator, $denominator); } /** @@ -1315,8 +1315,8 @@ class MathTrig * * @Deprecated 1.18.0 * - * @See MathTrig\Degrees::evaluate() - * Use the evaluate method in the MathTrig\Degrees class instead + * @See MathTrig\Angle::toDegrees() + * Use the toDegrees method in the MathTrig\Angle class instead * * @param mixed $number Should be numeric * @@ -1324,7 +1324,7 @@ class MathTrig */ public static function builtinDEGREES($number) { - return MathTrig\Degrees::evaluate($number); + return MathTrig\Angle::toDegrees($number); } /** @@ -1391,8 +1391,8 @@ class MathTrig * * @Deprecated 1.18.0 * - * @See MathTrig\Radians::evaluate() - * Use the evaluate method in the MathTrig\Radians class instead + * @See MathTrig\Angle::toRadians() + * Use the toRadians method in the MathTrig\Angle class instead * * @param mixed $number Should be numeric * @@ -1400,7 +1400,7 @@ class MathTrig */ public static function builtinRADIANS($number) { - return MathTrig\Radians::evaluate($number); + return MathTrig\Angle::toRadians($number); } /** diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Degrees.php b/src/PhpSpreadsheet/Calculation/MathTrig/Angle.php similarity index 50% rename from src/PhpSpreadsheet/Calculation/MathTrig/Degrees.php rename to src/PhpSpreadsheet/Calculation/MathTrig/Angle.php index 317c47c8..3062481f 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Degrees.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Angle.php @@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; use PhpOffice\PhpSpreadsheet\Calculation\Exception; -class Degrees +class Angle { /** * DEGREES. @@ -15,7 +15,7 @@ class Degrees * * @return float|string Rounded number */ - public static function evaluate($number) + public static function toDegrees($number) { try { $number = Helpers::validateNumericNullBool($number); @@ -25,4 +25,24 @@ class Degrees return rad2deg($number); } + + /** + * RADIANS. + * + * Returns the result of builtin function deg2rad after validating args. + * + * @param mixed $number Should be numeric + * + * @return float|string Rounded number + */ + public static function toRadians($number) + { + try { + $number = Helpers::validateNumericNullBool($number); + } catch (Exception $e) { + return $e->getMessage(); + } + + return deg2rad($number); + } } diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Mod.php b/src/PhpSpreadsheet/Calculation/MathTrig/Mod.php deleted file mode 100644 index 813434ad..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Mod.php +++ /dev/null @@ -1,36 +0,0 @@ -getMessage(); - } - - if (($dividend < 0.0) && ($divisor > 0.0)) { - return $divisor - fmod(abs($dividend), $divisor); - } - if (($dividend > 0.0) && ($divisor < 0.0)) { - return $divisor + fmod($dividend, abs($divisor)); - } - - return fmod($dividend, $divisor); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php b/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php new file mode 100644 index 00000000..595c7fdc --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php @@ -0,0 +1,136 @@ +getMessage(); + } + + if (($dividend < 0.0) && ($divisor > 0.0)) { + return $divisor - fmod(abs($dividend), $divisor); + } + if (($dividend > 0.0) && ($divisor < 0.0)) { + return $divisor + fmod($dividend, abs($divisor)); + } + + return fmod($dividend, $divisor); + } + + /** + * POWER. + * + * Computes x raised to the power y. + * + * @param float|int $x + * @param float|int $y + * + * @return float|int|string The result, or a string containing an error + */ + public static function power($x, $y) + { + try { + $x = Helpers::validateNumericNullBool($x); + $y = Helpers::validateNumericNullBool($y); + } catch (Exception $e) { + return $e->getMessage(); + } + + // Validate parameters + if (!$x && !$y) { + return Functions::NAN(); + } + if (!$x && $y < 0.0) { + return Functions::DIV0(); + } + + // Return + $result = $x ** $y; + + return Helpers::numberOrNan($result); + } + + /** + * PRODUCT. + * + * PRODUCT returns the product of all the values and cells referenced in the argument list. + * + * Excel Function: + * PRODUCT(value1[,value2[, ...]]) + * + * @param mixed ...$args Data values + * + * @return float|string + */ + public static function product(...$args) + { + // Return value + $returnValue = null; + + // Loop through arguments + foreach (Functions::flattenArray($args) as $arg) { + // Is it a numeric value? + if (is_numeric($arg)) { + if ($returnValue === null) { + $returnValue = $arg; + } else { + $returnValue *= $arg; + } + } else { + return Functions::VALUE(); + } + } + + // Return + if ($returnValue === null) { + return 0; + } + + return $returnValue; + } + + /** + * QUOTIENT. + * + * QUOTIENT function returns the integer portion of a division. Numerator is the divided number + * and denominator is the divisor. + * + * Excel Function: + * QUOTIENT(value1,value2) + * + * @param mixed $numerator Expect float|int + * @param mixed $denominator Expect float|int + * + * @return int|string + */ + public static function quotient($numerator, $denominator) + { + try { + $numerator = Helpers::validateNumericNullSubstitution($numerator, 0); + $denominator = Helpers::validateNumericNullSubstitution($denominator, 0); + Helpers::validateNotZero($denominator); + } catch (Exception $e) { + return $e->getMessage(); + } + + return (int) ($numerator / $denominator); + } +} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Power.php b/src/PhpSpreadsheet/Calculation/MathTrig/Power.php deleted file mode 100644 index 798ad535..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Power.php +++ /dev/null @@ -1,42 +0,0 @@ -getMessage(); - } - - // Validate parameters - if (!$x && !$y) { - return Functions::NAN(); - } - if (!$x && $y < 0.0) { - return Functions::DIV0(); - } - - // Return - $result = $x ** $y; - - return Helpers::numberOrNan($result); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Product.php b/src/PhpSpreadsheet/Calculation/MathTrig/Product.php deleted file mode 100644 index 79c3f192..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Product.php +++ /dev/null @@ -1,47 +0,0 @@ -getMessage(); - } - - return (int) ($numerator / $denominator); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Radians.php b/src/PhpSpreadsheet/Calculation/MathTrig/Radians.php deleted file mode 100644 index ca549c03..00000000 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Radians.php +++ /dev/null @@ -1,28 +0,0 @@ -getMessage(); - } - - return deg2rad($number); - } -} diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php b/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php index 946142a7..48ba8177 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Subtotal.php @@ -57,7 +57,7 @@ class Subtotal [Statistical\Counts::class, 'COUNTA'], // 3 [Statistical\Maximum::class, 'MAX'], // 4 [Statistical\Minimum::class, 'MIN'], // 5 - [Product::class, 'evaluate'], // 6 + [Operations::class, 'product'], // 6 [Statistical\StandardDeviations::class, 'STDEV'], // 7 [Statistical\StandardDeviations::class, 'STDEVP'], // 8 [Sum::class, 'sumIgnoringStrings'], // 9