prettier / prettier/plugin-php
Allow attributes to be placed above constructor properties
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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