juliangut/easy-coding-standard-config

easy-coding-standard 配置


README

PHP version Latest Version License

Total Downloads Monthly Downloads

easy-coding-standard-config

针对 Easy-Coding-Standard 的具有偏见的默认配置

安装

Composer

composer require --dev juliangut/easy-coding-standard-config

用法

在项目根目录创建 ecs.php 文件

<?php

use Jgut\ECS\ConfigSet82;

return (new ConfigSet82())
    ->setHeader(<<<'HEADER'
    Easy Coding Standard config.

    @license BSD-3-Clause
    @link https://github.com/juliangut/easy-coding-standard-config
    HEADER)
    ->configureBuilder()
    ->withPaths([
        __FILE__,
        __DIR__ . '/src',
    ]);

根据您想要支持的 PHP 版本,使用提供的配置之一

  • Jgut\ECS\ConfigSet83,PHP >= 8.3
  • Jgut\ECS\ConfigSet82,PHP >= 8.2
  • Jgut\ECS\ConfigSet81,PHP >= 8.1
  • Jgut\ECS\ConfigSet80,PHP >= 8.0

配置

头部

提供一个头部字符串,它将被添加到每个由 php-cs-fixer 分析的文件之前。

字符串 {{year}} 将被替换为当前年份,字符串 {{package}} 将被替换为您的包名。

(new ConfigSet82())
    ->setHeader(<<<'HEADER'
    (c) 2021-{{year}} Julián Gutiérrez <juliangut@gmail.com>

    This file is part of package {{package}}
    HEADER);
--- Original
+++ New
 <?php

+/*
+ * (c) 2021-2024 Julián Gutiérrez <juliangut@gmail.com>
+ *
+ * This file is part of package juliangut/php-cs-fixer-config
+ */
+
 declare(strict_types=1);

 namespace App;

如果 {{year}} 在当前年份之前,它将被合并成一个单一的日期。

// Assuming current year is 2024
(new ConfigSet82())
    ->setHeader(<<<'HEADER'
    (c) 2023-{{year}} Julián Gutiérrez <juliangut@gmail.com>

    This file is part of package {{package}}
    HEADER);
--- Original
+++ New
 <?php

+/*
+ * (c) 2024 Julián Gutiérrez <juliangut@gmail.com>
+ *
+ * This file is part of package juliangut/php-cs-fixer-config
+ */
+
 declare(strict_types=1);

 namespace App;

PHPUnit

如果您使用 PHPUnit

(new ConfigSet82())
    ->enablePhpUnitRules();

Doctrine

如果您使用 Doctrine

(new ConfigSet82())
    ->enableDoctrineRules();

类型推断

如果您正在进行“类型提示一切”的过程,请尝试启用类型推断规则,并让 php-cs-fixer 将类型从注释迁移到属性、参数和返回类型。

请注意,这些规则是实验性的,修复后需要人工监督,因此建议您不要永久启用类型推断。

(new ConfigSet82())
    ->enableTypeInferRules();
--- Original
+++ New
<?php

 declare(strict_types=1);

 namespace App;
 
 class Foo
 {
-    /**
-     * @var string|null
-     */
-    protected $foo
+    protected ?string $foo

-    /**
-     * @var Bar
-     */
-    protected $bar
+    protected Bar $bar

-    /**
-     * @var bool
-     */
-    protected $baz
+    protected bool $baz

     /**
      * Foo constructor.
-     *
-     * @param string|null $foo
-     * @param Bar         $bar
-     * @param bool        $baz
      */
-    public function __construct($foo, $bar, $baz = false)
+    public function __construct(?string $foo, Bar $bar, bool $baz = false)
     {
         $this->foo = $foo;
         $this->bar = $bar;
         $this->baz = $baz;
     }

-    /**
-     * @return bool
-     *
-    public function isBaz()
+    public function isBaz(): bool
     {
        return $this->baz;
     }
 }

附加规则

如果您需要添加一些附加规则,这些规则可以是新的,也可以覆盖已设置的规则,最简单的方法是使用 setAdditionalRules 方法。

最好通过它们的类名来识别修复程序,但使用修复程序名称也可以。

(new ConfigSet82())
    ->setAdditionalRules([
        SingleLineThrowFixer::class => true,
    ]);

自定义配置

如果您需要更多控制要应用的规则,或者更喜欢更简洁的设置,您可以轻松创建自定义修复程序配置,而不是设置附加规则。

use Jgut\ECS\ConfigSet82;
use PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer;

class CustomConfigSet extends ConfigSet82
{
    protected function getRules(): array
    {
        // Return your custom rules, or add/remove rules from parent's getRules()
        return array_merge(
            parent::getRules(),
            [
                SingleLineThrowFixer::class => true,
            ]
        );
    }
}

贡献

发现了错误或有功能请求?请 创建一个新的问题。在创建之前,请查看现有的问题。

请参阅 CONTRIBUTING.md 文件。

许可协议

有关许可协议的副本,请参阅包含在源代码中的 LICENSE 文件。