Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DX] Add configure() method to service configuration to make configuration easy again #1276

Merged
merged 3 commits into from
Nov 20, 2021

Conversation

TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Nov 19, 2021

We're using Symfony PHP fluent configuration to add autocomplete and ECS, PHPStan and Rector luxury to configs.

Yet it comes for a price, of complex configuration, mainly in case of value objects. Symfony container cannot work with value objects natively, so extra inlining complexity has to be added:

Also it's prone to errors:

    ->call('configure', [[
    ->call('configure', [[[
    ->call('configure', [
    ->call('configure',

🔴

        PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
                TestCase::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_THIS()),
         ],

        PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => ValueObjectInliner::inline([
                TestCase::class => PreferenceSelfThis::PREFER_THIS(),
         ]),

        PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
                TestCase::class => PreferenceSelfThis::PREFER_THIS(),
         ],

🔴


This bothers the user with boring knowledge. 👎


New ->configure(...) method 🎉

Instead, this PR gets the simplicity back. Rector DI itself handles the value object wrapping by adding special ->configure() method.


Before 🤧

$services->set(SomeChangeRector::class)
    ->call('configure', [[
        SomeChangeRector::SOME_CONFIG => ValueObjectInliner::inline([
            new SomeChange('before', 'after');
        ]),
    ]]);

Now 👍

$services->set(SomeChangeRector::class)
    ->configure([
        SomeChangeRector::SOME_CONFIG => [
            new SomeChange('before', 'after');
        ],
    ]);

public function configure(array $configuration): self
{
	// ...
}

With array parameter type hint useful for PHPStorm and PHPStan as a bonus ✔️

@TomasVotruba TomasVotruba changed the title tv symfony set configure dx [DX] Add configure() method to service configuration to make configuration easy again Nov 19, 2021
@TomasVotruba TomasVotruba force-pushed the tv-symfony-set-configure-dx branch 3 times, most recently from bca22b1 to b729ebd Compare November 19, 2021 11:46
@samsonasik
Copy link
Member

The config in config directory need to be updated ? eg:

->call('configure', [[

@TomasVotruba
Copy link
Member Author

@samsonasik Yes, you're right.
This PR is a prototype testing, so I've updated only the rector.php. Maybe we missed something or there is a better way.

If it prooves to be right way, we can update all configs 👍

@TomasVotruba TomasVotruba merged commit 612e1d0 into main Nov 20, 2021
@TomasVotruba TomasVotruba deleted the tv-symfony-set-configure-dx branch November 20, 2021 10:07
@TomasVotruba
Copy link
Member Author

Let's ship it 🚢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants