romanzipp/php-cs-fixer-config

个人PHP-CS-Fixer配置

3.3.0 2024-08-06 14:56 UTC

This package is auto-updated.

Last update: 2024-09-06 15:15:06 UTC


README

Latest Stable Version Total Downloads License GitHub Build Status

个人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 文件夹中。这个缺点是只有一个单一的规则集。

3. 通过单击“PHP CS Fixer”链接导航到质量工具

4. 选择PHP-CS-Fixer可执行文件