laravel / pint

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

Laravel preset class parentheses #285

Closed brdv closed 1 month ago

brdv commented 1 month ago

This PR adds the rule new_with_parentheses to the Laravel preset with the configuration. It's a followup for https://github.com/laravel/pint/issues/284. The PR also runs Pint with the new rule over it's own codebase.

In after this change, running Pint with the Laravel Preset will make sure empty parentheses () will be removed from class Instantiations.

// the following line ...
$item = new Item();

// will become
$item = new Item;
driesvints commented 1 month ago

@brdv can you fix the tests?

rojtjo commented 1 month ago

For anyone looking to revert to the previous behavior. You can disable the rule by changing the value to false:

{
    "preset": "laravel",
    "rules": {
        "new_with_parentheses": false
    }
}
dm-pf commented 1 month ago

Today I got a +5000/-5000 changelog after running pint. That's how I found this PR. I like the laravel preset because until now it has closely matched the PSR-12 one (which I like quite a lot).

On that note, how are these decisions made to change the 'laravel' preset? A discussion somewhere? On a whim?

driesvints commented 1 month ago

@dm-pf we make these decisions internal at Laravel. This preset is how we want to format code at Laravel for our products/projects. Using it as a third party it's intended that this changes code in your project to match our preset. It doesn't matters how much lines of codes are changed as long as your project is up to date with the preset. If you don't want that then this preset isn't for you and it's better to roll one with your own rules.

dm-pf commented 1 month ago

@driesvints thanks for the info. I'm fine with the changes, I just wanted some info on how decisions are made and you already clarified that, which I appreciate.

kylemilloy commented 1 month ago

Quite the spicy PR... 😏

I seem to remember at one point that this was the default behaviour and then got removed but maybe I'm remembering pre-Pint era when there was a Laravel preset for CS-Fixer that did this...personally I like the lack of control syntax when not required so thank you for bringing it back @brdv

rrmesquita commented 1 month ago

If both ways were fine, and you wanted Pint to opinionate on this rule, why not stick with PSR-12?

When instantiating a new class, parentheses MUST always be present even when there are no arguments passed to the constructor.

kylemilloy commented 1 month ago

@rrmesquita - see above.

This preset is how we want to format code at Laravel for our products/projects

ralphjsmit commented 2 weeks ago

The code by @rojtjo is correct, but it will not force the parentheses if they are not there yet in new code. If you want to enforce them always (which I prefer and find logical), you can use this:

"new_with_parentheses": {
    "anonymous_class": false,
    "named_class": true
}

Note: set the anonymous_class based on your own preferences. If enabled, it will change your migrations from return new class extends Migration to return new class() extends Migration.