[FR] Attributes: sniff to enforce no blank lines between attributes/docblock/structure
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.NoBlankLines
To address these rules from PER:
There MUST NOT be any blank lines between the docblock and attributes, or the attributes and the structure.
If two separate attribute blocks are used in a multi-line context, they MUST be on separate lines with no blank lines between them.
Notes for the implementation
Suggested error codes:
-
BlankLineFoundAbove(only when there is a docblock above) -
BlankLineFoundBelow
Rule should only apply if an attribute block is on a line by itself (or at least doesn't have non-attribute real code before or after it to prevent issues with function parameter attributes).
Will need careful verification that this sniff would not create fixer conflicts with the sniff proposed in #388 and that it works well together with the sniff proposed in #389.
Describe the solution you'd like
A new sniff as outlined above.
The sniff should be able to flag and auto-fix the following:
// OK.
/**
* Docblock
*/
#[AttributeOne]
#[AttributeTwo]
class Foo {}
// Error.
/**
* Docblock
*/
#[AttributeOne]
#[AttributeTwo]
class Foo {}
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.