laravel / pint

Laravel Pint is an opinionated PHP code style fixer for minimalists.
https://laravel.com/docs/pint
MIT License
2.73k stars 133 forks source link

Different formatting for traits depending on the initial arrangement #278

Closed dennisprudlo closed 1 month ago

dennisprudlo commented 1 month ago

Pint Version

1.15.1

PHP Version

8.3.7

Description

So I have a list of traits in my class that are arranged alphabetically. There's one exception: A trait that uses a generic annotation. It has been separated because of the weird looking PHP doc in between the other traits:

class Path extends IdentifiableModel implements Imageable, ReferenceAware, Shareable, Validatable
{
    use HasFactory;
    use OrdersEntities;
    use RouteScoping;
    use SoftDeletes;
    use UniqueId;

    /**
     * @use \App\Concerns\Labels<\App\Models\PathName, \App\Models\PathDescription>
     */
    use Labels;
}

Now running pint (by the way I use the default config, no special configuration) this is the result:

class Path extends IdentifiableModel implements Imageable, ReferenceAware, Shareable, Validatable
{
    use HasFactory;
    /**
     * @use \App\Concerns\Labels<\App\Models\PathName,\App\Models\PathDescription>
     */
    use Labels;
    use OrdersEntities;
    use RouteScoping;
    use SoftDeletes;

    use UniqueId;
}

So far so good. The traits have been rearranged in an alphabetical order. However I don't know why there is an empty line before UniqueId. Probably because the Label-trait had an empty line before as well. But usually the empty lines get stripped so there are no empty lines between traits.

Anyway, I then removes the empty line before UniqueId and out of interest ran pint again, just to see if it actually adds an empty line again. But this time the result was different:

class Path extends IdentifiableModel implements Imageable, ReferenceAware, Shareable, Validatable
{
    use HasFactory;

    /**
     * @use \App\Concerns\Labels<\App\Models\PathName,\App\Models\PathDescription>
     */
    use Labels;

    use OrdersEntities;
    use RouteScoping;
    use SoftDeletes;
    use UniqueId;
}

This happens probably because pint also wants to add a new line due to the Labels trait having a comment and another rule wants to add some space here. The same process form the last step without a PHP doc on Labels results in a perfectly arranged list.

I know I can remove the PHP doc from the trait by simply adding it to the class itself, however the people at phpstan use this in an example themselves: https://phpstan.org/blog/generics-by-examples#specify-template-type-variable-of-a-generic-trait-when-using-it.

Just wanted to let you know that pint creates different results here based on the initial order in case it shouldn't be that way.

Steps To Reproduce

Creating a class with some traits like in the first code snipped should do it.

driesvints commented 1 month ago

Heya, this sounds like an issue that needs to be reported to PHP CS Fixer itself.