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

Add RectorConfig for custom configuration methods and avoid conflicts with Symfony API #2019

Merged
merged 10 commits into from
Apr 8, 2022

Conversation

TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Apr 7, 2022

Follow up to #1891


Using Symfony container for rector.php configuration is handy for autocomplete, static analysis and re-use of Symfony DI container.
Yet, it has few drawbacks, when it comes to a project that runs on Symfony with it's own Symfony DI. There 2 classes of same name can conflict in IDE and come to an autoloading issue. See rectorphp/rector#7035 for more.

@Wirone came with a great idea, to have an own configurator class, that will be tailored to Rector's needs and features.
Similar to Config Builder Classes from Symfony: https://symfony.com/blog/new-in-symfony-5-3-config-builder-classes

How do we use rector.php now?

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;
use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;

// 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(DowngradeReadonlyPropertyRector::class);
};

How could we use RectorConfig in future?

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->parallel();

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


    $rectorConfig->rule(DowngradeReadonlyPropertyRector::class);
};

@TomasVotruba TomasVotruba changed the title proper configurator api Add RectorContainerConfigurator for custom methods Apr 7, 2022
@samsonasik
Copy link
Member

new e2e directory need to be register to e2e.yaml

directory:
- 'e2e/template-extends'
- 'e2e/plain-views'
- 'e2e/parallel-custom-config'
- 'e2e/parallel-reflection-resolver'
- 'e2e/no-parallel-reflection-resolver'
- 'e2e/applied-rule-return-array-nodes'
- 'e2e/applied-rule-change-docblock'

@TomasVotruba TomasVotruba force-pushed the proper-configurator-api branch from a788326 to c3c8bd0 Compare April 7, 2022 14:24
@samsonasik
Copy link
Member

custom config may need to be conditionally checked

- run: php ../e2eTestRunner.php -c custom/config/rector.php
working-directory: ${{ matrix.directory }}
if: ${{ matrix.directory == 'e2e/parallel-custom-config' }}

@TomasVotruba
Copy link
Member Author

new e2e directory need to be register to e2e.yaml

Hm 🤔 We should automate this with some script.

@samsonasik
Copy link
Member

custom config check need to be checked as well

- run: php ../e2eTestRunner.php --config custom/config/rector.php
working-directory: ${{ matrix.directory }}
if: ${{ matrix.directory == 'e2e/parallel-custom-config' }}

with || condition, ref

https://github.community/t/and-operator-in-if-condition/154825

if: ${{ (matrix.directory == 'e2e/parallel-custom-config') || (matrix.directory == 'e2e/use-rector-configurator')}}

also in line 64 with && condition`

if: ${{ (matrix.directory != 'e2e/parallel-custom-config') &&  (matrix.directory != 'e2e/use-rector-configurator') }}

@TomasVotruba TomasVotruba changed the title Add RectorContainerConfigurator for custom methods Add RectorConfigurator for custom configuration methods and avoid conflicts with Symfony API Apr 7, 2022
@TomasVotruba
Copy link
Member Author

@samsonasik > custom config check need to be checked as well

I see. I've just copy-pasted another directory, this can be in the root. I've moved it to avoid this one. Thanks

TomasVotruba referenced this pull request in rectorphp/vendor-patches Apr 7, 2022
Comment on lines 155 to 158
"symfony/dependency-injection": [
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/symfony-dependency-injection.patch"
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/symfony-dependency-injection.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/symfony-php-config-loader.patch"
]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR is merged, line needs to update in each Rector split repository's composer.json, so we have the same patched Symfony everywhere

@TomasVotruba TomasVotruba force-pushed the proper-configurator-api branch 2 times, most recently from 8eff286 to c04ce87 Compare April 7, 2022 16:20
@samsonasik
Copy link
Member

target-repository e2e may need update, for example on global install e2e https://github.com/rectorphp/rector-src/tree/main/build/target-repository/e2e/global-install

final class RectorConfig extends ContainerConfigurator
{
/**
* @param string[] $paths
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param mixed[] $paths

may be used when next Assert::allString($paths) is checked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@TomasVotruba TomasVotruba force-pushed the proper-configurator-api branch from 1f9e16e to 60e533f Compare April 8, 2022 18:37
@TomasVotruba TomasVotruba enabled auto-merge (squash) April 8, 2022 18:38
@TomasVotruba
Copy link
Member Author

Let's 🚢 it 👍

@TomasVotruba TomasVotruba force-pushed the proper-configurator-api branch from 60e533f to 30506cf Compare April 8, 2022 18:40
@TomasVotruba TomasVotruba merged commit c20f230 into main Apr 8, 2022
@TomasVotruba TomasVotruba deleted the proper-configurator-api branch April 8, 2022 18:45
@TomasVotruba TomasVotruba changed the title Add RectorConfigurator for custom configuration methods and avoid conflicts with Symfony API Add RectorConfig for custom configuration methods and avoid conflicts with Symfony API Apr 20, 2022
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.

3 participants