Skip to content
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

Assume functions are impure by default #7228

Closed
spawnia opened this issue May 13, 2022 · 3 comments
Closed

Assume functions are impure by default #7228

spawnia opened this issue May 13, 2022 · 3 comments

Comments

@spawnia
Copy link

spawnia commented May 13, 2022

Feature request

It is cool that PHPStan tries to remember the result of function calls, but I think it is overly eager in assuming that functions are pure by default. Multiple issues have reported problems with this:

Here is another example that illustrates a problem I was having with this analysis: https://phpstan.org/r/1516b762-efa0-4bdd-94c0-d9b4fa04a077

I have read https://phpstan.org/blog/remembering-and-forgetting-returned-values and found that PHPStan does offer a way to mark functions as impure with @phpstan-impure, but that might not always be applicable. For example, library code can not be annotated in this way but could still very well be impure.

I think that the default should be switched, functions should be assumed to be impure by default. Apart from purely functional languages, I don't that any other language or typechecker assumes purity by default. This will reduce the number of false positives.

Did PHPStan help you today? Did it make you happy in any way?

It allowed me to refactor some legacy code boldly with confidence.

@ondrejmirtes
Copy link
Member

For example, library code can not be annotated in this way

It can: https://phpstan.org/user-guide/stub-files

This has been suggested but rejected before. I believe the current way is the best of both worlds and is the least annoying. The majority of use-cases (getters) are behaving correctly.

@spawnia
Copy link
Author

spawnia commented May 13, 2022

I believe getters are the worst of all worlds and are the most annoying 😉

In all seriousness, the result of multiple calls to a pure function can (and should) be stored in a variable to avoid repeating work. It is great if PHPStan kicks in at that point and finds redundant comparisons. Same with explicitly pure function calls. Writing good code would then reward users with a better analysis.

If multiple function calls are actually necessary, I find it annoying that I have to do extra work to prove that my code is correct.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants