gtjamesa / php-standards

James 分享的 PHP 风格规则,用于 PHP-CS-Fixer

v2.0.1 2021-09-17 09:51 UTC

This package is auto-updated.

Last update: 2024-09-17 17:00:45 UTC


README

本包定义了我用于多个项目的 FriendsOfPHP/PHP-CS-Fixer 编码标准。有关使用规则的详细信息,您可以使用 php-cs-fixer-configurator

使用方法

通过要求安装 php-cs-fixerphp-standards 包来安装此包

composer require --dev friendsofphp/php-cs-fixer gtjamesa/php-standards

接下来,在您的项目根目录中创建一个包含以下内容的 .php-cs-fixer.dist.php 文件

<?php

use PhpCsFixer\Finder;

$project_path = getcwd();
$finder = Finder::create()
    ->notPath('bootstrap/*')
    ->notPath('storage/*')
    ->notPath('resources/view/mail/*')
    ->in([
        $project_path . '/app',
        $project_path . '/config',
        $project_path . '/database',
        $project_path . '/resources',
        $project_path . '/routes',
        $project_path . '/tests',
    ])
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return JamesAusten\styles($finder);

上述文件假设为 Laravel 项目,但配置可以编辑为任何 PHP 7.0+ 项目。

将以下条目添加到您的 .gitignore 文件中

.php_cs.cache
.php-cs-fixer.cache

配置

可以在 ./src/rules.php 中找到 php-cs-fixer 规则。这些规则是从 laravel-shift/.php_cs.laravel.php 中提取的,并修改以适应我的编码风格。以下配置参数可以指定给 styles 函数。该函数返回一个 \PhpCsFixer\Config 实例,因此您也可以在函数调用之后链式添加更多的配置参数。

// Define rules to override the defaults
$rules = [
    'method_argument_space' => true,
    'phpdoc_summary' => false,
];

// Specify whether to enable caching (default: `true`)
// https://github.com/FriendsOfPHP/PHP-CS-Fixer#caching
$useCache = false;

return JamesAusten\styles($finder, $rules, $useCache)
    ->setCacheFile(__DIR__ . '.php_cs.cache'); // Set the cache file

运行修复器

要运行修复器,请执行以下命令

./vendor/bin/php-cs-fixer fix