1
0
mirror of https://github.com/danog/math.git synced 2024-11-27 04:14:40 +01:00

BigNumber::of() now throws NumberFormatException instead of InvalidArgumentException

This allows to catch all of() exceptions with the parent, ArithmeticException.
This commit is contained in:
Benjamin Morel 2015-06-22 22:13:19 +02:00
parent d6aa1007a9
commit 27bc623b70
6 changed files with 77 additions and 56 deletions

View File

@ -48,6 +48,8 @@ final class BigDecimal extends BigNumber implements \Serializable
* @param int $scale The scale of the number.
*
* @return BigDecimal
*
* @throws \InvalidArgumentException
*/
public static function ofUnscaledValue($value, $scale = 0)
{

View File

@ -3,6 +3,7 @@
namespace Brick\Math;
use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException;
/**
@ -42,7 +43,7 @@ abstract class BigNumber
*
* @return static
*
* @throws \InvalidArgumentException If the number is not valid.
* @throws NumberFormatException If the format of the number is not valid.
* @throws DivisionByZeroException If the value represents a rational number with a denominator of zero.
* @throws RoundingNecessaryException If the value represents a valid number, but this number cannot be converted
* to the subclass this method has been called on, without rounding.
@ -50,7 +51,6 @@ abstract class BigNumber
public static function of($value)
{
if ($value instanceof BigNumber) {
try {
switch (static::class) {
case BigInteger::class:
return $value->toBigInteger();
@ -64,11 +64,6 @@ abstract class BigNumber
default:
return $value;
}
} catch (RoundingNecessaryException $e) {
$className = substr(static::class, strrpos(static::class, '\\') + 1);
throw new \InvalidArgumentException('Cannot convert value to a ' . $className . ' without losing precision.');
}
}
if (is_int($value)) {
@ -87,7 +82,7 @@ abstract class BigNumber
$value = (string) $value;
if (preg_match(BigNumber::REGEXP, $value, $matches) !== 1) {
throw new \InvalidArgumentException('The given value does not represent a valid number.');
throw new NumberFormatException('The given value does not represent a valid number.');
}
if (isset($matches['denominator'])) {
@ -100,7 +95,6 @@ abstract class BigNumber
$result = new BigRational(new BigInteger($numerator), new BigInteger($denominator), false);
try {
switch (static::class) {
case BigInteger::class:
return $result->toBigInteger();
@ -111,11 +105,6 @@ abstract class BigNumber
default:
return $result;
}
} catch (RoundingNecessaryException $e) {
$className = substr(static::class, strrpos(static::class, '\\') + 1);
throw new \InvalidArgumentException('Cannot convert value to a ' . $className . ' without losing precision.');
}
} elseif (isset($matches['fractional']) || isset($matches['exponent'])) {
$fractional = isset($matches['fractional']) ? $matches['fractional'] : '';
$exponent = isset($matches['exponent']) ? (int) $matches['exponent'] : 0;
@ -133,7 +122,6 @@ abstract class BigNumber
$result = new BigDecimal($unscaledValue, $scale);
try {
switch (static::class) {
case BigInteger::class:
return $result->toBigInteger();
@ -144,11 +132,6 @@ abstract class BigNumber
default:
return $result;
}
} catch (RoundingNecessaryException $e) {
$className = substr(static::class, strrpos(static::class, '\\') + 1);
throw new \InvalidArgumentException('Cannot convert value to a ' . $className . ' without losing precision.');
}
} else {
$integral = BigNumber::cleanUp($matches['integral']);

View File

@ -0,0 +1,11 @@
<?php
namespace Brick\Math\Exception;
/**
* Exception thrown when attempting to create a number from a string with an invalid format.
*/
class NumberFormatException extends ArithmeticException
{
}

View File

@ -163,7 +163,7 @@ class BigDecimalTest extends AbstractTestCase
/**
* @dataProvider providerOfInvalidValueThrowsException
* @expectedException \InvalidArgumentException
* @expectedException \Brick\Math\Exception\NumberFormatException
*
* @param string $value
*/

View File

@ -87,12 +87,12 @@ class BigIntegerTest extends AbstractTestCase
}
/**
* @dataProvider providerOfInvalidValueThrowsException
* @expectedException \InvalidArgumentException
* @dataProvider providerOfInvalidFormatThrowsException
* @expectedException \Brick\Math\Exception\NumberFormatException
*
* @param string|number $value
*/
public function testOfInvalidValueThrowsException($value)
public function testOfInvalidFormatThrowsException($value)
{
BigInteger::of($value);
}
@ -100,11 +100,9 @@ class BigIntegerTest extends AbstractTestCase
/**
* @return array
*/
public function providerOfInvalidValueThrowsException()
public function providerOfInvalidFormatThrowsException()
{
return [
[1.1],
[''],
['a'],
[' 1'],
@ -113,7 +111,34 @@ class BigIntegerTest extends AbstractTestCase
['+'],
['-'],
['+a'],
['-a']
['-a'],
['a0'],
['0a'],
['1.a'],
['a.1'],
];
}
/**
* @dataProvider providerOfNonConvertibleValueThrowsException
* @expectedException \Brick\Math\Exception\RoundingNecessaryException
*
* @param float|string $value
*/
public function testOfNonConvertibleValueThrowsException($value)
{
BigInteger::of($value);
}
/**
* @return array
*/
public function providerOfNonConvertibleValueThrowsException()
{
return [
[1.1],
['1e-1'],
['7/9'],
];
}

View File

@ -86,12 +86,12 @@ class BigRationalTest extends AbstractTestCase
}
/**
* @dataProvider providerParseInvalidString
* @expectedException \InvalidArgumentException
* @dataProvider providerOfInvalidString
* @expectedException \Brick\Math\Exception\NumberFormatException
*
* @param string $string An invalid string representation.
*/
public function testParseInvalidString($string)
public function testOfInvalidString($string)
{
BigRational::of($string);
}
@ -99,7 +99,7 @@ class BigRationalTest extends AbstractTestCase
/**
* @return array
*/
public function providerParseInvalidString()
public function providerOfInvalidString()
{
return [
['123/-456'],