本文目录导读:

我来为您提供一份详细的 PHP CS Fixer 配置说明和示例。
基础配置文件 (.php-cs-fixer.php)
<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$finder = Finder::create()
->in([
__DIR__ . '/app',
__DIR__ . '/config',
__DIR__ . '/database',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
$config = new Config();
return $config
->setFinder($finder)
->setRules([
// 基础规则
'@PSR12' => true,
'@PhpCsFixer' => true,
'@Symfony' => true,
// 数组相关
'array_syntax' => ['syntax' => 'short'],
'no_multiline_whitespace_around_double_arrow' => true,
'trim_array_spaces' => true,
// 导入相关
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
],
'no_unused_imports' => true,
'single_import_per_statement' => true,
// 代码风格
'braces' => [
'allow_single_line_closure' => true,
'position_after_functions_and_oop_constructs' => 'next',
],
'function_declaration' => [
'closure_function_spacing' => 'one',
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
// 字符串相关
'single_quote' => true,
'escape_implicit_backslashes' => true,
// 空白和换行
'blank_line_before_statement' => [
'statements' => ['return', 'throw', 'if', 'while', 'for', 'foreach'],
],
'no_extra_blank_lines' => true,
'no_whitespace_in_blank_line' => true,
'concat_space' => ['spacing' => 'none'],
// 函数和调用
'strict_param' => true,
'declare_strict_types' => true,
'void_return' => true,
'single_blank_line_at_eof' => true,
// 命名空间和类
'class_attributes_separation' => [
'elements' => [
'const' => 'one',
'method' => 'one',
'property' => 'one',
],
],
'class_definition' => [
'multi_line_extends_each_single_line' => true,
'single_item_single_line' => true,
],
// 其他
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => false,
],
'binary_operator_spaces' => [
'default' => 'align_single_space',
],
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
])
->setRiskyAllowed(true)
->setUsingCache(true);
简单的项目配置 (适合小型项目)
<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$finder = Finder::create()
->in([__DIR__ . '/src', __DIR__ . '/tests']);
return (new Config())
->setFinder($finder)
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
'ordered_imports' => true,
'single_quote' => true,
'declare_strict_types' => true,
]);
Laravel 项目配置
<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$finder = Finder::create()
->in([
base_path('app'),
base_path('config'),
base_path('database'),
base_path('routes'),
base_path('tests'),
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return (new Config())
->setFinder($finder)
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['default' => 'single_space'],
'blank_line_before_statement' => [
'statements' => ['return'],
],
'cast_spaces' => ['space' => 'none'],
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => true,
'function_typehint_space' => true,
'include' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'magic_method_casing' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'native_function_casing' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_extra_blank_lines' => true,
'no_trailing_whitespace' => true,
'no_whitespace_in_blank_line' => true,
'object_operator_without_whitespace' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'short_scalar_cast' => true,
'single_blank_line_at_eof' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'trailing_comma_in_multiline' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
])
->setRiskyAllowed(true);
配置文件说明
常用规则解释
| 规则 | 说明 |
|---|---|
@PSR12 |
使用 PSR-12 编码标准 |
@PhpCsFixer |
PhpCsFixer 推荐的规则集 |
@Symfony |
Symfony 编码标准 |
array_syntax |
数组语法,使用短数组 |
ordered_imports |
导入语句排序 |
no_unused_imports |
移除未使用的导入 |
single_quote |
使用单引号 |
declare_strict_types |
添加严格类型声明 |
void_return |
添加 void 返回类型 |
strict_param |
使用严格参数 |
使用命令
检查代码风格
# 检查但不修改 vendor/bin/php-cs-fixer fix --dry-run --diff
修复代码
# 自动修正代码风格 vendor/bin/php-cs-fixer fix
指定文件或目录
# 修复特定目录 vendor/bin/php-cs-fixer fix app/Models # 修复特定文件 vendor/bin/php-cs-fixer fix app/Models/User.php
使用配置文件
# 指定配置文件 vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php # 使用项目配置 vendor/bin/php-cs-fixer fix --using-cache=true
高级配置示例
多配置文件结构
// .php-cs-fixer.dist.php (主配置)
<?php
$configs = [];
$configs[] = require __DIR__ . '/.php-cs-fixer.base.php';
// 可根据环境加载不同配置
if (getenv('APP_ENV') === 'production') {
$configs[] = require __DIR__ . '/.php-cs-fixer.production.php';
}
return $configs;
忽略特定代码
// 在代码中忽略特定行的格式化
class ExampleClass
{
// phpcs:disable
public function someUnformattedCode()
{
$array = [1,2,3, 4,5, 6];
return $array;
}
// phpcs:enable
}
安装配置步骤
# 1. 安装 PHP CS Fixer
composer require --dev friendsofphp/php-cs-fixer
# 2. 创建配置文件
touch .php-cs-fixer.php
# 3. 添加 composer 脚本 (composer.json)
{
"scripts": {
"cs:fix": "php-cs-fixer fix",
"cs:check": "php-cs-fixer fix --dry-run --diff"
}
}
# 4. 加入到 CI/CD 流程
配合 IDE 使用
PHPStorm
- 设置 > 工具 > PHP CS Fixer
- 配置路径和规则
- 开启保存时自动格式化
VSCode
- 安装 PHP CS Fixer 扩展
- 配置
.vscode/settings.json - 设置保存时自动格式化
这份配置覆盖了大多数 PHP 项目的需求,您可以根据项目具体情况进行调整。