-
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
Errors for lines containing error suppression comments are always suppressed #1826
Comments
To clarify, this isn't new line ignore behaviour - these comments have always suppressed the line they are on and the following line. The changed behaviour is that if the comment is on a line with other code, it will only ignore that one line and not the following one. And the comments have their own tokens, so you can now listen for them more easily, but you could do this sort of checking with the old syntax as well if you really wanted to. I just wanted to make that clear as there isn't an older behaviour that can be reverted back to, and the behaviour is intentional. I don't think there are many options here. Those comments must have all errors suppressed so commenting sniffs (or anything really) don't start throwing errors for PHPCS instructions. Yes, specific sniffs can now detect those special tokens and exclude errors manually, but that's a lot of work by a lot of sniffs and a big BC break. Plus, PHPCS would need to decide on situations like if a There are two workarounds, but neither are very good.
The second option is better, if you can live with developers not seeing errors until build time. Both could also be done - an annoying error on the wrong line during live coding and then a build step to ensure all checks are done properly. Thinking about it a bit more, a build step would probably be a good idea anyway if you want to ensure a developer hasn't used annotations to ignore your annotation checks (like with ignoreFile). |
I realize the behaviour is not new, but in certain cases - like for sniffs regarding superfluous whitespace, type of whitespace or indentation -, I would call it undesired. For me, the big difference between the "old style" comments and the new style is their flexibility and ability to document. I rarely used the old style comments and actively discouraged people from using them as they would generally ignore way too much - all sniffs, while most of the time only one sniff needed to be ignored -. The new style comments with their modular ability to ignore/disable very select sniffs or even error codes and the ability to document why, however are completely different and I will actively favour them over exclusions from the ruleset now. This in practice - at least for me - means that there will be a lot more suppression comments in code, which makes it all the more relevant for them to:
That's aside from any specific requirements which a project may or may not have for those comments as described above. A secondary build could work, but that would mean that all other code would have to comply 100% with the sniffs which would be included as otherwise the build would fail - so things like Alternatively, a separate sniff would need to be written to check just the Hmm... not sure what to do now... I'm going to sleep on this one. |
Quick question - just checking -, when |
Looks like it, as an optimisation. If that was the only thing stopping a solution, they could still be tokenized without taking effect. |
I think, for consistency and predictability, it would be a good idea to tokenize files the same whether I noticed another issue with the tokenization of the new annotations while working on making the sniffs compatible - when used in a docblock and using the new Example code to demonstrate: <?php
/**
* Something
*
* @param string $abc
* @return bool
*
* @phpcs:ignore Standard.Category.Sniff -- for reasons.
* phpcs:disable Standard.Category.Sniff -- for reasons.
* @phpcs:ignoreFile Standard.Category.Sniff
*/
function abc( $abc ) {
do_something();
} Tokenizes as:
A further inconsistency there is that when used in a As a side-note: I'm currently presuming that all docblock related sniffs use the |
☝️ the tokenizer inconsistency with docblocks in combination with |
Found yet another issue. If a PHPCS annotation token is part of a changeset, for instance for fixing indentation of arrays or docblocks , the line containing the PHPCS annotation will not be fixed. Now this behaviour is properly not new, but like I said before, I expect these annotations to be used a lot more than the old style annotations, so I expect that you'll start to see more bug reports about this soon enough.
[Edit]: it might be better to not ignore errors for the whole line when the annotation is the only non-whitespace token on the line, but just to ignore errors for the actual PHPCS token. |
@gsherwood Just checking - have you had a chance to read through the above three comments and formed an opinion on this ? The issue with docblocks and Clarification about the direction you will be taking regarding the inconsistencies will help me to update/fix the last four sniffs which I know are having trouble with the new PHPCS annotations. |
Sorry, no. Probably another 24 hours before I can get back to PHPCS work. |
#1826) The comment was being treated as a tag, and any sniffs code or -- comment after it was being tokenized into diffenerent tokens. This meant that the main tokenizer never saw that a disable/enable command was applying to a specific sniff code.
I've fixed that one. Thanks a lot for finding that before it got released. I'll take a look at your other comments now. |
That's pretty tricky. That comment is entirely ignored to ensure it doesn't trigger things like commenting sniffs that try to reformat it, or any other custom sniffs really. If I just let PHPCS start reporting fixable errors during fixing, the comment will be changed in ways that PHPCS never complained about, which could be a fairly serious problem.
I can probably do that without breaking too much, but it wont help indentation issues because the error is reported on the comment token itself, so it will still be ignored. A sniff is still able to change that comment token during fixing. It just isn't able to kick off fixing code if it reports an error on the line the comment is on. So if a sniff went through and fixed the indentation of an entire IF statement because the indent of the IF keyword was wrong, it will still fix the comment indent. For example: <?php
if ($foo) {
if ($bar) {
//phpcs:ignore Foo -- fake code to ensure closing brace is fix
}
} Gets fixed as: <?php
if ($foo) {
if ($bar) {
//phpcs:ignore Foo -- fake code to ensure closing brace is fix
}
} So it's not like PHPCS doesn't allow those tokens to be fixed. It's just that it never reports errors with those lines and so PHPCBF shouldn't be implementing fixes for those hidden errors. I'm not sure how to change that behaviour without breaking things. |
Just revisiting this as it's stuck at the moment. I still can't figure out an east way to achieve what you're after without affecting the normal behaviour of PHPCS. If a developer decides to write a comment to ignore a line, and they choose to indent that line incorrectly (for example) and not conform to the rules you've required for those annotation (e.g., always put a comment after the annotation) I can't see how PHPCS can figure out that errors reported on that line are actually valid. It was a developer-level directive. So, where to go from here? Ultimately, we need the errors reported, so PHPCS can't drop them. If it can't drop them, it needs to flag them as being ignored from normal reporting. Then we need a way to disable this flag and make them available for normal reporting again. The errors exist, so we know they have error codes. Maybe we can add a ruleset.xml config that tells PHPCS to selectively surface these errors again. I'm thinking something that can be applied to any If this sounds like something that could work, we could work a bit on the XML syntax for re-surfacing the errors. Thoughts? |
Closing as replaced by PHPCSStandards/PHP_CodeSniffer#11 |
The new error suppression system ignores all notices for lines which only contain the new PHPCS native whitelist comments
// phpcs:....
.While this is great for, for instance, the
LineLength
sniff, it also means that, for instance, theSuperfluousWhitespace
orDisallowTabIndent
sniffs will not work for the lines containing these type of comments. Which means you still could end up with tab indentation even when space indentation is enforced.Also, the suppression on all notices for lines which only contain the new PHPCS native whitelist comment, also prevents examining these comments.
I was already discussing for one project to not allow "blanket" comment, i.e.
// phpcs:disable
without a sniffcode would not be allowed to be used in that project.Similarly, for that project, we would want to enforce, that all whitelist comments (except
// phpcs:enable
) have to be accompanied by a reason for whitelisting.Suppressing all errors for these type of lines, would also prevent any automated checks for this.
Any thoughts ?
The text was updated successfully, but these errors were encountered: