bssphp/php-cs-fixer-config

个人 PHP-CS-Fixer 配置

1.3 2023-01-16 08:55 UTC

This package is auto-updated.

Last update: 2024-09-16 19:33:13 UTC


README

Latest Stable Version Total Downloads License GitHub Build Status

个人 PHP-CS-Fixer 封装,预设管理和自定义规则。

安装

composer require bssphp/php-cs-fixer-config --dev

注意:如果您需要在 vendor/bin 文件夹中具有可执行文件的本地安装,您还需要安装 PHP-CS-Fixer 包 本身。

使用方法

此包旨在简化多个项目的配置管理,并保持 PHP CS Fixer 规则的更新。

.php-cs-fixer.dist.php

return bssphp\Fixer\Config::make()
    ->in(__DIR__)
    ->preset(
        new bssphp\Fixer\Presets\PrettyPHP()
    )
    ->out();

可用预设

您可以通过扩展 AbstractPreset 类轻松创建自己的预设。

覆盖预设

如果您只需要为特定项目进行一些调整——这些调整不值得创建一个预设——您可以使用各种方法。

$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.

可用规则

将 PHPDoc 类转换为 FQCN

$fixer->withRules([
    'bssphp/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 bssphp\Fixer\Config::make()
    // ...
    ->finderCallback(static function (PhpCsFixer\Finder $finder): void {
        // ...
    })
    ->configCallback(static function (PhpCsFixer\Config $config): void {
        $config->registerCustomFixers();
        // ...
    })
    // ...
    ->out();

PHPStorm 配置

先决条件

您需要在系统上全局安装 PHP CS Fixer,因为 PHPStorm 不允许 您根据项目或项目路径设置 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 可执行文件