Skip to content

Commit

Permalink
[config] Add import bare support (#2103)
Browse files Browse the repository at this point in the history
* add import bare support

* add phpVersion method
  • Loading branch information
TomasVotruba committed Apr 20, 2022
1 parent 3986087 commit 61fd4d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import('any-config.php');
};

?>
-----
<?php

namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (\Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->import('any-config.php');
};

?>
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture;

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
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);
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);

$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class);
Expand All @@ -22,12 +24,14 @@ return static function (ContainerConfigurator $containerConfigurator): void {
namespace Rector\Tests\DogFood\Rector\Closure\UpgradeRectorConfigRector\Fixture;

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
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->phpVersion(PhpVersion::PHP_74);
$rectorConfig->rule(TypedPropertyRector::class);
};

Expand Down
27 changes: 20 additions & 7 deletions rules/DogFood/Rector/Closure/UpgradeRectorConfigRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ final class UpgradeRectorConfigRector extends AbstractRector
Option::AUTO_IMPORT_NAMES => 'importNames',
Option::PARALLEL => 'parallel',
Option::PHPSTAN_FOR_RECTOR_PATH => 'phpstanConfig',
Option::PHP_VERSION_FEATURES => 'phpVersion',
];

/**
Expand All @@ -53,6 +52,11 @@ final class UpgradeRectorConfigRector extends AbstractRector
*/
private const PARAMETERS_VARIABLE = 'parameters';

/**
* @var string
*/
private const CONTAINER_CONFIGURATOR_CLASS = 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator';

public function __construct(
private readonly ContainerConfiguratorCallAnalyzer $containerConfiguratorCallAnalyzer,
private readonly ContainerConfiguratorEmptyAssignRemover $containerConfiguratorEmptyAssignRemover,
Expand Down Expand Up @@ -119,10 +123,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->isNames($paramType, [
'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator',
self::RECTOR_CONFIG_CLASS,
])) {
if (! $this->isNames($paramType, [self::CONTAINER_CONFIGURATOR_CLASS, self::RECTOR_CONFIG_CLASS])) {
return null;
}

Expand All @@ -131,7 +132,11 @@ public function refactor(Node $node): ?Node
// 1. change import of sets to single sets() method call
$this->containerConfiguratorImportsMerger->merge($node);

$this->traverseNodesWithCallable($node->getStmts(), function (Node $node): ?MethodCall {
$this->traverseNodesWithCallable($node->getStmts(), function (Node $node): ?Node {
if ($node instanceof Variable && $this->isName($node, 'containerConfigurator')) {
return new Variable(self::RECTOR_CONFIG_VARIABLE);
}

// 2. call on rule
if ($node instanceof MethodCall) {
if ($this->containerConfiguratorCallAnalyzer->isMethodCallWithServicesSetConfiguredRectorRule($node)) {
Expand Down Expand Up @@ -213,8 +218,16 @@ private function refactorConfigureRuleMethodCall(MethodCall $methodCall): null|M

private function refactorParameterName(MethodCall $methodCall): ?MethodCall
{
$args = $methodCall->getArgs();
if ($this->valueResolver->isValue($args[0]->value, Option::PHP_VERSION_FEATURES)) {
$methodCall->var = new Variable(self::RECTOR_CONFIG_VARIABLE);
$methodCall->name = new Identifier('phpVersion');
$methodCall->args = [$args[1]];
return $methodCall;
}

foreach (self::PARAMETER_NAME_TO_METHOD_CALL_MAP as $parameterName => $methodName) {
if (! $this->isOptionWithTrue($methodCall->getArgs(), $parameterName)) {
if (! $this->isOptionWithTrue($args, $parameterName)) {
continue;
}

Expand Down

0 comments on commit 61fd4d8

Please sign in to comment.