mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
Add RectorConfigurator for custom configuration methods and avoid conflicts with Symfony API (#2019)
Co-authored-by: Grzegorz Korba <grzegorz.korba@codito.pl>
This commit is contained in:
parent
4b351a7306
commit
c20f2301fe
11
.github/workflows/e2e.yaml
vendored
11
.github/workflows/e2e.yaml
vendored
|
@ -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 }}
|
||||
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
},
|
||||
"branch-alias": {
|
||||
|
|
|
@ -19,6 +19,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
$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',
|
||||
|
|
1
e2e/use-rector-configurator/.gitignore
vendored
Normal file
1
e2e/use-rector-configurator/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/vendor
|
5
e2e/use-rector-configurator/composer.json
Normal file
5
e2e/use-rector-configurator/composer.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"require": {
|
||||
"php": "^8.1"
|
||||
}
|
||||
}
|
22
e2e/use-rector-configurator/expected-output.diff
Normal file
22
e2e/use-rector-configurator/expected-output.diff
Normal file
|
@ -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
e2e/use-rector-configurator/rector.php
Normal file
15
e2e/use-rector-configurator/rector.php
Normal file
|
@ -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
e2e/use-rector-configurator/src/SomeClass.php
Normal file
14
e2e/use-rector-configurator/src/SomeClass.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace E2e\UseRectorConfigurator;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function __construct(
|
||||
private readonly \stdClass $stdClass
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
28
packages/Config/RectorConfig.php
Normal file
28
packages/Config/RectorConfig.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
32
rector.php
32
rector.php
|
@ -7,6 +7,7 @@ use Rector\CodingStyle\Enum\PreferenceSelfThis;
|
|||
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;
|
||||
|
@ -16,25 +17,24 @@ use Rector\PHPUnit\Set\PHPUnitSetList;
|
|||
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);
|
||||
$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 = $containerConfigurator->services();
|
||||
$services = $rectorConfig->services();
|
||||
|
||||
// phpunit
|
||||
$services->set(PreferThisOrSelfMethodCallRector::class)
|
||||
|
@ -45,9 +45,9 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$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',
|
||||
|
|
Loading…
Reference in New Issue
Block a user