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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ jobs:
matrix:
php_version: ['8.1']
directory:
- 'e2e/template-extends'
- 'e2e/plain-views'
- 'e2e/applied-rule-change-docblock'
- 'e2e/applied-rule-return-array-nodes'
- 'e2e/no-parallel-reflection-resolver'
- 'e2e/parallel-custom-config'
- 'e2e/parallel-reflection-resolver'
- 'e2e/no-parallel-reflection-resolver'
- 'e2e/applied-rule-return-array-nodes'
- 'e2e/applied-rule-change-docblock'
- 'e2e/plain-views'
- 'e2e/template-extends'
- 'e2e/use-rector-configurator'

name: End to end test - ${{ matrix.directory }}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
"enable-patching": true,
"patches": {
"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"
]
Comment on lines 155 to 158
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

},
"branch-alias": {
Expand Down
1 change: 1 addition & 0 deletions config/services-packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

$services->load('Rector\\', __DIR__ . '/../packages')
->exclude([
__DIR__ . '/../packages/Config/RectorConfig.php',
__DIR__ . '/../packages/*/{ValueObject,Contract,Exception}',
__DIR__ . '/../packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
__DIR__ . '/../packages/Testing/PHPUnit',
Expand Down
1 change: 1 addition & 0 deletions e2e/use-rector-configurator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
5 changes: 5 additions & 0 deletions e2e/use-rector-configurator/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.1"
}
}
22 changes: 22 additions & 0 deletions e2e/use-rector-configurator/expected-output.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1 file with changes
===================

1) src/SomeClass.php:6

---------- begin diff ----------
@@ @@
class SomeClass
{
public function __construct(
- private readonly \stdClass $stdClass
+ private \stdClass $stdClass
)
{
}
----------- end diff -----------

Applied rules:
* DowngradeReadonlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)


[OK] 1 file would have changed (dry-run) by Rector
15 changes: 15 additions & 0 deletions e2e/use-rector-configurator/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector;

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

$services = $rectorConfig->services();
$services->set(DowngradeReadonlyPropertyRector::class);
};
14 changes: 14 additions & 0 deletions e2e/use-rector-configurator/src/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace E2e\UseRectorConfigurator;

class SomeClass
{
public function __construct(
private readonly \stdClass $stdClass
)
{
}
}
28 changes: 28 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Config;

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Webmozart\Assert\Assert;

/**
* @api
* Same as Symfony container configurator, with patched return type for "set()" method for easier DX.
* It is an alias for internal class that is prefixed during build, so it's basically for keeping stable public API.
*/
final class RectorConfig extends ContainerConfigurator
{
/**
* @param mixed[] $paths
*/
public function paths(array $paths): void
{
Assert::allString($paths);

$parameters = $this->parameters();
$parameters->set(Option::PATHS, $paths);
}
}
34 changes: 17 additions & 17 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector;
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
use Rector\Config\RectorConfig;
use Rector\Core\Configuration\Option;
use Rector\Nette\Set\NetteSetList;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
Expand All @@ -16,25 +17,24 @@
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
return static function (RectorConfig $rectorConfig): void {
// include the latest PHP version + all bellow in one config!
$containerConfigurator->import(LevelSetList::UP_TO_PHP_81);
$rectorConfig->import(LevelSetList::UP_TO_PHP_81);

// include sets
$containerConfigurator->import(SetList::CODING_STYLE);
$containerConfigurator->import(SetList::CODE_QUALITY);
$containerConfigurator->import(SetList::DEAD_CODE);
$containerConfigurator->import(SetList::PRIVATIZATION);
$containerConfigurator->import(SetList::NAMING);
$containerConfigurator->import(SetList::TYPE_DECLARATION);
$containerConfigurator->import(SetList::EARLY_RETURN);
$containerConfigurator->import(SetList::TYPE_DECLARATION_STRICT);
$containerConfigurator->import(NetteSetList::NETTE_UTILS_CODE_QUALITY);
$containerConfigurator->import(PHPUnitSetList::PHPUNIT_CODE_QUALITY);

$services = $containerConfigurator->services();
$rectorConfig->import(SetList::CODING_STYLE);
$rectorConfig->import(SetList::CODE_QUALITY);
$rectorConfig->import(SetList::DEAD_CODE);
$rectorConfig->import(SetList::PRIVATIZATION);
$rectorConfig->import(SetList::NAMING);
$rectorConfig->import(SetList::TYPE_DECLARATION);
$rectorConfig->import(SetList::EARLY_RETURN);
$rectorConfig->import(SetList::TYPE_DECLARATION_STRICT);
$rectorConfig->import(NetteSetList::NETTE_UTILS_CODE_QUALITY);
$rectorConfig->import(PHPUnitSetList::PHPUNIT_CODE_QUALITY);

$services = $rectorConfig->services();

// phpunit
$services->set(PreferThisOrSelfMethodCallRector::class)
Expand All @@ -45,9 +45,9 @@
$services->set(ReturnArrayClassMethodToYieldRector::class)
->configure([new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*')]);

$parameters = $containerConfigurator->parameters();
$parameters = $rectorConfig->parameters();

$parameters->set(Option::PATHS, [
$rectorConfig->paths([
__DIR__ . '/bin',
__DIR__ . '/src',
__DIR__ . '/rules',
Expand Down