per-coding-style
per-coding-style copied to clipboard
Require attributes be placed below docblocks
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.
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.
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
Just linking these together: #26 does require attributes come after any comments
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
The PR in #26 makes it clear that no blank line is permitted.