Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public function createFromInput(InputInterface $input): Configuration
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, true);
}

$isPhpOnly = (bool) $input->getOption(Option::PHP);

// "--php" narrows the run the same way "--only" does
if ($isPhpOnly) {
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, true);
}

return new Configuration(
$isDryRun,
$showProgressBar,
Expand All @@ -121,6 +128,7 @@ public function createFromInput(InputInterface $input): Configuration
$levelOverflows,
$showRulesSummary,
$isComposerBased,
$isPhpOnly,
);
}

Expand Down
24 changes: 24 additions & 0 deletions src/Configuration/ConfigurationRuleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Rector\Contract\Rector\RectorInterface;
use Rector\ValueObject\Configuration;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration;

/**
* Modify available rector rules based on the configuration options
* @see \Rector\Tests\Configuration\ConfigurationRuleFilterTest
*/
final class ConfigurationRuleFilter
{
Expand Down Expand Up @@ -41,6 +43,10 @@ public function filter(array $rectors): array
return $this->filterComposerBased($rectors);
}

if ($this->configuration->isPhpOnly()) {
return $this->filterPhpOnly($rectors);
}

return $rectors;
}

Expand Down Expand Up @@ -86,6 +92,24 @@ private function filterComposerBased(array $rectors): array
return $activeRectors;
}

/**
* Keeps rules bound to a minimal PHP version, e.g. PHP upgrade rules
*
* @param list<RectorInterface> $rectors
* @return list<RectorInterface>
*/
private function filterPhpOnly(array $rectors): array
{
$activeRectors = [];
foreach ($rectors as $rector) {
if ($rector instanceof MinPhpVersionInterface) {
$activeRectors[] = $rector;
}
}

return $activeRectors;
}

/**
* @return string[]
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ final class Option
*/
public const string COMPOSER_BASED = 'composer-based';

/**
* Run only rules bound to a minimal PHP version
*/
public const string PHP = 'php';

/**
* @internal To filter files by specific suffix
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Console/ProcessConfigureDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public static function decorate(Command $command): void
'Run only rules bound to an installed composer package version'
);

$command->addOption(
Option::PHP,
null,
InputOption::VALUE_NONE,
'Run only PHP rules, e.g. rules bound to a minimal PHP version'
);

$command->addOption(
Option::ONLY_SUFFIX,
null,
Expand Down
6 changes: 6 additions & 0 deletions src/ValueObject/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __construct(
private array $levelOverflows = [],
private bool $showRulesSummary = false,
private bool $isComposerBased = false,
private bool $isPhpOnly = false,
) {
}

Expand All @@ -44,6 +45,11 @@ public function isComposerBased(): bool
return $this->isComposerBased;
}

public function isPhpOnly(): bool
{
return $this->isPhpOnly;
}

public function isDryRun(): bool
{
return $this->isDryRun;
Expand Down
56 changes: 56 additions & 0 deletions tests/Configuration/ConfigurationRuleFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Configuration;

use Rector\Configuration\ConfigurationRuleFilter;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\Php80\Rector\Class_\StringableForToStringRector;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\ValueObject\Configuration;

final class ConfigurationRuleFilterTest extends AbstractLazyTestCase
{
private ConfigurationRuleFilter $configurationRuleFilter;

protected function setUp(): void
{
parent::setUp();

$this->configurationRuleFilter = $this->make(ConfigurationRuleFilter::class);
}

public function testPhpOnlyKeepsMinPhpVersionRules(): void
{
$stringableForToStringRector = $this->make(StringableForToStringRector::class);
$removeDeadInstanceOfRector = $this->make(RemoveDeadInstanceOfRector::class);

$this->configurationRuleFilter->setConfiguration($this->createConfiguration(true));

$filteredRectors = $this->configurationRuleFilter->filter(
[$stringableForToStringRector, $removeDeadInstanceOfRector]
);

$this->assertSame([$stringableForToStringRector], $filteredRectors);
}

public function testWithoutPhpOnlyKeepsAllRules(): void
{
$stringableForToStringRector = $this->make(StringableForToStringRector::class);
$removeDeadInstanceOfRector = $this->make(RemoveDeadInstanceOfRector::class);

$this->configurationRuleFilter->setConfiguration($this->createConfiguration(false));

$filteredRectors = $this->configurationRuleFilter->filter(
[$stringableForToStringRector, $removeDeadInstanceOfRector]
);

$this->assertSame([$stringableForToStringRector, $removeDeadInstanceOfRector], $filteredRectors);
}

private function createConfiguration(bool $isPhpOnly): Configuration
{
return new Configuration(isPhpOnly: $isPhpOnly);
}
}
10 changes: 10 additions & 0 deletions tests/Parallel/Command/WorkerCommandLineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ public static function provideData(): Iterator
],
"'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --memory-limit='-1' --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi",
];

yield [
[
self::COMMAND => 'process',
Option::SOURCE => ['src'],
'--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME,
'--' . Option::PHP => true,
],
"'" . PHP_BINARY . "' '" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --php --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi",
];
}

private function cleanUpEmptyQuoteExpectedCommandOutput(string $result): string
Expand Down
Loading