From 6380a778d9bef6585b550140e686626301bf69c6 Mon Sep 17 00:00:00 2001 From: "FRESCHE-CORP\\Sebastien.Duguay" Date: Mon, 27 Nov 2023 19:07:15 +0100 Subject: [PATCH] Move exception building to NumberFormatException --- src/BigNumber.php | 17 +++-------------- src/Exception/NumberFormatException.php | 10 +++++++++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/BigNumber.php b/src/BigNumber.php index 2f964bd..f623c6f 100644 --- a/src/BigNumber.php +++ b/src/BigNumber.php @@ -89,7 +89,7 @@ private static function _of(BigNumber|int|float|string $value) : BigNumber if (str_contains($value, '/')) { // Rational number if (\preg_match(self::PARSE_REGEXP_RATIONAL, $value, $matches, PREG_UNMATCHED_AS_NULL) !== 1) { - self::throwException($value); + throw NumberFormatException::invalidFormat($value); } $sign = $matches['sign']; @@ -114,7 +114,7 @@ private static function _of(BigNumber|int|float|string $value) : BigNumber } else { // Integer or decimal number if (\preg_match(self::PARSE_REGEXP_NUMERICAL, $value, $matches, PREG_UNMATCHED_AS_NULL) !== 1) { - self::throwException($value); + throw NumberFormatException::invalidFormat($value); } $sign = $matches['sign']; @@ -124,7 +124,7 @@ private static function _of(BigNumber|int|float|string $value) : BigNumber $exponent = $matches['exponent']; if ($integral === null && $fractional === null) { - self::throwException($value); + throw NumberFormatException::invalidFormat($value); } if ($integral === null) { @@ -159,17 +159,6 @@ private static function _of(BigNumber|int|float|string $value) : BigNumber } } - /** - * Throws a NumberFormatException for the given value. - * @psalm-pure - */ - private static function throwException(string $value) : never { - throw new NumberFormatException(\sprintf( - 'The given value "%s" does not represent a valid number.', - $value - )); - } - /** * Overridden by subclasses to convert a BigNumber to an instance of the subclass. * diff --git a/src/Exception/NumberFormatException.php b/src/Exception/NumberFormatException.php index d9cf6ff..119cadb 100644 --- a/src/Exception/NumberFormatException.php +++ b/src/Exception/NumberFormatException.php @@ -9,6 +9,14 @@ */ class NumberFormatException extends MathException { + public static function invalidFormat(string $value) : self + { + return new self(\sprintf( + 'The given value "%s" does not represent a valid number.', + $value, + )); + } + /** * @param string $char The failing character. * @@ -28,6 +36,6 @@ public static function charNotInAlphabet(string $char) : self $char = '"' . $char . '"'; } - return new self(sprintf('Char %s is not a valid character in the given alphabet.', $char)); + return new self(\sprintf('Char %s is not a valid character in the given alphabet.', $char)); } }