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/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function processFiles(
$fileDiffs = [];

$totalChanged = 0;
$totalChangeCount = 0;
foreach ($filePaths as $filePath) {
if ($preFileCallback !== null) {
$preFileCallback($filePath);
Expand All @@ -152,6 +153,7 @@ public function processFiles(
$currentFileDiff = $fileProcessResult->getFileDiff();
if ($currentFileDiff instanceof FileDiff) {
$fileDiffs[] = $currentFileDiff;
$totalChangeCount += count($currentFileDiff->getRectorChanges());
}

// progress bar on parallel handled on runParallel()
Expand All @@ -162,6 +164,12 @@ public function processFiles(
if ($fileProcessResult->hasChanged()) {
++$totalChanged;
}

// stop once the requested number of changes is reached, leaving the rest untouched
$maxChanges = $configuration->getMaxChanges();
if ($maxChanges !== null && $totalChangeCount >= $maxChanges) {
break;
}
} catch (Throwable $throwable) {
$this->changedFilesDetector->invalidateFile($filePath);

Expand Down
22 changes: 22 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\ValueObject\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Configuration\ConfigurationFactoryTest
Expand Down Expand Up @@ -97,6 +98,13 @@ public function createFromInput(InputInterface $input): Configuration
$isParallel = false;
}

$maxChanges = $this->resolveMaxChanges($input);

// a global change counter cannot be shared across parallel workers, so enforce the limit in a single process
if ($maxChanges !== null) {
$isParallel = false;
}

$memoryLimit = $this->resolveMemoryLimit($input);

$isReportingWithRealPath = SimpleParameterProvider::provideBoolParameter(Option::ABSOLUTE_FILE_PATH);
Expand Down Expand Up @@ -140,9 +148,23 @@ public function createFromInput(InputInterface $input): Configuration
$isComposerBased,
$isPhpOnly,
$filters,
$maxChanges,
);
}

private function resolveMaxChanges(InputInterface $input): ?int
{
$maxChanges = $input->getOption(Option::MAX_CHANGES);
if ($maxChanges === null) {
return null;
}

$maxChanges = (int) $maxChanges;
Assert::positiveInteger($maxChanges);

return $maxChanges;
}

private function shouldShowProgressBar(InputInterface $input, string $outputFormat): bool
{
$noProgressBar = (bool) $input->getOption(Option::NO_PROGRESS_BAR);
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ final class Option

public const string RULES_SUMMARY = 'rules-summary';

public const string MAX_CHANGES = 'max-changes';

public const string CONFIG = 'config';

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Console/ProcessConfigureDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,12 @@ public static function decorate(Command $command): void
InputOption::VALUE_NONE,
'Show summary of rules applied during the run.'
);

$command->addOption(
Option::MAX_CHANGES,
null,
InputOption::VALUE_REQUIRED,
'Stop after this many changes are made, leaving the rest untouched. Forces non-parallel run.'
);
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ public function __construct(
private bool $isComposerBased = false,
private bool $isPhpOnly = false,
private array $filters = [],
private ?int $maxChanges = null,
) {
}

public function getMaxChanges(): ?int
{
return $this->maxChanges;
}

public function isComposerBased(): bool
{
return $this->isComposerBased;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,25 @@ public function testOnlySuffixRunCachesUnderOwnScopeWithoutPoisoningFullRun(): v
$this->changedFilesDetector->setActiveScope(null, null);
$this->assertTrue($this->changedFilesDetector->hasFileChanged($filePath));
}

public function testMaxChangesStopsAfterLimit(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-max-changes.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);

$filePaths = [
__DIR__ . '/Source/WithTwoClosuresFirst.php',
__DIR__ . '/Source/WithTwoClosuresSecond.php',
];

// each file holds 2 closures = 2 changes; the first file alone reaches the limit of 2,
// so the run stops before touching the second file - proving changes are counted, not files
$processResult = $applicationFileProcessor->processFiles($filePaths, new Configuration(
isDryRun: true,
maxChanges: 2,
));

$this->assertCount(1, $processResult->getFileDiffs());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor\Source;

final class WithTwoClosuresFirst
{
public function run(): void
{
$first = function () {
return 1;
};

$second = function () {
return 2;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\ApplicationFileProcessor\Source;

final class WithTwoClosuresSecond
{
public function run(): void
{
$first = function () {
return 1;
};

$second = function () {
return 2;
};
}
}
11 changes: 11 additions & 0 deletions tests/Application/ApplicationFileProcessor/config-max-changes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->cacheDirectory(sys_get_temp_dir() . '/_rector_max_changes_test');
$rectorConfig->rule(ClosureToArrowFunctionRector::class);
};
Loading