You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Verify that function body really does what its @param-out says.
What all of these have in common is that you need to construct a graph of nodes with relations between them - where values are written and when they are read. Psalm has some of these features and this article (https://psalm.dev/articles/better-unused-variable-detection) talks about it.
I've kindly asked @arnaud-lb to work on this and right now it's in a form of this package: https://github.com/arnaud-lb/php-sema which should somehow be adopted in PHPStan. Not sure if we can use it directly, most likely we'll adapt the algorithms and apply them here.
The priority for me is to get rid of the false positives about readonly properties. At the same time we should be able to detect more situations where readonly property assignment is missing or there is a risk of multiple assignments.
After that is all figured out and released, we can work on the dead variable rules etc. :)
What's unclear to me is how this graph should be represented by PHPStan and how rules should ask about it. Should we have a graph of virtual nodes like VariableRead, VariableWrite and construct a graph of those, or should we connect the nodes already present in the AST? Lots of questions and lots of research that needs to be done.
Being able to detect when the readonly property is really assigned multiple times (like inside a while loop) - currently not reported
Being able to detect when the readonly property is only assigned in an if but not else - currently not reported
By taking advantage of what Scope already does well - tracking variable types. I've assigned a "virtual expr node" PropertyInitializationExpr phpstan/phpstan-src@ce3c164 and Scope is able to tell me when a property is always/sometimes/never initialized.
The same approach could be also for example used for:
Tracking if parent constructor is always called - there's a rule for that in phpstan-strict-rules
We could also track when a variable is rewritten with a completely different value which would help us to solve the rest of the items mentioned here.
We might not have to do the complex control flow graph feature at all :)
Activity
ondrejmirtes commentedon Oct 21, 2022
What's unclear to me is how this graph should be represented by PHPStan and how rules should ask about it. Should we have a graph of virtual nodes like
VariableRead,VariableWriteand construct a graph of those, or should we connect the nodes already present in the AST? Lots of questions and lots of research that needs to be done.ondrejmirtes commentedon Jul 6, 2023
I was already able to solve a few of these items:
ifbut notelse- currently not reportedBy taking advantage of what Scope already does well - tracking variable types. I've assigned a "virtual expr node" PropertyInitializationExpr phpstan/phpstan-src@ce3c164 and Scope is able to tell me when a property is always/sometimes/never initialized.
The same approach could be also for example used for:
We might not have to do the complex control flow graph feature at all :)
AlexKratky commentedon Oct 10, 2023
Please try to improve unused variable like so slevomat/coding-standard#1633