Skip to content

Commit

Permalink
Allow divisor in mod to be a number as well
Browse files Browse the repository at this point in the history
Fixes #687
  • Loading branch information
helhum authored and frederikbosch committed Nov 22, 2023
1 parent 6fcdcaf commit d77d552
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,22 @@ public function divide(int|string $divisor, int $roundingMode = self::ROUND_HALF
* the remainder after dividing the value by
* the given factor.
*/
public function mod(Money $divisor): Money
public function mod(Money|int|string $divisor): Money
{
// 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');
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');
}
$amount = $divisor->amount;
} else {
if (is_int($divisor)) {
$divisor = (string) $divisor;
}
$amount = $divisor;
}

return new self(self::$calculator::mod($this->amount, $divisor->amount), $this->currency);
return new self(self::$calculator::mod($this->amount, $amount), $this->currency);

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build lowest version

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Psalm

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build (8.0)

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build (8.1)

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build (8.2)

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Psalm

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build (8.0)

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build lowest version

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)

Check failure on line 290 in src/Money.php

View workflow job for this annotation

GitHub Actions / Build (8.1)

ArgumentTypeCoercion

src/Money.php:290:63: ArgumentTypeCoercion: Argument 2 of Money\Calculator::mod expects numeric-string, but parent type string provided (see https://psalm.dev/193)
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ public function itCalculatesTheModulusOfAnAmount($left, $right, $expected): void
self::assertEquals($expected, $money->getAmount());
}

/**
* @psalm-param positive-int $left
* @psalm-param positive-int $right
* @psalm-param numeric-string $expected
*
* @dataProvider modExamples
* @test
*/
public function itCalculatesTheModulusOfNumber($left, $right, $expected): void
{
$money = new Money($left, new Currency(self::CURRENCY));

$money = $money->mod($right);

self::assertInstanceOf(Money::class, $money);
self::assertEquals($expected, $money->getAmount());
}

/**
* @test
*/
Expand Down

0 comments on commit d77d552

Please sign in to comment.