prettier / prettier/plugin-php

Allow attributes to be placed above constructor properties

Open
#2,329 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

psr compat
Dominant language
PHP
Stars
1.9k
Forks
139
PR merge metrics
No merged PRs in 30d

Description

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,
    ) {
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked playground example and compare the input and output to understand how constructor-property attributes are currently formatted. The change is done when attributes retain their original placement whenever the resulting code fits within the print width, while still formatting oversized attributes appropriately.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.