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

[Feature] Add 3 new rectors for fixing incorrect null type in @var, @param and @return #2049

Conversation

dorrogeray
Copy link
Contributor

Hi, here are 3 rectors which I wanted to share. I have added them to the TypeDeclaration directory but if there is a better place please let me know.

  • ParamAnnotationIncorrectNullableRector
    • Adds or removes null type from @param phpdoc typehint based on php parameter type declaration
  • ReturnAnnotationIncorrectNullableRector
    • Adds or removes null type from @return phpdoc typehint based on php return type declaration
  • VarAnnotationIncorrectNullableRector
    • Adds or removes null type from @var phpdoc typehint based on php property type declaration

Some background info: Basically we had a lot of these problems in our project where the phpdocs were incorrect, ie the php type was nullable, for example ?array but the php doc said it was simply SomeObject[], not SomeObject[]|null as it should. Or vice versa, the phpdoc stated it was SomeObject[]|null but it was actually non nullable array type. Among other things, this was then confusing phpstan and leading to all sorts of problems. These 3 rector rules solved this problem for us.

@samsonasik
Copy link
Member

@dorrogeray btw, could you extract into 3 PRs for the 3 rules?

@dorrogeray
Copy link
Contributor Author

@samsonasik Thanks, I have removed the UnionTypeAnalyzer and used the TypeCombinator instead. I have also sent separate PR for the first rule VarAnnotationIncorrectNullableRector

I will send the next separate PR once this one is merged, so that I can rebase and prevent conflicts (rules share some code and docs need to be regenerated).

@dorrogeray
Copy link
Contributor Author

All three rules have been merged, so I am closing this PR. Thanks! 👍

@dorrogeray dorrogeray closed this Apr 14, 2022
@TomasVotruba
Copy link
Member

Thank you for your work @dorrogeray 👏

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