Description
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:
- PHPUnit's
// @codeCoverageIgnore
(https://docs.phpunit.de/en/11.2/code-coverage.html#ignoring-code-blocks) - PHPStan's
// @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:
$a = 1; // @codeCoverageIgnore
$b = 2; // @phpstan-ignore-line
$c = 3; // @phpstan-ignore variable.undefined
Open questions:
- Are there other trailing annotations used by popular PHP tools that the sniff should allow as well?
- PHPUnit doesn't support anything after
// @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.
- I have read the Contribution Guidelines and this is not a support question.
- I intend to create a pull request to implement this feature.