laravel / pint

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

Support custom fixers #290

Open Riley19280 opened 3 months ago

Riley19280 commented 3 months ago

Many times the community has asked for custom rules to be supported (#23, #26, #55, #161, #239, #286). With so many people persistently asking for this feature, I believe that it should finally be adopted.

The entire purpose of Pint is to be extremely opinionated with minimal configuration. Originally posted by taylorotwell in https://github.com/laravel/pint/issues/161#issuecomment-1483803186

While Pint does a great job at being extremely opinionated, it doesn't close off the ability for those opinions to be customized, allowing for presets to be extended, and also including presets for other frameworks such as Symfony.

Support for custom fixers is similar to providing support for customizing rules, letting the end user fine tune their configuration to their needs and opinions.

Many of us Laravel Enthusiasts would like to use the benefits that Pint and the Laravel ecosystem provides, without having to fork the package or use the underlying dependency just to make a configuration change.

I am happy to PR the docs update as well if this moves forward 🙂

(Also thanks to @devatreides for all the hard work in #161, which this heavily borrows from)

Jubeki commented 3 months ago

What would happen if the custom fixer uses a class outside the imported custom fixer file?

I think currently it would only be possible if the Custom Fixer only relies on PHP-CS-Fixer Classes and doesn't import any other class that is not compiled in the Pint.

I would like to try confirm that, but that is only possible after the binary is build.

Riley19280 commented 3 months ago

What would happen if the custom fixer uses a class outside the imported custom fixer file?

I think currently it would only be possible if the Custom Fixer only relies on PHP-CS-Fixer Classes and doesn't import any other class that is not compiled in the Pint.

I would like to try confirm that, but that is only possible after the binary is build.

In order to reference additional classes that are not the fixer (Ex. utilities), you would have to manually require_once that file

Additionally, if you just wanted to load everything that normally gets autoloaded you could do this in the custom fixer:

public function __construct() {
    require_once "vendor/autoload.php";
}
Aferz commented 1 month ago

Hey, any progress on this? I saw it was marked as draft by Taylor.

Anything we can do to move this forward?

yusufkandemir commented 1 week ago

One use case I've needed was Prettier. Laravel Pint VS Code extension (and PHP CS Fixer one) is structured as a formatter. It's not possible to use multiple formatters for a single file type in VS Code. So, one solution I could find is creating a custom fixer that executes Prettier. This way, the default formatter for PHP files will be Pint, which will then execute Prettier as part of PHP CS Fixer: https://github.com/prettier/plugin-php/tree/main/docs/recipes/php-cs-fixer

I think Prettier support can even be a new feature that can be implemented from the Prettier logic in #256