plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Allow attributes to be placed above constructor properties

Open piotrjoniec opened this issue 1 year ago • 1 comments

Currently, attributes used on constructor properties are forcefully placed inline with the property (or above if it doesn't fit the print width).

This can result in chaotic code where the types and names of the properties are all over the place, making it much harder to scan.

I feel like it would be better to allow the attributes to retain their original placement, as long as everything fits within the print width.

Here's an example of this happening:

@prettier/plugin-php v0.22.2 Playground link

Input:

<?php

class VariantData extends Data
{
    public function __construct(
        #[Exists(Variant::class, 'id')]
        public int|Optional $id,

        public int|null $media_id,

        #[RequiredWithout('id'), Filled, StringType, Max(255)]
        public string $title,

        #[RequiredWithout('id'), Numeric, Min(0)]
        public string $price,

        #[Numeric, Min(0)]
        public string|null $compare_at_price,

        #[Min(0)]
        public int $min_stock,
    ) {
    }
}

Output:

<?php

class VariantData extends Data
{
    public function __construct(
        #[Exists(Variant::class, 'id')] public int|Optional $id,

        public int|null $media_id,

        #[
            RequiredWithout('id'),
            Filled,
            StringType,
            Max(255),
        ]
        public string $title,

        #[RequiredWithout('id'), Numeric, Min(0)] public string $price,

        #[Numeric, Min(0)] public string|null $compare_at_price,

        #[Min(0)] public int $min_stock,
    ) {
    }
}

piotrjoniec avatar Feb 15 '24 20:02 piotrjoniec

Yes, appears that this would be in line with the latest PER CS for Attributes https://www.php-fig.org/per/coding-style/#12-attributes

Following the spec it looks like all attributes should be on their own line. I am not sure if we want this in closures parameters though.

cseufert avatar Jun 11 '24 00:06 cseufert