netlogix/coding-guidelines-php

netlogix 编码规范工具集合

1.0.2 2023-10-17 15:00 UTC

This package is auto-updated.

Last update: 2024-09-03 13:34:14 UTC


README

安装

  • 通过以下方式安装 Composer 包:
composer require --dev netlogix/coding-guidelines-php
  • 使用以下方法将 CodeStyleSettings.xml 导入您的 PhpStorm IDE:设置/首选项 > 编辑器 > 代码样式 > PHP > ⚙️ > 导入方案...

基本配置

安装完成后,在 composer.json 旁边添加一个 ecs.php 文件

<?php

declare(strict_types=1);

use Netlogix\CodingGuidelines\Php\DefaultPhp;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
    (new DefaultPhp())->configure($ecsConfig);

    $ecsConfig->paths(
        [
            __DIR__ . '/src',
        ]
    );
};

添加 lintlint-fix 的 composer 脚本

{
    "name": "my/package",
    "require-dev": {
        "netlogix/coding-guidelines-php": "@dev"
    },
    "scripts": {
        "lint": "ecs check",
        "lint-fix": "ecs check --fix"
    }
}

然后您可以使用 composer run lint 进行代码检查,并使用 composer run lint-fix 尽可能修复问题。

Neos / Flow 项目的配置

对于 Neos 或 Flow 项目,有一个名为 DefaultFlow 的特殊规则集。您可以直接在 ecs.php 中使用它而不是 DefaultPhp

<?php

declare(strict_types=1);

use Netlogix\CodingGuidelines\Php\Neos\Flow\DefaultFlow;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
    (new DefaultFlow())->configure($ecsConfig);

    $ecsConfig->paths(
        [
            __DIR__ . '/DistributionPackages',
        ]
    );
};

自定义代码检查规则

要调整特定项目的代码检查规则,您可以在 ecs.php 中使用我们规则集提供的获取器。

<?php

declare(strict_types=1);

use Netlogix\CodingGuidelines\Php\DefaultPhp;
use Netlogix\CodingGuidelines\Php\Neos\Flow\DefaultFlow;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
    (new DefaultFlow())->configure($ecsConfig);

    // Combine rules of DefaultPhp, DefaultFlow and the FinalClassFixer
    $ecsConfig->rules(
        array_merge(
            DefaultPhp::getRules(),
            DefaultFlow::getRules(),
            [
                \PhpCsFixer\Fixer\ClassNotation\FinalClassFixer::class,
            ]
        )
    );
    
    // Combine skips of DefaultPhp, DefaultFlow and the OrderedClassElementsFixer
    $ecsConfig->skip(
        array_merge(
            DefaultPhp::getSkips(),
            DefaultFlow::getSkips(),
            [
                \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer::class,
            ]
        )
    );

    $ecsConfig->paths(
        [
            __DIR__ . '/src',
        ]
    );
};