rectorphp / type-perfect

Next level type declaration check PHPStan rules
https://getrector.com/blog/introducing-type-perfect-for-extra-safety
MIT License
74 stars 4 forks source link

Contradiction with phpstan rules #17

Closed GErpeldinger closed 4 months ago

GErpeldinger commented 4 months ago

Hello,

I'm reaching out because I'm facing a case where PHPStan and this set of rules contradict each other. It's one of the default rules, specifically the rule for instanceof.

I have a situation where a class is set only if a condition is met. Previously, I used isset() for this with no issues, but now I'm being recommended to use instanceof, which I completely agree with. However, when I use instanceof without isset, PHPStan tells me that the variable might be undefined. So, I added isset before instanceof, but then type-perfect says to use instanceof instead of isset.

Here is an example where PHPStan says the variable might not be defined: https://phpstan.org/r/9486e811-bc55-467f-a487-03a858286fdf

edit: After some testing it seems that phpstan is right and we need to verify with isset() that the variable exist

Capture d’écran 2024-06-17 à 16 26 20
samsonasik commented 4 months ago

That should already resolved in latest dev-main, see

and will be included in next release

GErpeldinger commented 4 months ago

Cool, thanks !