-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Squiz.Commenting.PostStatementComment should allow trailing annotations from popular PHP tools #560
Comments
Thanks for opening this issue @rodrigoprimo. As you may have guessed, I'm in favour of the proposed change. Having said that, I don't think the sniff should blindly ignore trailing comments which are tool related annotations. I think these should still be flagged, but flagged with a separate error code - maybe This also minimizes the BC-break/change in behaviour for the sniff.
Maybe the sniff shouldn't look for specific annotations, but for any trailing comment which starts with an That would allow it to also handle things like trailing
IMO whether the annotation format is supported by the tool it is intended for, is 100% out of scope. The format of the annotation/comment is not the concern of this sniff. This sniff only concerns itself with trailing comments. Think of it this way: the sniff also doesn't check whether a non-annotation comment uses proper punctuation, so why would it check the format of tooling annotations ? |
P.S.: by the looks of it, there is a work-around which could be applied at this time already and that is to use a trailing comment starting with Might be good to open a separate bug report for this as trailing comments are trailing comments, whether they start with |
I don't have a strong preference, and your suggestion works for me. I can prepare a PR implementing it. There is just one more thing that I want to check with you before proceeding. See below.
I believe the hyphen is not necessary in the regex unless we expect an annotation that starts with a hyphen? I guess just I'm not sure if there is a standard definition of what are valid characters for an annotation. I don't know if an annotation can start with or contain an underscore, for example. If there is no definition, maybe we should consider a comment to be an annotation if it starts with
👍
My guess is that Here is the issue: #562 |
@rodrigoprimo I think the PHP_CodeSniffer/src/Tokenizers/Comment.php Line 202 in 9168f53
|
Oh and for the record: There may or may not be a space between the comment marker and the |
Is your feature request related to a problem?
Currently, the
Squiz.Commenting.PostStatementComment
sniff makes it impossible to use trailing annotations like PHPUnit's// @codeCoverageIgnore
. This happens because the sniff forbids inline comments after statements (with some exceptions).Describe the solution you'd like
The
Squiz.Commenting.PostStatementComment
already makes some exceptions and allows inline comments after statements in a few cases. Most notably, it allows PHPCS's// phpcs:ignore
trailing annotation. I believe the sniff should also allow trailing annotations used by other popular PHP tools.Here is a non-exhaustive list of trailing annotations that the sniff should allow:
// @codeCoverageIgnore
(https://docs.phpunit.de/en/11.2/code-coverage.html#ignoring-code-blocks)// @phpstan-ignore
and// @phpstan-ignore-line
(https://phpstan.org/user-guide/ignoring-errors)If this change is implemented, the code examples below, which currently are flagged as errors by the sniff, will stop being flagged:
Open questions:
// @codeCoverageIgnore
while PHPStan requires an error identifier and, optionally, a comment after// @phpstan-ignore
. I think that the sniff doesn't need to conform to the exact rules used by each tool, and just checking that a given comment starts with the annotation supported by the tool is enough. So, for example,$a = 1; // @codeCoverageIgnore some comment
is not supported by PHPUnit, but would not be flagged by the sniff.Additional context (optional)
This was originally discussed with @jrfnl in #525 (review).
If the change proposed here is accepted, this will allow PHPCS itself to use
// @codeCoverageIgnore
in its codebase to make PHPUnit ignore lines of code that cannot be covered by tests when generating the code coverage report.The text was updated successfully, but these errors were encountered: