Handle side effects of constructor calls #1205
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes this original problem: https://phpstan.org/r/9800c2a7-080a-4d22-aeb9-802cd3cd8ce3 - if you pass a property into constructor, you do not know, what happens with it there and of course the state can be changed as in any other method where you pass an object. Marking the constructor as impure does not help: https://phpstan.org/r/e942d9a8-c20a-4b7d-973a-6bca67b5d69b .
Since you cannot return a value from a constructor, all constructors should be impure by the definition from https://phpstan.org/blog/remembering-and-forgetting-returned-values:
Question is whether that is practical default in regards to the code which calls the constructors - probably most of them do not change passed arguments.
There is a possibility to invalidate only with explicit impure annotation - but then this would behave differently than all of the other cases.