Skip to content

Commit

Permalink
Move from SETS parameter to explicit import() (#6375)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored May 6, 2021
1 parent 0800c7c commit ac82ae6
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 101 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 5 additions & 7 deletions build/config/config-downgrade-php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

$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);
};

/**
Expand Down
10 changes: 4 additions & 6 deletions build/config/config-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

$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);
};

/**
Expand Down
3 changes: 1 addition & 2 deletions docs/init_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
__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/*',
Expand Down
36 changes: 19 additions & 17 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@
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([
Expand All @@ -42,23 +61,6 @@

$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',
Expand Down
6 changes: 1 addition & 5 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -138,11 +139,6 @@ final class Option
*/
public const OPTION_CONFIG = 'config';

/**
* @var string
*/
public const FIX = 'fix';

/**
* @var string
*/
Expand Down
36 changes: 1 addition & 35 deletions src/Console/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

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;
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
{
Expand All @@ -22,11 +19,6 @@ final class ShowCommand extends Command
*/
private $symfonyStyle;

/**
* @var ParameterProvider
*/
private $parameterProvider;

/**
* @var RectorInterface[]
*/
Expand All @@ -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();
Expand All @@ -52,7 +43,6 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->reportLoadedRectors();
$this->reportLoadedSets();

return ShellCode::SUCCESS;
}
Expand Down Expand Up @@ -83,28 +73,4 @@ private function reportLoadedRectors(): void
$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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
3 changes: 3 additions & 0 deletions src/HttpKernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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;
Expand Down Expand Up @@ -108,6 +109,8 @@ protected function build(ContainerBuilder $containerBuilder): void
$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)
Expand Down
3 changes: 1 addition & 2 deletions src/Reporting/MissingRectorRulesReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function report(): void
$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');
Expand Down
4 changes: 1 addition & 3 deletions templates/rector.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function provideData(): Iterator

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',
],
];

Expand All @@ -105,6 +105,7 @@ public function provideData(): Iterator

yield [
__DIR__ . '/config/two_sets_with_own_rename.php', [
'Old' => 'New',
'Twig_SimpleFilter' => 'Twig_Filter',
'Twig_SimpleFunction' => 'Twig_Function',
'Twig_SimpleTest' => 'Twig_Test',
Expand All @@ -114,7 +115,6 @@ public function provideData(): Iterator
'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',
],
];
}
Expand Down
5 changes: 1 addition & 4 deletions tests/DependencyInjection/config/one_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
5 changes: 1 addition & 4 deletions tests/DependencyInjection/config/one_set_with_own_rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions tests/DependencyInjection/config/two_sets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Loading

0 comments on commit ac82ae6

Please sign in to comment.