Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 3258656

Browse files
author
Andreu Correa Casablanca
committed
Use more specific exceptions for infinite and NaN inputs
1 parent 162e5e2 commit 3258656

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Decimal.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Litipk\BigNumbers\DecimalConstants as DecimalConstants;
66

7+
use Litipk\BigNumbers\Errors\InfiniteInputError;
8+
use Litipk\BigNumbers\Errors\NaNInputError;
79
use Litipk\BigNumbers\Errors\NotImplementedError;
810
use Litipk\Exceptions\InvalidArgumentTypeException as InvalidArgumentTypeException;
911

@@ -80,11 +82,9 @@ public static function fromFloat(float $fltValue, int $scale = null, bool $remov
8082
self::paramsValidation($fltValue, $scale);
8183

8284
if (\is_infinite($fltValue)) {
83-
throw new \InvalidArgumentException('fltValue must be a finite number');
85+
throw new InfiniteInputError('fltValue must be a finite number');
8486
} elseif (\is_nan($fltValue)) {
85-
throw new \DomainException(
86-
"To ensure consistency, this class doesn't handle NaN objects."
87-
);
87+
throw new NaNInputError("To ensure consistency, this class doesn't handle NaN objects.");
8888
}
8989

9090
$defaultScale = 16;

src/Errors/InfiniteInputError.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Litipk\BigNumbers\Errors;
4+
use DomainException;
5+
6+
class InfiniteInputError extends DomainException implements BigNumbersError
7+
{
8+
public function __construct(string $message = 'Infinite values are not supported')
9+
{
10+
parent::__construct($message, 0, null);
11+
}
12+
}

src/Errors/NaNInputError.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Litipk\BigNumbers\Errors;
4+
use DomainException;
5+
6+
class NaNInputError extends DomainException implements BigNumbersError
7+
{
8+
public function __construct(string $message = 'NaN values are not supported')
9+
{
10+
parent::__construct($message, 0, null);
11+
}
12+
}

0 commit comments

Comments
 (0)