Add RectorConfigProvider to ask for configuration behave for 3rd party packages (#2187)

* add note about deprecated configure() method

* bump rector config to ruleWithConfiguration()

* remove ParameterProvider from StaticEasyPrefixer, as requires complete alias cleanup messed with scoped

* add RectorConfigProvider
This commit is contained in:
Tomas Votruba 2022-04-28 19:13:39 +02:00 committed by GitHub
parent 8272c42bfa
commit b055a88927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 12 deletions

View File

@ -13,10 +13,12 @@ return static function (RectorConfig $rectorConfig): void {
__DIR__ . '/src',
]);
$services = $rectorConfig->services();
$services->set(RenameClassRector::class)
->configure(['DateTime' => 'DateTimeInterface']);
$services->set(DowngradeAttributeToAnnotationRector::class)
->configure([new DowngradeAttributeToAnnotation('Symfony\Component\Routing\Annotation\Route')]);
$services->set(RemoveUselessVarTagRector::class);
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'DateTime' => 'DateTimeInterface'
]);
$rectorConfig->ruleWithConfiguration(DowngradeAttributeToAnnotationRector::class, [
new DowngradeAttributeToAnnotation('Symfony\Component\Routing\Annotation\Route')
]);
$rectorConfig->rule(RemoveUselessVarTagRector::class);
};

View File

@ -13,13 +13,12 @@ use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Use_;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
use Rector\Renaming\NodeManipulator\ClassRenamer;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
@ -32,7 +31,7 @@ final class RenameClassRector extends AbstractRector implements ConfigurableRect
public function __construct(
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly ClassRenamer $classRenamer,
private readonly ParameterProvider $parameterProvider,
private readonly RectorConfigProvider $rectorConfigProvider,
) {
}
@ -101,7 +100,7 @@ CODE_SAMPLE
return $this->classRenamer->renameNode($node, $oldToNewClasses, $this->file);
}
if (! $this->parameterProvider->provideBoolParameter(Option::AUTO_IMPORT_NAMES)) {
if (! $this->rectorConfigProvider->shouldImportNames()) {
return null;
}

View File

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Configuration;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
/**
* Rector native configuration provider, to keep deprecated options hidden,
* but also provide configuration that custom rules can check
*/
final class RectorConfigProvider
{
public function __construct(
private readonly ParameterProvider $parameterProvider
) {
}
public function shouldImportNames(): bool
{
return $this->parameterProvider->provideBoolParameter(Option::AUTO_IMPORT_NAMES);
}
}

View File

@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ServiceConfigurato
final class RectorServiceConfigurator extends ServiceConfigurator
{
/**
* @deprecated Use @see \Rector\Config\RectorConfig instead
* @param mixed[] $configuration
*/
public function configure(array $configuration): self

View File

@ -21,8 +21,6 @@ final class StaticEasyPrefixer
'Helmich\TypoScriptParser\Parser\Traverser\Traverser',
// for usage in packages/Testing/PHPUnit/PlatformAgnosticAssertions.php
'PHPUnit\Framework\Constraint\IsEqual',
// part of public API
'Symplify\PackageBuilder\Parameter\ParameterProvider',
];
/**