mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
[DX] Remove buggy --only option to complete --set removal; use PHP config with reliable autocomplete (#4701)
This commit is contained in:
parent
0963b8dca7
commit
054baf96d1
@ -13,7 +13,8 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
package_name:
|
||||
- symfony-php-config
|
||||
# @todo move to simplify as well
|
||||
- simple-php-doc-parser
|
||||
|
||||
name: Monorepo Split of ${{ matrix.package_name }}
|
||||
|
||||
|
@ -12,7 +12,6 @@ use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
|
||||
use Rector\Core\Configuration\Configuration;
|
||||
use Rector\Core\EventDispatcher\Event\AfterProcessEvent;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Testing\Application\EnabledRectorsProvider;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
@ -78,11 +77,6 @@ final class RectorApplication
|
||||
*/
|
||||
private $removedAndAddedFilesProcessor;
|
||||
|
||||
/**
|
||||
* @var EnabledRectorsProvider
|
||||
*/
|
||||
private $enabledRectorsProvider;
|
||||
|
||||
/**
|
||||
* @var NodeScopeResolver
|
||||
*/
|
||||
@ -100,7 +94,6 @@ final class RectorApplication
|
||||
|
||||
public function __construct(
|
||||
Configuration $configuration,
|
||||
EnabledRectorsProvider $enabledRectorsProvider,
|
||||
ErrorAndDiffCollector $errorAndDiffCollector,
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
FileProcessor $fileProcessor,
|
||||
@ -116,7 +109,6 @@ final class RectorApplication
|
||||
$this->fileProcessor = $fileProcessor;
|
||||
$this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector;
|
||||
$this->removedAndAddedFilesProcessor = $removedAndAddedFilesProcessor;
|
||||
$this->enabledRectorsProvider = $enabledRectorsProvider;
|
||||
$this->nodeScopeResolver = $nodeScopeResolver;
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
$this->privatesAccessor = $privatesAccessor;
|
||||
@ -137,12 +129,6 @@ final class RectorApplication
|
||||
// PHPStan has to know about all files!
|
||||
$this->configurePHPStanNodeScopeResolver($phpFileInfos);
|
||||
|
||||
// active only one rule
|
||||
if ($this->configuration->getOnlyRector() !== null) {
|
||||
$onlyRector = $this->configuration->getOnlyRector();
|
||||
$this->enabledRectorsProvider->addEnabledRector($onlyRector);
|
||||
}
|
||||
|
||||
// 1. parse files to nodes
|
||||
$this->parseFileInfosToNodes($phpFileInfos);
|
||||
|
||||
|
@ -76,11 +76,6 @@ final class Configuration
|
||||
*/
|
||||
private $ciDetector;
|
||||
|
||||
/**
|
||||
* @var OnlyRuleResolver
|
||||
*/
|
||||
private $onlyRuleResolver;
|
||||
|
||||
/**
|
||||
* @var ParameterProvider
|
||||
*/
|
||||
@ -96,21 +91,12 @@ final class Configuration
|
||||
*/
|
||||
private $configFileInfo;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $onlyRector;
|
||||
|
||||
public function __construct(
|
||||
CiDetector $ciDetector,
|
||||
OnlyRuleResolver $onlyRuleResolver,
|
||||
ParameterProvider $parameterProvider
|
||||
) {
|
||||
public function __construct(CiDetector $ciDetector, ParameterProvider $parameterProvider)
|
||||
{
|
||||
$this->ciDetector = $ciDetector;
|
||||
$this->isCacheEnabled = (bool) $parameterProvider->provideParameter(Option::ENABLE_CACHE);
|
||||
$this->fileExtensions = (array) $parameterProvider->provideParameter(Option::FILE_EXTENSIONS);
|
||||
$this->paths = (array) $parameterProvider->provideParameter(Option::PATHS);
|
||||
$this->onlyRuleResolver = $onlyRuleResolver;
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
}
|
||||
|
||||
@ -131,12 +117,6 @@ final class Configuration
|
||||
|
||||
$this->outputFormat = (string) $input->getOption(Option::OPTION_OUTPUT_FORMAT);
|
||||
|
||||
/** @var string|null $onlyRector */
|
||||
$onlyRector = $input->getOption(Option::OPTION_ONLY);
|
||||
if ($onlyRector !== null) {
|
||||
$this->setOnlyRector($onlyRector);
|
||||
}
|
||||
|
||||
$commandLinePaths = (array) $input->getArgument(Option::SOURCE);
|
||||
// manual command line value has priority
|
||||
if ($commandLinePaths !== []) {
|
||||
@ -212,11 +192,6 @@ final class Configuration
|
||||
return $this->mustMatchGitDiff;
|
||||
}
|
||||
|
||||
public function getOnlyRector(): ?string
|
||||
{
|
||||
return $this->onlyRector;
|
||||
}
|
||||
|
||||
public function getOutputFile(): ?string
|
||||
{
|
||||
return $this->outputFile;
|
||||
@ -317,9 +292,4 @@ final class Configuration
|
||||
|
||||
return $outputFileOption;
|
||||
}
|
||||
|
||||
private function setOnlyRector(string $rector): void
|
||||
{
|
||||
$this->onlyRector = $this->onlyRuleResolver->resolve($rector);
|
||||
}
|
||||
}
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Configuration;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use Rector\Core\Exception\Configuration\RectorRuleNotFoundException;
|
||||
|
||||
/**
|
||||
* @see \Rector\Core\Tests\Configuration\OnlyRuleResolverTest
|
||||
*/
|
||||
final class OnlyRuleResolver
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/WOQuBL/1
|
||||
*/
|
||||
private const SLASH_REGEX = '#\\\\#';
|
||||
|
||||
/**
|
||||
* @var RectorClassesProvider
|
||||
*/
|
||||
private $rectorClassesProvider;
|
||||
|
||||
public function __construct(RectorClassesProvider $rectorClassesProvider)
|
||||
{
|
||||
$this->rectorClassesProvider = $rectorClassesProvider;
|
||||
}
|
||||
|
||||
public function resolve(string $rule): string
|
||||
{
|
||||
$rule = ltrim($rule, '\\');
|
||||
|
||||
$rectorClasses = $this->rectorClassesProvider->provide();
|
||||
|
||||
// 1. exact class
|
||||
foreach ($rectorClasses as $rectorClass) {
|
||||
if (is_a($rectorClass, $rule, true)) {
|
||||
return $rule;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. short class
|
||||
foreach ($rectorClasses as $rectorClass) {
|
||||
if (Strings::endsWith($rectorClass, '\\' . $rule)) {
|
||||
return $rectorClass;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. class without slashes
|
||||
foreach ($rectorClasses as $rectorClass) {
|
||||
$rectorClassWithoutSlashes = Strings::replace($rectorClass, self::SLASH_REGEX);
|
||||
if ($rectorClassWithoutSlashes === $rule) {
|
||||
return $rectorClass;
|
||||
}
|
||||
}
|
||||
|
||||
$message = sprintf(
|
||||
'Rule "%s" was not found.%sMake sure it is registered in your config or in one of the sets',
|
||||
$rule,
|
||||
PHP_EOL
|
||||
);
|
||||
throw new RectorRuleNotFoundException($message);
|
||||
}
|
||||
}
|
@ -36,11 +36,6 @@ final class Option
|
||||
*/
|
||||
public const PHP_VERSION_FEATURES = 'php_version_features';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const OPTION_ONLY = 'only';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -162,13 +162,6 @@ final class ProcessCommand extends AbstractCommand
|
||||
'Execute only on file(s) matching the git diff.'
|
||||
);
|
||||
|
||||
$this->addOption(
|
||||
Option::OPTION_ONLY,
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Run only one single Rector from the loaded Rectors (in services, sets, etc).'
|
||||
);
|
||||
|
||||
$names = $this->outputFormatterCollector->getNames();
|
||||
|
||||
$description = sprintf('Select output format: "%s".', implode('", "', $names));
|
||||
|
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Configuration;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Configuration\OnlyRuleResolver;
|
||||
use Rector\Core\HttpKernel\RectorKernel;
|
||||
use Rector\Core\Tests\Configuration\Source\CustomLocalRector;
|
||||
use Rector\Php72\Rector\Assign\ListEachRector;
|
||||
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
|
||||
|
||||
final class OnlyRuleResolverTest extends AbstractKernelTestCase
|
||||
{
|
||||
/**
|
||||
* @var OnlyRuleResolver
|
||||
*/
|
||||
private $onlyRuleResolver;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->bootKernelWithConfigs(RectorKernel::class, [__DIR__ . '/Source/config/some_config.php']);
|
||||
$this->onlyRuleResolver = self::$container->get(OnlyRuleResolver::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $rule, string $expectedRectorClass): void
|
||||
{
|
||||
$resolveRectorClass = $this->onlyRuleResolver->resolve($rule);
|
||||
$this->assertSame($expectedRectorClass, $resolveRectorClass);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
yield [ListEachRector::class, ListEachRector::class];
|
||||
yield ['ListEachRector', ListEachRector::class];
|
||||
yield [CustomLocalRector::class, CustomLocalRector::class];
|
||||
yield ['CustomLocalRector', CustomLocalRector::class];
|
||||
|
||||
// miss-formats
|
||||
yield ['\\' . ListEachRector::class, ListEachRector::class];
|
||||
yield ['RectorCoreTestsConfigurationSourceCustomLocalRector', CustomLocalRector::class];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user