-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using Money, Currency and Context in Psalm's immutable contexts #75
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
/** | ||
* Adjusts a rational number to a decimal amount. | ||
* | ||
* @psalm-immutable | ||
*/ | ||
interface Context | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ | |
* - CashContext is similar to DefaultContext, but supports a cash rounding step. | ||
* - CustomContext handles monies with a custom scale, and optionally step. | ||
* - AutoContext automatically adjusts the scale of the money to the minimum required. | ||
* | ||
* @psalm-immutable | ||
*/ | ||
final class Money extends AbstractMoney | ||
{ | ||
|
@@ -71,6 +73,8 @@ private function __construct(BigDecimal $amount, Currency $currency, Context $co | |
* @return Money | ||
* | ||
* @throws MoneyMismatchException If all the monies are not in the same currency. | ||
* | ||
* @psalm-pure | ||
*/ | ||
public static function min(Money $money, Money ...$monies) : Money | ||
{ | ||
|
@@ -96,6 +100,8 @@ public static function min(Money $money, Money ...$monies) : Money | |
* @return Money | ||
* | ||
* @throws MoneyMismatchException If all the monies are not in the same currency. | ||
* | ||
* @psalm-pure | ||
*/ | ||
public static function max(Money $money, Money ...$monies) : Money | ||
{ | ||
|
@@ -121,6 +127,8 @@ public static function max(Money $money, Money ...$monies) : Money | |
* @return Money | ||
* | ||
* @throws MoneyMismatchException If all the monies are not in the same currency and context. | ||
* | ||
* @psalm-pure | ||
*/ | ||
public static function total(Money $money, Money ...$monies) : Money | ||
{ | ||
|
@@ -146,6 +154,8 @@ public static function total(Money $money, Money ...$monies) : Money | |
* @return Money | ||
* | ||
* @throws RoundingNecessaryException If RoundingMode::UNNECESSARY is used but rounding is necessary. | ||
* | ||
* @psalm-pure | ||
*/ | ||
public static function create(BigNumber $amount, Currency $currency, Context $context, int $roundingMode = RoundingMode::UNNECESSARY) : Money | ||
{ | ||
|
@@ -641,6 +651,10 @@ private function gcdOfMultipleInt(array $values): int | |
{ | ||
$values = array_map(fn (int $value) => BigInteger::of($value), $values); | ||
|
||
/** | ||
* @psalm-suppress ImpureMethodCall | ||
* FIXME: remove the suppression after BigInteger::gcdMultiple() is marked @psalm-pure | ||
*/ | ||
return BigInteger::gcdMultiple(...$values)->toInt(); | ||
} | ||
|
||
|
@@ -745,6 +759,10 @@ public function convertedTo( | |
int $roundingMode = RoundingMode::UNNECESSARY, | ||
) : Money { | ||
if (! $currency instanceof Currency) { | ||
/** | ||
* FIXME: dunno what to do here, Currency::of() uses ISOCurrencyProvider which is heavily "impure". | ||
* @psalm-suppress ImpureMethodCall | ||
*/ | ||
$currency = Currency::of($currency); | ||
} | ||
|
||
|
@@ -769,6 +787,7 @@ public function convertedTo( | |
*/ | ||
public function formatWith(\NumberFormatter $formatter) : string | ||
{ | ||
/** @psalm-suppress ImpureMethodCall */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to suppress this? As I understand it, this method is not pure as you could pass the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is bad news, because |
||
return $formatter->formatCurrency( | ||
$this->amount->toFloat(), | ||
$this->currency->getCurrencyCode() | ||
|
@@ -785,6 +804,9 @@ public function formatWith(\NumberFormatter $formatter) : string | |
* @param bool $allowWholeNumber Whether to allow formatting as a whole number if the amount has no fraction. | ||
* | ||
* @return string | ||
* | ||
* @psalm-suppress ImpureMethodCall - \NumberFormatter is impure, but we use it here in a side effect free way | ||
* @psalm-suppress ImpureStaticVariable - static variables are used for optimization reasons | ||
*/ | ||
public function formatTo(string $locale, bool $allowWholeNumber = false) : string | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What prevents
BigInteger::gcdMultiple()
from being marked as pure?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is another library, hence out of scope of this PR