jonesshen / php-cs-fixer-config
A custom PHP CS Fixer configuration library
Requires
- php: >=7.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ~3.95.32
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-28 02:46:21 UTC
README
一套开箱即用的 PHP CS Fixer 规则集:按目标项目的 PHP 版本选择,返回可直接交给 PhpCsFixer\Config::setRules() 的完整规则数组。
特性
- 内置 PHP 5.3 ~ 8.6 共 16 套规则集,每套均为完整展开的规则数组(各 282 条),不依赖
@PSR12、@Symfony等预设规则集 - 按 PHP 语言能力做差异化:语法规则只启用目标版本支持的特性
- 16 套规则集均可正常加载执行,最近一次校验基于 PHP CS Fixer v3.95.32
- 链式 API:可追加/覆盖单条规则、设置文件头部注释
- 运行时不依赖 php-cs-fixer:只负责生成规则数组(格式化工具仍需另行安装),规则名以较新的 3.x 为准
环境要求
| 用途 | 要求 |
|---|---|
| 本项目 | PHP >= 7.4 |
| 执行格式化 | friendsofphp/php-cs-fixer 3.x(较新的 3.x 要求 PHP >= 7.4) |
| 目标项目 | PHP 5.3 ~ 8.6,可按项目PHP版本要求来决定选用哪一套规则集 |
规则集中包含较新的规则(如
new_expression_parentheses、class_keyword、octal_notation),过旧的 3.x 版本会报The rules contain unknown fixers: "...",遇到时升级 php-cs-fixer 即可。
安装
本库只生成规则数组,格式化工具需一并安装:
composer require --dev jonesshen/php-cs-fixer-config
composer require --dev friendsofphp/php-cs-fixer
快速开始
在项目根目录创建 .php-cs-fixer.php 或 .php-cs-fixer.dist.php:
<?php use Jonesshen\Rules; use PhpCsFixer\Config; use PhpCsFixer\Finder; $finder = Finder::create() ->in(__DIR__) ->exclude('vendor') ->name('*.php') ->ignoreDotFiles(true) ->ignoreVCS(true); return (new Config()) ->setRiskyAllowed(true) ->setFinder($finder) ->setUsingCache(true) ->setRules( Rules::php74()->withHeader('版权所有 (c) 2026 您的公司名称')->toArray() );
要点:
Rules::phpXX()返回的是Jonesshen\Rules实例而不是数组,必须调用toArray()才能传给setRules()- 必须设置
setRiskyAllowed(true),规则集中启用了数十条 risky 规则(8.x 规则集为 57 条,详见注意事项) - 不需要头部注释时直接
Rules::php74()->toArray()
执行格式化:
# 只检查不修改,并输出差异
vendor/bin/php-cs-fixer fix --dry-run --diff
# 只检查不修改,并输出差异,使用 --using-cache=no 显式关闭缓存(避免命中缓存跳过文件)
vendor/bin/php-cs-fixer fix --dry-run --diff --using-cache=no
# 自动修复
vendor/bin/php-cs-fixer fix
使用缓存时(默认 .php-cs-fixer.cache)记得将其加入 .gitignore。
运行测试
composer test
测试基于 PHPUnit 9.x(兼容 PHP >= 7.4),遍历全部 16 套规则集,验证规则数量与键集合一致性、规则名合法性、配置有效性以及关键规则的 PHP 版本启用边界。
可用规则集
Rules 通过 __callStatic() 转发,方法名固定为 php{版本},直接调用 RuleSet::create() 生成对应版本的规则:
| 方法 | 适用版本 | 方法 | 适用版本 |
|---|---|---|---|
Rules::php53() |
PHP 5.3 | Rules::php74() |
PHP 7.4 |
Rules::php54() |
PHP 5.4 | Rules::php80() |
PHP 8.0 |
Rules::php55() |
PHP 5.5 | Rules::php81() |
PHP 8.1 |
Rules::php56() |
PHP 5.6 | Rules::php82() |
PHP 8.2 |
Rules::php70() |
PHP 7.0 | Rules::php83() |
PHP 8.3 |
Rules::php71() |
PHP 7.1 | Rules::php84() |
PHP 8.4 |
Rules::php72() |
PHP 7.2 | Rules::php85() |
PHP 8.5 |
Rules::php73() |
PHP 7.3 | Rules::php86() |
PHP 8.6 |
选择建议与注意事项:
- 按目标项目声明的最低 PHP 版本选择(
composer.json的require.php/config.platform);不确定时选低不选高,避免启用目标环境不支持的语法类规则 - 方法名格式为
php{两位数字}(大小写不敏感,Rules::PHP74()与Rules::php74()等价,跨平台一致);名称不匹配、目标 PHP 版本不受支持,或传入任何参数时抛出BadMethodCallException Rules为final类且构造函数私有,只能通过静态方法获取实例Rules.php顶部的@method static self phpXX()注解让 IDE 能提示并跳转这 16 个版本方法
API 说明
| 方法 | 说明 |
|---|---|
toArray(): array |
返回最终规则数组,供 setRules() 使用 |
withRules(array $rules): self |
合并自定义规则,同名规则覆盖已有配置 |
withHeader(string $header, string $location = 'after_open'): self |
设置文件头部注释,对应 header_comment 规则 |
use Jonesshen\Rules; $rules = Rules::php74() // 覆盖单条规则(整条规则配置被替换,见下方说明) ->withRules([ 'concat_space' => ['spacing' => 'none'], 'ordered_imports' => ['sort_algorithm' => 'length'], ]) // 自定义头部注释 ->withHeader('Copyright (c) 2026 Example Inc.', 'after_declare_strict') ->toArray();
withRules() 和 withHeader() 均为不可变方法:返回应用了修改的新实例,不修改原实例。链式调用时每一环都基于上一环的返回值。
withRules(array $rules): self
-
传入空数组时被忽略,不报错。
-
规则值传
false可显式关闭某条规则,例如->withRules(['yoda_style' => false])。 -
使用
array_merge()做浅合并:同名规则的整体配置被替换,而不是逐项合并。例如// 规则集默认:['default' => 'single_space', 'operators' => ['=>' => 'align_single_space_minimal_by_scope']] // 下面只给 default,会连带丢掉 => 的对齐设置 ->withRules(['binary_operator_spaces' => ['default' => 'align']])
需要保留原有选项时,请把完整配置写全。
withHeader(string $header, string $location = 'after_open'): self
$header:注释内容;为空字符串时回退为默认注释This is the header comment of the file。$location:插入位置,after_open(默认,紧跟<?php之后)或after_declare_strict(跟在declare(strict_types=1)之后),其它字符串值回退为after_open。- 生成的
header_comment配置为comment_type = PHPDoc、separate = both - 规则集默认
header_comment => false,只有调用withHeader()才会开启;若你自己在withRules()里设置过header_comment,后调用的会覆盖先调用的
注意事项
- risky 规则较多:各版本启用 47 ~ 57 条 risky 规则,其中 8.x 规则集为 57 条(如
declare_strict_types、strict_comparison等),因此配置中必须setRiskyAllowed(true),并在应用后运行测试确认行为未变 - 显式关闭的规则:每套规则集中有 37 ~ 66 条取值为
false的条目(如header_comment、final_class),这是刻意声明的“明确不启用”,便于阅读与后续覆盖;false不会影响其它规则 - 覆盖范围:每套 282 条,覆盖 php-cs-fixer 3.95 中除废弃别名外的全部规则;未包含
visibility_required、braces、single_space_after_construct等已废弃的旧规则名,请勿自行回填 header_comment默认关闭:需要统一版权头时用withHeader()开启src/Rules/RuleSet.php提供静态create()(返回规则数组)与supports()(版本校验),均无链式能力;统一走Rules::phpXX()入口即可获得链式 API
目录结构
src/
├── Rules.php # 统一入口:静态转发 + 链式配置
└── Rules/
├── Doctrine.php # doctrine_annotation_* 规则共用的忽略标签常量
└── RuleSet.php # 规则定义:以 Php86 为基准 + 版本降级差异表
许可证
本项目采用 MIT 许可证开源,详情见 LICENSE。
鸣谢 / 致谢
本项目各 PHP 版本的规则参考 ergebnis/php-cs-fixer-config (6.68.0)。