Add rule checking coalesces by leongersen · Pull Request #36 · phpstan/phpstan-src
In a similar vein to #25, this adds a rule checking that the left side of a coalesce statement is actually valid.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, the rule isn't added to any level. Should be level 1.
@ondrejmirtes, @BackEndTea I think I've addressed your comments on this PR, let me know if there's anything else you'd like me to improve. Thanks for your reviews so far!
How does this behave when the left side is a function call?
e.g.
I'd expect an error here, as rand cant return null.
Also, how does this behave with 'always null'? e.g.
function (?string $a) : void { if(!is_string($a) { $a ?? = 'foo'; } }
This always triggers are $a is always null here.
@BackEndTea Good points. I've added support for FuncCall, as well as PropertyFetch and StaticPropertyFetch. I've also added checks for "type is always null" everywhere.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Looks pretty good to me, it even found a fault in the phpstan source code
Hi, I just merged it as: 3608ded
I made some mostly cosmetic changes to the error messages, and cover more cases. The core logic is really solid and I appreciate it, it couldn't be easy to come up with it! :)
I have more work to be done I will cover myself over this weekend:
- Reuse the same logic in existing VariableCertaintyInIssetRule.
- Report variables on level 1 and the rest on level 4. Currently all of this is reported on level 1.
- Create a new rule that reports the same things for
isset()on level 4.
This was referenced
Feb 29, 2020This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters