Skip to content

Squiz.Commenting.PostStatementComment should allow trailing annotations from popular PHP tools #560

Closed
@rodrigoprimo

Description

@rodrigoprimo

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions