-
-
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?
Conversation
some shortcuts were taken - for example, for the places where \NumberFormatter is used, @psalm-suppress annotations was required also, unfortunately, Currency::of() is using ISOCurrencyProvider which is heavily impure internally, therefore all static methods using Currency::of() were not marked as pure.
The From the Psalm's point of view it would be ideal to force the client of the library to call the provider explicitly (basically, treating the provider as a sort of a repository), and/or the other classes should not accept currency codes and automatically transform them into |
Before you merge it, what do you think about this problem:
|
I guess marking |
I think yes, since the source data used by the I will add |
@@ -769,6 +789,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 comment
The 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 NumberFormatter
instance twice but configured differently, and the method would return different results?
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 bad news, because formatWith()
will prevent Money
from being a @psalm-immutable
object otherwise :(
/** | ||
* @psalm-suppress ImpureMethodCall | ||
* FIXME: remove the suppression after BigInteger::gcdMultiple() is marked @psalm-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.
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
The motivation of this PR is the inability to use the value objects this lib provides in projects which use Psalm as a static analyzer, in its "immutable" contexts.
Added
@psalm-immutable
and@psalm-pure
annotations to appropriate classes and static methods.I took some shortcuts: for example, for the places where \NumberFormatter is used,
psalm-suppress
annotations was required.Also, unfortunately,
Currency::of()
is usingISOCurrencyProvider
which is heavily impure internally, therefore all static methods usingCurrency::of()
were not marked as pure. Even more unfortunate is that one of theMoney
's non-static method is usingCurrency::of()
and this place had to be marked with a@psalm-suppress
. I am not sure what to do about this.