simgroep / coding-standards

使用 fabpot/php-cs-fixer 的 SIMGroep PHP 项目编码规范配置

2.1.0 2024-03-14 05:46 UTC

This package is auto-updated.

Last update: 2024-09-14 06:49:47 UTC


README

使用 friendsofphp/php-cs-fixer 为 SIMGroep PHP 项目配置编码规范

安装

$ composer require simgroep/coding-standards

配置

要在项目中自定义默认配置,您可以添加一个 .php-cs-fixer.php 文件,并包含默认配置,如下所示

<?php


$config = require 'vendor/simgroep/coding-standards/php-cs-fixer.dist.php';

$config->setFinder(
    \PhpCsFixer\Finder::create()
        ->in([
            getcwd() . '/src',
            getcwd() . '/test',
        ])
    )
;

return $config;

旧版注意 已废弃的配置文件名为 .php_cs,对于旧版使用可能需要 vendor/simgroep/coding-standards/.php_cs.dist

Symfony 示例

当将编码规范应用于标准 Symfony 项目时,您可以自定义配置以实现黑名单方法,如下所示

<?php

$config = require 'vendor/simgroep/coding-standards/.php_cs.dist';

$config->setFinder(
    \PhpCsFixer\Finder::create()
        ->in(getcwd())
        ->exclude('app')
        ->exclude('bin')
        ->exclude('var/cache')
        ->exclude('vendor')
    )
;

return $config;

用法

仅检查编码规范违规

$ ./vendor/bin/php-cs-fixer fix --dry-run --diff --config=vendor/simgroep/coding-standard/.php_cs.dist --stop-on-violation --allow-risky=yes

修复编码规范违规

$ ./vendor/bin/php-cs-fixer fix --diff --config=vendor/simgroep/coding-standard/.php_cs.dist --stop-on-violation --allow-risky=yes

PhpStorm 集成

首先全局安装此库

$ composer global require simgroep/coding-standards

打开 PhpStorm 并导航到 "设置" -> "工具" -> "文件监视器",添加一个 (+)。禁用 "立即文件同步"。

程序: /Users/<username>/.composer/vendor/bin/php-cs-fixer

参数: fix $FileDir$/$FileName$ --config=/Users/<username>/.composer/vendor/simgroep/coding-standards/.php_cs.dist

工作目录: $ProjectFileDir$