-
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
Squiz.Commenting.InlineComment will fail to fix comments at the end of the file #1645
Squiz.Commenting.InlineComment will fail to fix comments at the end of the file #1645
Conversation
I came across this issue when running `phpcbf` with the complete `WordPress` standard against the WPCS unit test files in an attempt to find fixer conflicts. When an inline comment is the last non-whitespace content in a file, the sniff *will* report an error, but will always fail to fix it as the value of `$next` (formely on line 295) would be `false`. The file would get the "FAILED TO FIX" status as there is still one "fixable" error remaining, even though the file is correctly overwritten by the fixed file. As whether or not there should be a blank line at the end of a file is another matter and is covered by the `Generic.Files.EndFileNewline` / `Generic.Files.EndFileNoNewLine` sniffs, this specific case should IMHO not be handled by this sniff anyway. The fix I'm proposing is to exit out of the sniff if the inline comment being examined is the last non-whitespace token in the file. Includes unit tests demonstrating the issue.
@gsherwood Could this small fix please be tagged for the 3.1.1 release ? 🙏 |
I don't get a conflict between the InlineComment sniff and the EndFileNewline sniffs. I do get two errors that can't both be fixed, but the fact that Here is a sample file:
And a test command:
The fixer for InlineComment does stuff like this:
Are you sure it is this specific combination of sniffs causing your problem? |
True, the file gets fixed correctly, but the status it gets is: "Failed to fix" as after fixing there's one (un-)fixable error left. The fix in the branch makes sure that the error is ignored completely if it's the last non-whitespace token in the file.
Yes, I'm sure. It's not even a combination of sniffs, it's just this single sniff causing the issue. To reproduce: phpcbf -p -s ./path/to/InlineCommentUnitTest.inc --standard=Squiz --sniffs=Squiz.Commenting.InlineComment The output will be:
With the fix in this branch applied, the output will be:
|
Just had another look at the fixed file and I'm surprised that line 44 (original file) / line 40 (fixed file) does not give an error anymore, so will have a look at that as well. Same goes for line 97 / 91 and line 103 / 96. |
Ok, I've added two more commits:
And yes, those two should probably have their own PRs, but as they would conflict with this PR it made sense to add them here. Let me know if you want me to split the PR anyhow. |
7d67684
to
f3b069c
Compare
… not being thrown If two subsequent lines contained comments with code between them, they would be seen as part of the same comment block, while in reality there are not. This meant that errors for `NotCapital` and `InvalidEndChar` where not always being thrown when they should be. Includes additional unit tests demonstrating the issue.
f3b069c
to
97bbb19
Compare
Sorry, I misunderstood the original issue, but was able to replicate it. Thanks a lot for the fix. |
Thanks @gsherwood ! You may want to also add a changelog entry for the secondary fix I made to the sniff (third commit)
|
Good idea :) Thanks. |
@gsherwood Now that this fix is in, the road is clear to add checking for fixer conflicts to the Travis builds. Would you be interested in me pulling a similar PR here ? It would do a basic test for each standard (excluding For efficiency, I'd suggest only running this check once per build using an environment variable. What do you think ? |
I think that would be really interesting and would love to see it. So... yes please :)
The reason I run PHPCS over itself on all environments is basically as an integration test for each PHP version - not just to confirm I am meeting the coding standards. I'd probably do the same thing for these new integration tests for checking the standards themselves unless they are really slow. I should probably be doing a PEAR install and checking that as well... |
I'll prepare a PR for you to pursue.
Fair enough. As these checks will have to run for each standard separately, it will add significantly to the build time if these are run on every build.
For that matter, I can also include a (one build only as that really is sufficient) couple of commands which validate the XML rulesets and check the code style of them. |
Hmm... an initial run is already throwing up quite some issues... both fixer conflicts as well as hard bugs in sniffs ( Those will all need to be fixed before I can pull this PR, so that will take a little while before we are through that. Other findings:
I'm doing a run now with You can see the results of the various builds here: https://travis-ci.org/jrfnl/PHP_CodeSniffer/branches - branch: |
I came across this issue when running
phpcbf
with the completeWordPress
standard against the WPCS unit test files in an attempt to find fixer conflicts.When an inline comment is the last non-whitespace content in a file, the sniff will report an error, but will always fail to fix it as the value of
$next
(formely on line 295) would befalse
.The file would get the "FAILED TO FIX" status as there is still one "fixable" error remaining, even though the file is correctly overwritten by the fixed file.
As whether or not there should be a blank line at the end of a file is another matter and is covered by the
Generic.Files.EndFileNewline
/Generic.Files.EndFileNoNewLine
sniffs, this specific case should IMHO not be handled by this sniff anyway.The fix I'm proposing is to exit out of the sniff if the inline comment being examined is the last non-whitespace token in the file.
Includes unit tests demonstrating the issue.