-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Add RectorConfig upgrade rule to make upgrade easier :) (#2098)
- Loading branch information
1 parent
b92465d
commit d90953d
Showing
11 changed files
with
448 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
rules-tests/DogFood/Rector/Closure/UpgradeRectorConfigRector/Fixture/configured_rule.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture; | ||
|
||
use Rector\Renaming\Rector\Name\RenameClassRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(RenameClassRector::class) | ||
->configure([ | ||
'old_class' => 'new_class', | ||
]); | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture; | ||
|
||
use Rector\Renaming\Rector\Name\RenameClassRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (\Rector\Config\RectorConfig $rectorConfig): void { | ||
$rectorConfig | ||
->ruleWithConfiguration(RenameClassRector::class, [ | ||
'old_class' => 'new_class', | ||
]); | ||
}; | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
...od/Rector/Closure/UpgradeRectorConfigRector/Fixture/remove_rector_config_services.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture; | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$services = $rectorConfig->services(); | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture; | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig) : void { | ||
}; | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
rules-tests/DogFood/Rector/Closure/UpgradeRectorConfigRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture; | ||
|
||
use Rector\Core\Configuration\Option; | ||
use Rector\Php74\Rector\Property\TypedPropertyRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$parameters = $containerConfigurator->parameters(); | ||
$parameters->set(Option::PARALLEL, true); | ||
$parameters->set(Option::AUTO_IMPORT_NAMES, true); | ||
|
||
$services = $containerConfigurator->services(); | ||
$services->set(TypedPropertyRector::class); | ||
}; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture; | ||
|
||
use Rector\Core\Configuration\Option; | ||
use Rector\Php74\Rector\Property\TypedPropertyRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (\Rector\Config\RectorConfig $rectorConfig): void { | ||
$rectorConfig->parallel(); | ||
$rectorConfig->importNames(); | ||
$rectorConfig->rule(TypedPropertyRector::class); | ||
}; | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...-tests/DogFood/Rector/Closure/UpgradeRectorConfigRector/UpgradeRectorConfigRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class UpgradeRectorConfigRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
rules-tests/DogFood/Rector/Closure/UpgradeRectorConfigRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DogFood\Rector\Closure\UpgradeRectorConfigRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(UpgradeRectorConfigRector::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
rules/DogFood/NodeAnalyzer/ContainerConfiguratorCallAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DogFood\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use Rector\Core\Contract\Rector\RectorInterface; | ||
use Rector\Core\PhpParser\Node\Value\ValueResolver; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class ContainerConfiguratorCallAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly ValueResolver $valueResolver, | ||
private readonly NodeNameResolver $nodeNameResolver, | ||
) { | ||
} | ||
|
||
public function isMethodCallWithServicesSetConfiguredRectorRule(MethodCall $methodCall): bool | ||
{ | ||
return $this->nodeNameResolver->isName($methodCall->name, 'configure'); | ||
} | ||
|
||
public function isMethodCallWithServicesSetRectorRule(MethodCall $methodCall): bool | ||
{ | ||
if (! $this->isMethodCallNamed($methodCall, 'services', 'set')) { | ||
return false; | ||
} | ||
|
||
$firstArg = $methodCall->getArgs()[0]; | ||
$serviceClass = $this->valueResolver->getValue($firstArg->value); | ||
|
||
if (! is_string($serviceClass)) { | ||
return false; | ||
} | ||
|
||
return is_a($serviceClass, RectorInterface::class, true); | ||
} | ||
|
||
public function isMethodCallNamed(Expr $expr, string $variableName, string $methodName): bool | ||
{ | ||
if (! $expr instanceof MethodCall) { | ||
return false; | ||
} | ||
|
||
if (! $this->nodeNameResolver->isName($expr->var, $variableName)) { | ||
return false; | ||
} | ||
|
||
return $this->nodeNameResolver->isName($expr->name, $methodName); | ||
} | ||
} |
Oops, something went wrong.