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

Support @immutable phpdoc #1335

Merged
merged 1 commit into from
May 20, 2022

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented May 20, 2022

Closes phpstan/phpstan#4600

See also https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-immutable

first draft, not sure about some edge cases yet. e.g. inheritance. most likely more tests are needed in a dedicated file maybe

does also not support traits because it looks like traits are currently not fully working with native or phpdoc readonly (e.g. readonly property in trait, missing assignment in class that uses that trait). maybe a good follow-up / separate feature/fix? I created phpstan/phpstan#7271

I'm also still not sure if this should be also settable via @readonly on class-level maybe too?

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@voku
Copy link
Contributor

voku commented May 20, 2022

Maybe you can cherry-pick 🍒 some test from this solution? https://github.com/svnldwg/phpstan-rules/

@ondrejmirtes
Copy link
Member

Really really nice! Feel free to mark it as ready to be merged :)

About readonly properties and traits - yeah, it's a mystery, I have no idea why it would be broken. There are already some issue about it: phpstan/phpstan#7219, phpstan/phpstan#7198

Would be great to solve that in a follow-up :)

@herndlm
Copy link
Contributor Author

herndlm commented May 20, 2022

Ok then, will mark it as ready since I think this is already adding value like that. I'll try to look into some of the more potentially headache-inducing edge/trait cases in follow-ups.

@herndlm herndlm marked this pull request as ready for review May 20, 2022 20:53
@ondrejmirtes ondrejmirtes merged commit 622a84b into phpstan:1.7.x May 20, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@herndlm herndlm deleted the support-immutable-phpdoc branch May 20, 2022 22:06
@herndlm
Copy link
Contributor Author

herndlm commented May 21, 2022

What do you think of people using native readonly and combine it with the @immutable phpdoc on the class? That would not be needed anymore, but in case of psalm users this could lead to annoying "duplicate" errors, I guess? E.g. https://phpstan.org/r/d04d798b-c3c1-4756-8494-2de4aad35537

@ondrejmirtes
Copy link
Member

It's fine I guess. How we modeled it it means those are two separate errors which is correct. Imagine that people will want to ignore one but not the other - the current model supports that.

Also once you're on 8.1, it's easy to upgrade to 8.2 at which point you can use the native readonly class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Immutability through a docblock annotation or an attribute
3 participants