-
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
Autofixer for Squiz.WhiteSpace.FunctionSpacing goes into "infinite" loop for method defined on the same line as a class #3904
Comments
@Daimona Thanks for reporting this issue. I have been able to reproduce this with the provided information and can confirm the bug.
I'm hoping the issue can be fixed in the sniff.
But I don't understand this remark, I mean: this is exactly what PHPCS is doing: reporting the issue as unfixable (by checking 50 loops and bowing out if there is still a fixer conflict). |
Oh and I'm also wondering why the sniff does not report the |
Actually, looks like the sniff doesn't care about code before or after the function. Just about the blank lines above/below the code, which explains why the |
Ah, sorry, I should have stated this more clearly. What I meant is: if this is a hard-to-fix edge case, then an autofix should not be offered at all, instead of offering it but then having it fail. The main reason why I mentioned this is that I observed a severe performance degradation, and while I haven't fully investigated it, I suspect it might have been caused by this autofixer. For the small code snippet I used as an example here it doesn't really make any difference, but when testing a whole standard on this file, phpcbf takes ~200 ms without the Squiz.WhiteSpace.FunctionSpacing enabled, and ~1.5 s with that sniff enabled. As I said, I haven't looked into why the failed autofix has such a severe impact, but it does seem to be the cause.
I can confirm that the PR fixes this bug (as the violation is no longer reported), thank you! |
I see what you mean and yes, I agree. The problem is finding those edge-cases. Code can be written in so many different ways. Since I started contributing to this project, I've learned to write incredible convoluted code for test cases, but even though my fantasy goes far, I often still miss some edge cases I just didn't think off. Writing good sniffs is hard. Please do keep reporting these kind of things as it really helps to make sniffs better.
You might be interested in doing some testing with the Performance report (open PR #3810), that should help in identifying which sniffs are particularly slow. I already identified a couple of bottlenecks, in part via that report. There is now also a "Performance" tag with which those type of issues are labeled. |
As someone who wrote a few sniffs and has worked on static analyzers, I wholeheartedly agree! In fact, I discovered this bug as part of an exploration I did to try and come up with an absolutely horrendous piece of code :-)
Thanks, I'll definitely look into that! |
FYI: the fix for this issue 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). |
Describe the bug
In the (intentionally bad) snippet of code below, I have a method defined on the same line as its class. When running PHPCS on it with the
Squiz.WhiteSpace.FunctionSpacing
sniff enabled (ruleset below), it reports that 1 blank line is expected before the method. However, if I then try to autofix that with phpcbf, it tries to add the line in the wrong place, thus going into an infinite loop because it detects that the issue still hasn't been fixed; it only aborts upon reaching the limit of 50 passes.Code sample
Custom ruleset
To reproduce
Steps to reproduce the behavior:
test.php
with the code sample abovephpcs test.php
phpcbf -vvv test.php
Expected behavior
Ideally, the issue should be fixed. Or if that's hard to do, at least PHPCS should not go into an endless loop, and instead report the issue as unfixable.
Versions (please complete the following information)
Please confirm:
master
branch of PHP_CodeSniffer.The text was updated successfully, but these errors were encountered: