New in ECS: Simpler Config

Found a typo? Edit me

ECS runs on Symfony container configuration to build the service model. While it brings automated autowiring, array autowiring, and native container features, the downside is that ECS configuration syntax is complex and talkative.

We decided to simplify it so ECS is truly easy to use.

How do we configure ECS now?

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;

// ups, possible conflict with ContainerConfigurator
return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();

    // too verbose params, constants and possible typo in param value
    $parameters->set(Option::PATHS, [[ // ups, "[[" typo
        __DIR__ . '/src/',
    ]]);

    $services = $containerConfigurator->services();
    $services->set(ArraySyntaxFixer::class);
};


Phew! Such complexity is confusing just to read and easy to make an error in.

The new Way to Configure ECS

The new ECS version introduces its own ECSConfigobject that wraps around Symfony configuration and makes setup even more straightforward.


It brings:


How does it look?

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {
    $ecsConfig->paths([
        __DIR__ . '/src',
    ]);

    $ecsConfig->rule(ArraySyntaxFixer::class);

    $ecsConfig->sets([SetList::PSR_12]);
};

How to upgrade to ECSConfig?

First, upgrade your ECS to 10.2 to enjoy this feature. Then, replace ContainerConfigurator in your ecs.php with ECSConfig. And then? Use your IDE - autocomplete methods will lead you:


Do you configure your rules? Use ruleWithConfiguration() method:

    $ecsConfig->ruleWithConfiguration(ArraySyntaxFixer::class, [
        'syntax' => 'short',
    ]);


What if you miss upgrading an imported config? Don't worry. We've added a safe checker that goes through provided configs and warns you about an old configuration.

So you won't miss any of your configs to upgrade.


Happy coding!


Have you find this post useful? Do you want more?

Follow me on Twitter, RSS or support me on GitHub Sponsors.