-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Make InnerFunctionsSniff detect functions inside closures #3807
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I agree this is a good change, but looking at this sniff now, I see more things wrong with it. @Daimona Would you be willing to make some additional fixes ? Additional things which I believe should be addressed:
Does that make sense ? |
Makes sense to me! In fact, I was a bit puzzled by that check given the existence of I agree with the rest as well. I've updated my PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making these updates @Daimona !
Looking good.
I wonder if the two loops walking the $conditions
array can be combined ?
I'm thinking that logic like this may work (very much pseudo-code):
// Reverse the conditions array
foreach () {
if ($condition === T_FUNCTION || $condition === T_CLOSURE) {
break; // Error.
}
if (isset(Tokens::$ooScopeToken[$condition])) {
return; // Found OO structure before function/closure, so this is an OO method.
}
}
Extra code sample you may want to add to the tests (would currently not be detected/false negative): function foo() {
if (class_exists('Bar') === false) {
class Bar {
function foo() {
function innerFunction() {} // Error.
}
}
}
} |
I wanted to use separate loops so that we can bail out earlier if it's not a nested function. However, on second though that's simply untrue -- we'd loop through the whole array of conditions in the common case without nested functions. And I guess reversing the array of conditions is not terribly expensive, either, so I'm going to merge the loops, and make all the other suggested changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
Thanks for working with me to get this sniff into shape!
@Daimona I'm going to merge this PR. The changelog normally contains the full name of the contributor. Would you mind sharing yours ? |
Hi :) I don't use my real name online; at least not in connection with this username. Would it be possible to just credit me as "Daimona"? |
Absolutely! Just wanted to give you the chance, but only if you want it. |
FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release. As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo). |
Fixes #3806