Skip to content

[FR] Attributes: sniff to enforce comma spacing #391

@jrfnl

Description

@jrfnl

Setting the scene

PHP 8.0 introduced support for attributes via the #[...] syntax. At this moment, neither PHPCS itself, nor PHPCSExtra contain any sniffs to handle the formatting of attributes.

The PER Coding Standard from FIG, since PER 2.0, outlines a set of rules for attribute formatting to comply with, so using those rules as a starting point would allow for creating an initial set of sniffs to address attribute formatting.

Proposed new sniff: Universal.Attributes.CommaSpacing

To address these rules from PER:

If multiple attributes are placed in the same attribute block, they MUST be separated by a comma with a space following but no space preceding.

Notes for the implementation

This sniff could benefit from the upcoming PHPCSUtils AttributeBlock::getAttributes() method.
This sniff could also benefit from the SpacesFixer from PHPCSUtils.

Sniff should probably get the following configurable properties to give users some flexibility:

  • public $allowNewLine = false;

Suggested error codes:

  • SpaceBefore
  • SpaceAfter

Other notes:

  • Thing to be careful about is that this is about the comma between attributes, not about commas in parameter lists!
    Using the AttributeBlock::getAttributes() method should help with that.
  • Also need to be careful about not enforcing "space after" for attribute blocks with a trailing comma after the last attribute as it would conflict with the "space on the inside of attribute brackets" sniff as per [FR] Attributes: sniff to normalize spacing on the inside of attribute brackets #386.

Also note that comma spacing is already covered via the Universal.WhiteSpace.CommaSpacing sniff. We could look into updating that sniff to separate out the error code for attributes.
Should probably be done anyway, even if this becomes a separate sniff to allow for preventing duplicate error messages.
Keep in mind that we (again) would need to differentiate between the commas between attributes versus the commas in parameter lists.

Describe the solution you'd like

A new sniff as outlined above.

The sniff should be able to flag and auto-fix the following:

#[AttributeOne, AttributeTwo] // OK.
#[AttributeOne , AttributeTwo] // Error space before.
#[AttributeOne,AttributeTwo] // Error, space after.
#[AttributeOne,   AttributeTwo] // Error, space after.

Also see the examples outlined in the PER documents (rules + migration guide).

Additional context (optional)

This ticket is part of a series of tickets related to PHP attributes and is the result of a detailed analysis of the rules as outlined in PER 2.0, as well as a critical look at what's still missing rule-wise.

  • I intend to create a pull request to implement this feature.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions