-
-
Notifications
You must be signed in to change notification settings - Fork 947
FN: impure behaviour in explicitly pure functions not reported as an error #4426
Copy link
Copy link
Closed
Labels
Description
Bug report
A method could be mistakenly marked as pure (or intentionally, with the expectation that it would constify it, a la C++).
In such cases, impure behaviour such as:
- calling an impure function (or method)
- assigning a field
- assigning a global variable
- (maybe) assigning to a reference?
should be considered an error when performed inside a pure function.
Code snippet that reproduces the problem
https://phpstan.org/r/f87df79e-9bd6-45bf-a9be-ef4e1cd8a84d
https://3v4l.org/clKaY
Expected output
An error should be reported on line 13, because a pure method is calling an impure method.
Side notes
I guess there will have to be a differentiation between assumed purity vs explicit purity for this issue to be solved without being super annoying. It would be nice to have an optional rule to check for violation of assumed purity and emit an error to tell the user to mark such methods explicitly with @phpstan-impure for more correct analysis.
Reactions are currently unavailable