mirror of https://github.com/rectorphp/rector.git
Move from SETS parameter to explicit import() (#6375)
This commit is contained in:
parent
0800c7cdc3
commit
ac82ae6a63
|
@ -112,17 +112,14 @@ And modify it:
|
|||
|
||||
```php
|
||||
// rector.php
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Php74\Rector\Property\TypedPropertyRector;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
// here we can define, what sets of rules will be applied
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
// tip: use "SetList" class to autocomplete sets
|
||||
$parameters->set(Option::SETS, [SetList::CODE_QUALITY]);
|
||||
$containerConfigurator->import(SetList::CODE_QUALITY);
|
||||
|
||||
// register single rule
|
||||
$services = $containerConfigurator->services();
|
||||
|
|
|
@ -16,13 +16,11 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan-for-downgrade.neon');
|
||||
|
||||
$parameters->set(Option::SETS, [
|
||||
DowngradeSetList::PHP_80,
|
||||
DowngradeSetList::PHP_74,
|
||||
DowngradeSetList::PHP_73,
|
||||
DowngradeSetList::PHP_72,
|
||||
DowngradeSetList::PHP_71,
|
||||
]);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_80);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_74);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_73);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_72);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_71);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,12 +25,10 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan-for-downgrade.neon');
|
||||
|
||||
$parameters->set(Option::SETS, [
|
||||
DowngradeSetList::PHP_80,
|
||||
DowngradeSetList::PHP_74,
|
||||
DowngradeSetList::PHP_73,
|
||||
DowngradeSetList::PHP_72,
|
||||
]);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_80);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_74);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_73);
|
||||
$containerConfigurator->import(DowngradeSetList::PHP_72);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,8 +17,7 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
|
|||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
// here we can define, what sets of rules will be applied
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::SETS, [SetList::CODE_QUALITY]);
|
||||
$containerConfigurator->import(SetList::CODE_QUALITY);
|
||||
|
||||
// register single rule
|
||||
$services = $containerConfigurator->services();
|
||||
|
|
5
ecs.php
5
ecs.php
|
@ -52,7 +52,10 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
__DIR__ . '/config/set',
|
||||
]);
|
||||
|
||||
$parameters->set(Option::SETS, [SetList::PSR_12, SetList::SYMPLIFY, SetList::COMMON, SetList::CLEAN_CODE]);
|
||||
$containerConfigurator->import(SetList::PSR_12);
|
||||
$containerConfigurator->import(SetList::SYMPLIFY);
|
||||
$containerConfigurator->import(SetList::COMMON);
|
||||
$containerConfigurator->import(SetList::CLEAN_CODE);
|
||||
|
||||
$parameters->set(Option::SKIP, [
|
||||
'*/Source/*',
|
||||
|
|
36
rector.php
36
rector.php
|
@ -23,6 +23,25 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
|
|||
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::SETS, [SetList::NAMING]);
|
||||
|
||||
// include sets
|
||||
$containerConfigurator->import(SetList::CODING_STYLE);
|
||||
$containerConfigurator->import(SetList::CODE_QUALITY);
|
||||
$containerConfigurator->import(SetList::CODE_QUALITY_STRICT);
|
||||
$containerConfigurator->import(SetList::DEAD_CODE);
|
||||
$containerConfigurator->import(SetList::PRIVATIZATION);
|
||||
$containerConfigurator->import(SetList::NAMING);
|
||||
$containerConfigurator->import(SetList::TYPE_DECLARATION);
|
||||
$containerConfigurator->import(SetList::PHP_71);
|
||||
$containerConfigurator->import(SetList::PHP_72);
|
||||
$containerConfigurator->import(SetList::PHP_73);
|
||||
$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();
|
||||
|
||||
$configuration = ValueObjectInliner::inline([
|
||||
|
@ -42,23 +61,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
$parameters->set(Option::SETS, [
|
||||
SetList::CODING_STYLE,
|
||||
SetList::CODE_QUALITY,
|
||||
SetList::CODE_QUALITY_STRICT,
|
||||
SetList::DEAD_CODE,
|
||||
SetList::PRIVATIZATION,
|
||||
SetList::NAMING,
|
||||
SetList::TYPE_DECLARATION,
|
||||
SetList::PHP_71,
|
||||
SetList::PHP_72,
|
||||
SetList::PHP_73,
|
||||
SetList::EARLY_RETURN,
|
||||
SetList::TYPE_DECLARATION_STRICT,
|
||||
NetteSetList::NETTE_UTILS_CODE_QUALITY,
|
||||
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
|
||||
]);
|
||||
|
||||
$parameters->set(Option::PATHS, [
|
||||
__DIR__ . '/src',
|
||||
__DIR__ . '/rules',
|
||||
|
|
|
@ -99,6 +99,7 @@ final class Option
|
|||
public const AUTOLOAD_PATHS = 'autoload_paths';
|
||||
|
||||
/**
|
||||
* @deprecated Use $containerConfigurator->import() as it does the same job, just better and more explicit
|
||||
* @var string
|
||||
*/
|
||||
public const SETS = 'sets';
|
||||
|
@ -138,11 +139,6 @@ final class Option
|
|||
*/
|
||||
public const OPTION_CONFIG = 'config';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FIX = 'fix';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\Core\Console\Command;
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\Contract\Rector\RectorInterface;
|
||||
use Rector\PostRector\Contract\Rector\PostRectorInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
@ -12,8 +11,6 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symplify\PackageBuilder\Console\ShellCode;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ShowCommand extends Command
|
||||
{
|
||||
|
@ -22,11 +19,6 @@ final class ShowCommand extends Command
|
|||
*/
|
||||
private $symfonyStyle;
|
||||
|
||||
/**
|
||||
* @var ParameterProvider
|
||||
*/
|
||||
private $parameterProvider;
|
||||
|
||||
/**
|
||||
* @var RectorInterface[]
|
||||
*/
|
||||
|
@ -35,10 +27,9 @@ final class ShowCommand extends Command
|
|||
/**
|
||||
* @param RectorInterface[] $rectors
|
||||
*/
|
||||
public function __construct(SymfonyStyle $symfonyStyle, ParameterProvider $parameterProvider, array $rectors)
|
||||
public function __construct(SymfonyStyle $symfonyStyle, array $rectors)
|
||||
{
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
$this->rectors = $rectors;
|
||||
|
||||
parent::__construct();
|
||||
|
@ -52,7 +43,6 @@ final class ShowCommand extends Command
|
|||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->reportLoadedRectors();
|
||||
$this->reportLoadedSets();
|
||||
|
||||
return ShellCode::SUCCESS;
|
||||
}
|
||||
|
@ -83,28 +73,4 @@ final class ShowCommand extends Command
|
|||
$this->symfonyStyle->warning($warningMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private function reportLoadedSets(): void
|
||||
{
|
||||
$sets = (array) $this->parameterProvider->provideParameter(Option::SETS);
|
||||
if ($sets === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->symfonyStyle->newLine(2);
|
||||
$this->symfonyStyle->title('Loaded Sets');
|
||||
|
||||
sort($sets);
|
||||
|
||||
$setFilePaths = [];
|
||||
foreach ($sets as $set) {
|
||||
$setFileInfo = new SmartFileInfo($set);
|
||||
$setFilePaths[] = $setFileInfo->getRelativeFilePathFromCwd();
|
||||
}
|
||||
|
||||
$this->symfonyStyle->listing($setFilePaths);
|
||||
|
||||
$message = sprintf('%d loaded sets', count($sets));
|
||||
$this->symfonyStyle->success($message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\DependencyInjection\CompilerPass;
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
final class DeprecationWarningCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const DEPRECATED_PARAMETERS = [
|
||||
Option::SETS => 'Use $containerConfigurator->import(<set>); instead',
|
||||
];
|
||||
|
||||
public function process(ContainerBuilder $containerBuilder): void
|
||||
{
|
||||
$parametersBag = $containerBuilder->getParameterBag();
|
||||
|
||||
foreach (self::DEPRECATED_PARAMETERS as $parameter => $message) {
|
||||
if (! $parametersBag->has($parameter)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$setsParameters = $parametersBag->get($parameter);
|
||||
if ($setsParameters === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$message = sprintf('The "%s" parameter is deprecated. %s', $parameter, $message);
|
||||
trigger_error($message);
|
||||
// to make it noticable
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ namespace Rector\Core\HttpKernel;
|
|||
|
||||
use Rector\Core\Contract\Rector\RectorInterface;
|
||||
use Rector\Core\DependencyInjection\Collector\ConfigureCallValuesCollector;
|
||||
use Rector\Core\DependencyInjection\CompilerPass\DeprecationWarningCompilerPass;
|
||||
use Rector\Core\DependencyInjection\CompilerPass\MakeRectorsPublicCompilerPass;
|
||||
use Rector\Core\DependencyInjection\CompilerPass\MergeImportedRectorConfigureCallValuesCompilerPass;
|
||||
use Rector\Core\DependencyInjection\CompilerPass\RemoveSkippedRectorsCompilerPass;
|
||||
|
@ -108,6 +109,8 @@ final class RectorKernel extends Kernel
|
|||
$containerBuilder->addCompilerPass(new AutowireInterfacesCompilerPass([RectorInterface::class]));
|
||||
$containerBuilder->addCompilerPass(new MakeRectorsPublicCompilerPass());
|
||||
|
||||
$containerBuilder->addCompilerPass(new DeprecationWarningCompilerPass());
|
||||
|
||||
// add all merged arguments of Rector services
|
||||
$containerBuilder->addCompilerPass(
|
||||
new MergeImportedRectorConfigureCallValuesCompilerPass($this->configureCallValuesCollector)
|
||||
|
|
|
@ -55,8 +55,7 @@ final class MissingRectorRulesReporter
|
|||
$this->symfonyStyle->newLine(1);
|
||||
|
||||
$this->symfonyStyle->title('2. Add set of rules to "rector.php"');
|
||||
$this->symfonyStyle->writeln(' $parameters = $containerConfigurator->parameters();');
|
||||
$this->symfonyStyle->writeln(' $parameters->set(Option::SETS, [...]);');
|
||||
$this->symfonyStyle->writeln(' $containerConfigurator->import(SetList::...);');
|
||||
$this->symfonyStyle->newLine(1);
|
||||
|
||||
$this->symfonyStyle->title('Missing "rector.php" in your project? Let Rector create it for you');
|
||||
|
|
|
@ -12,9 +12,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
// Define what rule sets will be applied
|
||||
$parameters->set(Option::SETS, [
|
||||
SetList::DEAD_CODE,
|
||||
]);
|
||||
$containerConfigurator->import(SetList::DEAD_CODE);
|
||||
|
||||
// get services (needed for register a single rule)
|
||||
// $services = $containerConfigurator->services();
|
||||
|
|
|
@ -79,13 +79,13 @@ final class ConfigurableRectorImportConfigCallsMergeTest extends AbstractRectorT
|
|||
|
||||
yield [
|
||||
__DIR__ . '/config/one_set_with_own_rename.php', [
|
||||
'Old' => 'New',
|
||||
'PHPUnit_Framework_MockObject_Stub' => 'PHPUnit\Framework\MockObject\Stub',
|
||||
'PHPUnit_Framework_MockObject_Stub_Return' => 'PHPUnit\Framework\MockObject\Stub\ReturnStub',
|
||||
'PHPUnit_Framework_MockObject_Matcher_Parameters' => 'PHPUnit\Framework\MockObject\Matcher\Parameters',
|
||||
'PHPUnit_Framework_MockObject_Matcher_Invocation' => 'PHPUnit\Framework\MockObject\Matcher\Invocation',
|
||||
'PHPUnit_Framework_MockObject_MockObject' => 'PHPUnit\Framework\MockObject\MockObject',
|
||||
'PHPUnit_Framework_MockObject_Invocation_Object' => 'PHPUnit\Framework\MockObject\Invocation\ObjectInvocation',
|
||||
'Old' => 'New',
|
||||
],
|
||||
];
|
||||
|
||||
|
@ -105,6 +105,7 @@ final class ConfigurableRectorImportConfigCallsMergeTest extends AbstractRectorT
|
|||
|
||||
yield [
|
||||
__DIR__ . '/config/two_sets_with_own_rename.php', [
|
||||
'Old' => 'New',
|
||||
'Twig_SimpleFilter' => 'Twig_Filter',
|
||||
'Twig_SimpleFunction' => 'Twig_Function',
|
||||
'Twig_SimpleTest' => 'Twig_Test',
|
||||
|
@ -114,7 +115,6 @@ final class ConfigurableRectorImportConfigCallsMergeTest extends AbstractRectorT
|
|||
'PHPUnit_Framework_MockObject_Matcher_Invocation' => 'PHPUnit\Framework\MockObject\Matcher\Invocation',
|
||||
'PHPUnit_Framework_MockObject_MockObject' => 'PHPUnit\Framework\MockObject\MockObject',
|
||||
'PHPUnit_Framework_MockObject_Invocation_Object' => 'PHPUnit\Framework\MockObject\Invocation\ObjectInvocation',
|
||||
'Old' => 'New',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -2,12 +2,9 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
$parameters->set(Option::SETS, [SetList::PHPUNIT_60]);
|
||||
$containerConfigurator->import(SetList::PHPUNIT_60);
|
||||
};
|
||||
|
|
|
@ -2,15 +2,12 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Renaming\Rector\Name\RenameClassRector;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
$parameters->set(Option::SETS, [SetList::PHPUNIT_60]);
|
||||
$containerConfigurator->import(SetList::PHPUNIT_60);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(RenameClassRector::class)
|
||||
|
|
|
@ -2,12 +2,10 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
$parameters->set(Option::SETS, [SetList::PHPUNIT_60, SetList::TWIG_20]);
|
||||
$containerConfigurator->import(SetList::PHPUNIT_60);
|
||||
$containerConfigurator->import(SetList::TWIG_20);
|
||||
};
|
||||
|
|
|
@ -2,15 +2,14 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\PHPUnit\Set\PHPUnitSetList;
|
||||
use Rector\Renaming\Rector\Name\RenameClassRector;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
use Rector\Symfony\Set\TwigSetList;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
$parameters->set(Option::SETS, [SetList::PHPUNIT_60, SetList::TWIG_20]);
|
||||
$containerConfigurator->import(PHPUnitSetList::PHPUNIT_60);
|
||||
$containerConfigurator->import(TwigSetList::TWIG_20);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(RenameClassRector::class)
|
||||
|
|
Loading…
Reference in New Issue