jonesshen/php-cs-fixer-config

A custom PHP CS Fixer configuration library

Maintainers

Package info

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

Homepage

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

Transparency log

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-22 04:06 UTC

This package is auto-updated.

Last update: 2026-08-22 04:08:43 UTC


README

这是一个轻量级的 PHP CS Fixer 编码规范配置库,提供了一套开箱即用的 PHP 代码格式化规则集。

功能特性

  • 集成 PSR-12、Symfony、DoctrineAnnotation 和 PhpCsFixer 官方推荐的编码规范
  • 支持 PHP 7.4+ 版本
  • 支持自定义文件头部注释
  • 可灵活配置的规则集,便于项目间统一代码风格

安装

通过 Composer 安装:

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

使用

在你的项目根目录创建 .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);

$header = '版权所有 (c) 2026 您的公司名称';

return (new Config())
    ->setRiskyAllowed(true)
    ->setFinder($finder)
    ->setUsingCache(false)
    ->setRules(Rules::php74Plus($header));

参数说明

Rules::php74Plus($header) 方法接受一个字符串参数作为文件头部注释内容:

// 使用自定义头部注释
$header = 'Copyright (c) 2026 Example Inc.';
$rules = Rules::php74Plus($header);

// 不传参或传入空值,将使用默认注释
$rules = Rules::php74Plus('');

运行代码格式化

# 检查代码风格(不修改文件)
./vendor/bin/php-cs-fixer fix --dry-run

# 自动修复代码风格
./vendor/bin/php-cs-fixer fix

规则集概览

该配置启用了以下主要规范集合:

规范集 说明
@PSR12 PSR-12 编码标准
@Symfony Symfony 框架编码风格
@DoctrineAnnotation Doctrine 注解编码规范
@PhpCsFixer PHP CS Fixer 官方推荐最佳实践

自定义调整的规则

  • binary_operator_spaces:数组键值对操作符 => 按作用域最少单空格对齐
  • yoda_style:不强制使用 Yoda 风格条件判断
  • general_phpdoc_annotation_remove:默认移除 @author 注解
  • header_comment:自动在 <?php 后添加文件头部注释
  • declare_strict_types:强制添加严格类型声明

完整的规则列表请参考项目源码中的 Rules.php 文件。

环境要求

  • PHP >= 5.6
  • friendsofphp/php-cs-fixer

许可证

本项目采用 MIT 许可证开源,详情请见 LICENSE 文件。

相关链接