juliangut/easy-coding-standard-config

easy-coding-standard configuration

1.15 2024-03-05 15:51 UTC

This package is auto-updated.

Last update: 2024-05-05 21:35:58 UTC


README

PHP version Latest Version License

Total Downloads Monthly Downloads

easy-coding-standard-config

Opinionated as can be configuration defaults for Easy-Coding-Standard

Installation

Composer

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

Usage

Create ecs.php file at your project's root directory

<?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',
    ]);

Use one of the provided configurations depending on the PHP version you want to support:

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

Configurations

Header

Provide a header string, it will be prepended to every file analysed by php-cs-fixer.

The string {{year}} will be replaced by the current year, and the string {{package}} will be replaced by your package name

(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;

If {{year}} is preceded by current year it will be combined into a single date

// 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

If you work with PHPUnit

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

Doctrine

If you work with Doctrine

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

Type Inference

If you're in the middle of "type hinting everything", try enabling type inference rules and let php-cs-fixer migrate types from annotations into properties, parameters and return types

Be aware these rules are experimental and will need human supervision after fixing, so you are advised NOT to permanently enable type inference

(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;
     }
 }

Additional rules

If you need to add a few additional rules, this rules can be new or override rules already set, the easiest way is using setAdditionalRules method

It is preferred to identify fixers by their class name, anyway using fixer names will work as well

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

Custom config

If you need more control over applied rules or prefer a cleaner setup, you can easily create your custom fixer config instead of setting additional rules

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

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.