per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

Require attributes be placed below docblocks

Open lpd-au opened this issue 2 years ago • 5 comments

To the best of my recollection in all of the code I've encountered, despite functioning correctly in either order, attributes are placed between any docblock and the target they apply to (class, method, etc). If you were to instead place attributes above the docblock, you'd encounter a nasty bug in any static analyser that relies on nikic's parser (eg phpstan): https://github.com/nikic/PHP-Parser/issues/762. In my opinion, this PER should require a consistent order of docblock first, then attributes and finally the node.

lpd-au avatar Apr 29 '22 16:04 lpd-au

I've run into this parser issue as well. At its root it's a PHP-Parser bug, but even that aside it's the sort of arbitrary thing worth trying to standardize.

Crell avatar Apr 29 '22 16:04 Crell

I came across similar issue few times too. This order makes sense as docblock is just a comment above any PHP code. Does not matter if attribute or any other stmt:

/** ... */
public $property;

/** ... */
#[Attribute]

Also, maybe we could help to automate it with @rectorphp :) https://github.com/rectorphp/rector/issues/7225

TomasVotruba avatar Jun 10 '22 06:06 TomasVotruba

Just linking these together: #26 does require attributes come after any comments

KorvinSzanto avatar Jul 17 '22 18:07 KorvinSzanto

Also it seems that the guide is not really clear how many lines of code are allowed between the attribute and the class.

#[Attribute]
class Test 
or 

#[Attribute]

class Test 

snake-py avatar Oct 12 '22 06:10 snake-py

The PR in #26 makes it clear that no blank line is permitted.

Crell avatar Oct 12 '22 15:10 Crell