Search by

jonesshen / php-cs-fixer-config

Jone Shen

A custom PHP CS Fixer configuration library

Package info

github.com/jonesshen/php-cs-fixer-config

Homepage

pkg:composer/jonesshen/php-cs-fixer-config

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-11-28 02:41 UTC

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)。

相关链接