First of all, add PHP CS Fixer to your project:
composer require --dev friendsofphp/php-cs-fixer
A configuration file called .php-cs-fixer.dist.php needs to be in the root directory of your project if you want to avoid command-line options.
There are many rules that can be set, I usually use these. It follows the PSR12 code standards and checks both the src and tests directories.
You can find more rules in the PHP CS Fixer docs.
Let’s add a some scripts to composer.json file:
Now, just run composer run fix-style and all your code style errors will be fixed.
The --dry-run flag will run the fixer without making changes to your files.
If you are using Git, add .php-cs-fixer.cache (this is the cache file created by php-cs-fixer) to .gitignore:
.php-cs-fixer.cache
Top comments (0)