Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attributes to be placed above constructor properties #2329

Open
piotrjoniec opened this issue Feb 15, 2024 · 0 comments
Open

Allow attributes to be placed above constructor properties #2329

piotrjoniec opened this issue Feb 15, 2024 · 0 comments

Comments

@piotrjoniec
Copy link

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,
    ) {
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant