diff --git a/src/Exception/CurrencyMismatchException.php b/src/Exception/CurrencyMismatchException.php new file mode 100644 index 00000000..e05ce053 --- /dev/null +++ b/src/Exception/CurrencyMismatchException.php @@ -0,0 +1,9 @@ +currency != $other->currency) { - throw new InvalidArgumentException('Currencies must be identical'); + throw InvalidArgumentException::currencyMismatch(); } // @phpstan-ignore impure.staticPropertyAccess, possiblyImpure.methodCall @@ -209,7 +209,7 @@ public function add(Money ...$addends): Money foreach ($addends as $addend) { // Note: non-strict equality is intentional here, since `Currency` is `final` and reliable. if ($this->currency != $addend->currency) { - throw new InvalidArgumentException('Currencies must be identical'); + throw InvalidArgumentException::currencyMismatch(); } // @phpstan-ignore impure.staticPropertyAccess, possiblyImpure.methodCall @@ -232,7 +232,7 @@ public function subtract(Money ...$subtrahends): Money foreach ($subtrahends as $subtrahend) { // Note: non-strict equality is intentional here, since `Currency` is `final` and reliable. if ($this->currency != $subtrahend->currency) { - throw new InvalidArgumentException('Currencies must be identical'); + throw InvalidArgumentException::currencyMismatch(); } // @phpstan-ignore impure.staticPropertyAccess, possiblyImpure.methodCall @@ -291,7 +291,7 @@ public function mod(Money|int|string $divisor): Money if ($divisor instanceof self) { // Note: non-strict equality is intentional here, since `Currency` is `final` and reliable. if ($this->currency != $divisor->currency) { - throw new InvalidArgumentException('Currencies must be identical'); + throw InvalidArgumentException::currencyMismatch(); } $divisor = $divisor->amount; @@ -385,7 +385,7 @@ public function ratioOf(Money $money): string // Note: non-strict equality is intentional here, since `Currency` is `final` and reliable. if ($this->currency != $money->currency) { - throw new InvalidArgumentException('Currencies must be identical'); + throw InvalidArgumentException::currencyMismatch(); } return self::$calculator::divide($this->amount, $money->amount); diff --git a/tests/MoneyTest.php b/tests/MoneyTest.php index a7317f9c..0f0fb2de 100644 --- a/tests/MoneyTest.php +++ b/tests/MoneyTest.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use Money\Currency; +use Money\Exception\CurrencyMismatchException; use Money\Money; use PHPUnit\Framework\TestCase; @@ -294,7 +295,7 @@ public function itThrowsWhenCalculatingModulusOfDifferentCurrencies(): void $money = new Money(self::AMOUNT, new Currency(self::CURRENCY)); $rightMoney = new Money(self::OTHER_AMOUNT, new Currency(self::OTHER_CURRENCY)); - $this->expectException(InvalidArgumentException::class); + $this->expectException(CurrencyMismatchException::class); $money->mod($rightMoney); } @@ -442,7 +443,7 @@ public function itThrowsWhenComparingDifferentCurrencies(): void { $money = new Money('5', new Currency(self::CURRENCY)); - $this->expectException(InvalidArgumentException::class); + $this->expectException(CurrencyMismatchException::class); $money->compare(new Money('5', new Currency('SOME'))); }