romanzipp / php-cs-fixer-config
个人PHP-CS-Fixer配置
3.3.0
2024-08-06 14:56 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^7.0|^8.5.23|^9.0
README
个人PHP-CS-Fixer包装,预设管理和自定义规则。
安装
composer require romanzipp/php-cs-fixer-config --dev
注意:如果您需要在您的vendor/bin
文件夹中有一个可执行的本地安装,则需要安装PHP-CS-Fixer包本身。
用法
此包已创建,以简化多个项目的配置管理,并保持PHP CS Fixer规则更新。
.php-cs-fixer.dist.php
return romanzipp\Fixer\Config::make() ->in(__DIR__) ->preset( new romanzipp\Fixer\Presets\PrettyPHP() ) ->out();
可用预设
您可以通过扩展AbstractPreset类轻松创建自己的预设。
覆盖预设
如果您只需为特定项目进行一些调整(不值得一套自己的预设),则有各种方法可供使用。
$config = romanzipp\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.
可用规则
将PHPDoc类转换为FQCN
$fixer->withRules([ 'RomanZipp/phpdoc_fqcn' => true, ]);
显示代码示例
差
use App\Foo; use App\Bar; /** * @param Foo $foo * @return Bar[] */ function foo(Foo $foo): array {}
好
use App\Foo; /** * @param \App\Foo $foo * @return \App\Bar[] */ function foo(Foo $foo): array {}
高级用法
访问配置和查找实例
return romanzipp\Fixer\Config::make() // ... ->finderCallback(static function (PhpCsFixer\Finder $finder): void { // ... }) ->configCallback(static function (PhpCsFixer\Config $config): void { $config->registerCustomFixers(); // ... }) // ... ->out();
PHPStorm配置
先决条件
由于PHPStorm 不允许您按项目或项目路径设置php-cs-fixer可执行文件,因此您需要在系统上全局安装PHP CS Fixer。
composer global require friendsofphp/php-cs-fixer
1. 启用检查
2. 选择规则集 .php-cs-fixer.dist.php 文件 [...]
遗憾的是,您需要为每个项目重复此过程,因为 PHPStorm 中存在一个错误,阻止用户使用相对路径来使用.php-cs-fixer.dist.php
配置或可执行文件。
解决这个问题的一个理论方法是创建一个统一的规则集文件在您的用户 .composer 文件夹中。这个缺点是只有一个单一的规则集。