prettier / prettier/plugin-php

Allow attributes to be placed above constructor properties

Offen
#2,329 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

psr compat
Vorherrschende Sprache
PHP
Sterne
1.9k
Forks
139
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten Playground-Beispiel und vergleiche Eingabe und Ausgabe, um zu verstehen, wie Attribute von Konstruktor-Properties derzeit formatiert werden. Die Änderung ist abgeschlossen, wenn Attribute ihre ursprüngliche Platzierung beibehalten, wann immer der resultierende Code in die Druckbreite passt, während zu große Attribute weiterhin angemessen formatiert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.