bssphp / php-cs-fixer-config
Personal PHP-CS-Fixer Configuration
Requires
- php: ^7.4|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^7.0|^8.5.23|^9.0
This package is auto-updated.
Last update: 2024-10-16 19:45:17 UTC
README
Personal PHP-CS-Fixer wrapper, preset management and custom rules.
Installation
composer require bssphp/php-cs-fixer-config --dev
Notice: You also need to install the PHP-CS-Fixer package itself if you need a local installation with executable in your vendor/bin
folder.
Usage
This package has been created to streamline configuration management for multiple projects and keeping PHP CS Fixer rules up to date.
.php-cs-fixer.dist.php
return bssphp\Fixer\Config::make() ->in(__DIR__) ->preset( new bssphp\Fixer\Presets\PrettyPHP() ) ->out();
Available Presets
- PrettyPHP
- PrettyLaravel (extends PrettyPHP)
You can easily create your own presets by extending the AbstractPreset class.
Overriding presets
In case you only need some tweaks for specific projects - which won't deserve an own preset - there are various methods you can make us of.
$config = bssphp\Fixer\Config::make(); $config->allowRisky(true); // Allow risky rules. $config->withRules(['...']); // Set additional rules $config->exclude(['...']); // Add single or many files to the list of excluded files. $config->excludeDirectories(['...']); // Add single or many directories to the list of excluded directories.
Available Rules
Convert PHPDoc Classes to FQCN
$fixer->withRules([ 'bssphp/phpdoc_fqcn' => true, ]);
Show code sample
Bad
use App\Foo; use App\Bar; /** * @param Foo $foo * @return Bar[] */ function foo(Foo $foo): array {}
Good
use App\Foo; /** * @param \App\Foo $foo * @return \App\Bar[] */ function foo(Foo $foo): array {}
Advanced Usage
Access the config and finder instances
return bssphp\Fixer\Config::make() // ... ->finderCallback(static function (PhpCsFixer\Finder $finder): void { // ... }) ->configCallback(static function (PhpCsFixer\Config $config): void { $config->registerCustomFixers(); // ... }) // ... ->out();
PHPStorm Configuration
Prequisites
You will need to install PHP CS Fixer globally on your system because PHPStorm does not allow you to set the php-cs-fixer executable on a per-project basis or relative to the project path.
composer global require friendsofphp/php-cs-fixer
1. Enable Inspection
2. Select ruleset .php-cs-fixer.dist.php file [...]
Unfortunately you have to repeat this process for every project since there is a bug in PHPStorm which prevents users from using relative paths for the .php-cs-fixer.dist.php
configuration or executable file.
Another theoretical approach to this issue is to create a unified ruleset file in your users .composer folder. This has the downside on only having one single ruleset.