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

Inconsistent trait ordering and spacing fixes when running Pint #341

Open
thiagovictorino opened this issue Feb 4, 2025 · 0 comments
Open

Comments

@thiagovictorino
Copy link

thiagovictorino commented Feb 4, 2025

Pint Version

1.20

PHP Version

8.1.31

Description

When running Laravel Pint locally without modifying the file layout, no changes are applied, but adding an extra newline triggers reordering and spacing fixes. This inconsistency can lead to unexpected modifications in trait ordering and blank-line placement. It appears that the interaction between the class_attributes_separation, ordered_traits, and related fixers depends on subtle differences in the file’s layout. Moreover, when run on GitHub Actions, Pint applies a change or break the pipeline that isn’t caught locally.

I suspect this behavior results from the order in which the fixers are applied. It would be helpful to determine if there is a way to control or change the order of fixers applied by Laravel Pint (or PHP-CS-Fixer?) so that the output is consistent regardless of minor whitespace differences.

I am willing to contribute code to help resolve this issue, but I need guidance on the proper approach for controlling the fixers' order or handling inline comments with trait uses.

Steps To Reproduce

  1. I have a file like this
class PR extends BaseModel
{
    // uncomment to enable raw data
    use RawDataTrait;
    
    // uncomment to enable compression
    use CompressorTrait;

    use HasFactory;
}
  1. Run Laravel Pint Locally:
./vendor/bin/pint app/Model/PR.php

It applied the following rules

class_attributes_separation, ordered_traits, unary_operator_spaces, not_operator_with_successor_space, no_whitespace_in_blank_line

Changing the class that now looks like

class PR extends BaseModel
{
    // uncomment to enable compression
    use CompressorTrait;

    use HasFactory;
    // uncomment to enable raw data
    use RawDataTrait;
}

If I run the command again, no changes will be applied.

  1. Modify the File:
    Add an extra newline after the last trait so that the snippet becomes:
class PR extends BaseModel
{
    // uncomment to enable compression
    use CompressorTrait;

    use HasFactory;
    // uncomment to enable raw data
    use RawDataTrait;
           <---- here has a new line  now
}

Run the command again

./vendor/bin/pint app/Model/PR.php

Now the applied rules were

class_attributes_separation, no_trailing_whitespace

And the file changed to

class PR extends BaseModel
{
    // uncomment to enable compression
    use CompressorTrait;
    use HasFactory;

    // uncomment to enable raw data
    use RawDataTrait;
}

My pint.json is

{
    "preset": "laravel",
    "exclude": [
        "bootstrap",
        "config",
        "database"
    ],
    "rules": {
    }
}

My main questions are

  1. Why it does not fix when I run the command again without adding an extra line?
  2. Is there a way to change the sequence of fixes applied
  3. Is it a php-cs-fixer issue or is there something the I can do here to avoid it

I know that it is a major bug, but I would like to understand and see if I can avoid it or even see if there is a way to contribute with code.

@thiagovictorino thiagovictorino changed the title Inconsistent Trait Ordering and Spacing Fixes When Running Laravel Pint Inconsistent trait ordering and spacing fixes when running Pint Feb 4, 2025
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