Skip to content

Commit

Permalink
Revert BC break in Runner signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Feb 1, 2024
1 parent 96354b4 commit c471778
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
13 changes: 5 additions & 8 deletions src/Console/Command/FixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use PhpCsFixer\Error\ErrorsManager;
use PhpCsFixer\FixerFileProcessedEvent;
use PhpCsFixer\Runner\Runner;
use PhpCsFixer\Runner\RunnerConfig;
use PhpCsFixer\ToolInfoInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -302,20 +301,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);

$runner = new Runner(
new RunnerConfig(
$resolver->isDryRun(),
$resolver->shouldStopOnViolation(),
$resolver->getParallelConfig(),
$resolver->getConfigFile()
),
$finder,
$resolver->getFixers(),
$resolver->getDiffer(),
ProgressOutputType::NONE !== $progressType ? $this->eventDispatcher : null,
$this->errorsManager,
$resolver->getLinter(),
$resolver->isDryRun(),
$resolver->getCacheManager(),
$resolver->getDirectory()
$resolver->getDirectory(),
$resolver->shouldStopOnViolation(),
$resolver->getParallelConfig(),
$resolver->getConfigFile()
);

$this->eventDispatcher->addListener(FixerFileProcessedEvent::NAME, [$progressOutput, 'onFixerFileProcessed']);
Expand Down
13 changes: 6 additions & 7 deletions src/Console/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PhpCsFixer\FixerFileProcessedEvent;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
use PhpCsFixer\Runner\Runner;
use PhpCsFixer\Runner\RunnerConfig;
use PhpCsFixer\ToolInfoInterface;
use React\EventLoop\StreamSelectLoop;
use React\Socket\ConnectionInterface;
Expand Down Expand Up @@ -215,25 +214,25 @@ private function createRunner(InputInterface $input): Runner
'using-cache' => $input->getOption('using-cache'),
'cache-file' => $input->getOption('cache-file'),
'diff' => $input->getOption('diff'),
'stop-on-violation' => false, // @TODO Pass this option to the runner
],
getcwd(),
$this->toolInfo
);

return new Runner(
new RunnerConfig(
$this->configurationResolver->isDryRun(),
false, // @TODO Pass this option to the runner
ParallelConfig::sequential() // IMPORTANT! Worker must run in sequential mode
),
null, // Paths are known when parallelisation server requests new chunk, not now
$this->configurationResolver->getFixers(),
$this->configurationResolver->getDiffer(),
$this->eventDispatcher,
$this->errorsManager,
$this->configurationResolver->getLinter(),
$this->configurationResolver->isDryRun(),
$this->configurationResolver->getCacheManager(),
$this->configurationResolver->getDirectory()
$this->configurationResolver->getDirectory(),
$this->configurationResolver->shouldStopOnViolation(),
ParallelConfig::sequential(), // IMPORTANT! Worker must run in sequential mode
$this->configurationResolver->getConfigFile()
);
}
}
60 changes: 36 additions & 24 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PhpCsFixer\Linter\LinterInterface;
use PhpCsFixer\Linter\LintingException;
use PhpCsFixer\Linter\LintingResultInterface;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
use PhpCsFixer\Runner\Parallel\ParallelisationException;
use PhpCsFixer\Runner\Parallel\Process;
use PhpCsFixer\Runner\Parallel\ProcessIdentifier;
Expand All @@ -46,13 +47,9 @@
* @author Greg Korba <greg@codito.dev>
*
* @phpstan-type _RunResult array<string, array{appliedFixers: list<string>, diff: string}>
*
* @internal
*/
final class Runner
{
private RunnerConfig $runnerConfig;

private DifferInterface $differ;

private ?DirectoryInterface $directory;
Expand All @@ -63,10 +60,12 @@ final class Runner

private CacheManagerInterface $cacheManager;

private bool $isDryRun;

private LinterInterface $linter;

/**
* @var ?iterable<\SplFileInfo>
* @var ?\Traversable<\SplFileInfo>
*/
private $fileIterator;

Expand All @@ -77,31 +76,43 @@ final class Runner
*/
private array $fixers;

private bool $stopOnViolation;

private ParallelConfig $parallelConfig;

private ?string $configFile;

/**
* @param null|iterable<\SplFileInfo> $fileIterator
* @param list<FixerInterface> $fixers
* @param null|\Traversable<\SplFileInfo> $fileIterator
* @param list<FixerInterface> $fixers
*/
public function __construct(
RunnerConfig $runnerConfig,
?iterable $fileIterator,
?\Traversable $fileIterator,
array $fixers,
DifferInterface $differ,
?EventDispatcherInterface $eventDispatcher,
ErrorsManager $errorsManager,
LinterInterface $linter,
bool $isDryRun,
CacheManagerInterface $cacheManager,
?DirectoryInterface $directory = null
?DirectoryInterface $directory = null,
bool $stopOnViolation = false,
?ParallelConfig $parallelConfig = null,
?string $configFile = null
) {
$this->runnerConfig = $runnerConfig;
$this->fileCount = \count($fileIterator ?? []); // Required only for main process (calculating workers count)
$this->fileIterator = $fileIterator;
$this->fixers = $fixers;
$this->differ = $differ;
$this->eventDispatcher = $eventDispatcher;
$this->errorsManager = $errorsManager;
$this->linter = $linter;
$this->isDryRun = $isDryRun;
$this->cacheManager = $cacheManager;
$this->directory = $directory ?? new Directory('');
$this->stopOnViolation = $stopOnViolation;
$this->parallelConfig = $parallelConfig ?? ParallelConfig::sequential();
$this->configFile = $configFile;
}

/**
Expand All @@ -117,7 +128,7 @@ public function setFileIterator(iterable $fileIterator): void
*/
public function fix(): array
{
return $this->runnerConfig->getParallelConfig()->getMaxProcesses() > 1
return $this->parallelConfig->getMaxProcesses() > 1
? $this->fixParallel()
: $this->fixSequential();
}
Expand Down Expand Up @@ -148,7 +159,7 @@ private function fixParallel(): array
$fileChunk = function () use ($fileIterator): array {
$files = [];

while (\count($files) < $this->runnerConfig->getParallelConfig()->getFilesPerProcess()) {
while (\count($files) < $this->parallelConfig->getFilesPerProcess()) {
$current = $fileIterator->current();

if (null === $current) {
Expand Down Expand Up @@ -191,15 +202,20 @@ private function fixParallel(): array
});

$processesToSpawn = min(
$this->runnerConfig->getParallelConfig()->getMaxProcesses(),
(int) ceil($this->fileCount / $this->runnerConfig->getParallelConfig()->getFilesPerProcess())
$this->parallelConfig->getMaxProcesses(),
(int)ceil($this->fileCount / $this->parallelConfig->getFilesPerProcess())
);

for ($i = 0; $i < $processesToSpawn; ++$i) {
$identifier = ProcessIdentifier::create();
$process = Process::create(
$streamSelectLoop,
$this->runnerConfig,
new RunnerConfig(
$this->isDryRun,
$this->stopOnViolation,
$this->parallelConfig,
$this->configFile
),
$identifier,
$serverPort,
);
Expand Down Expand Up @@ -285,7 +301,7 @@ private function fixSequential(): array
$name = $this->directory->getRelativePathTo($file->__toString());
$changed[$name] = $fixInfo;

if ($this->runnerConfig->shouldStopOnViolation()) {
if ($this->stopOnViolation) {
break;
}
}
Expand Down Expand Up @@ -388,7 +404,7 @@ private function fixFile(\SplFileInfo $file, LintingResultInterface $lintingResu
return null;
}

if (!$this->runnerConfig->isDryRun()) {
if (!$this->isDryRun) {
$fileName = $file->getRealPath();

if (!file_exists($fileName)) {
Expand Down Expand Up @@ -469,14 +485,10 @@ private function getFileIterator(): LintingResultAwareFileIteratorInterface
throw new \RuntimeException('File iterator is not configured. Pass paths during Runner initialisation or set them after with `setFileIterator()`.');
}

$fileIterator = new \ArrayIterator(
$fileFilterIterator = new FileFilterIterator(
$this->fileIterator instanceof \IteratorAggregate
? $this->fileIterator->getIterator()
: $this->fileIterator
);

$fileFilterIterator = new FileFilterIterator(
$fileIterator,
: $this->fileIterator,
$this->eventDispatcher,
$this->cacheManager
);
Expand Down
17 changes: 9 additions & 8 deletions tests/Runner/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
use PhpCsFixer\Linter\Linter;
use PhpCsFixer\Linter\LinterInterface;
use PhpCsFixer\Linter\LintingResultInterface;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
use PhpCsFixer\Runner\Runner;
use PhpCsFixer\Runner\RunnerConfig;
use PhpCsFixer\Tests\TestCase;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -58,13 +56,13 @@ public function testThatFixSuccessfully(): void

$path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
$runner = new Runner(
new RunnerConfig(true, false, new ParallelConfig()),
Finder::create()->in($path),
$fixers,
new NullDiffer(),
null,
new ErrorsManager(),
$linter,
true,
new NullCacheManager(),
new Directory($path)
);
Expand All @@ -77,15 +75,16 @@ public function testThatFixSuccessfully(): void

$path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
$runner = new Runner(
new RunnerConfig(true, true, new ParallelConfig()),
Finder::create()->in($path),
$fixers,
new NullDiffer(),
null,
new ErrorsManager(),
$linter,
true,
new NullCacheManager(),
new Directory($path),
true,
);

$changed = $runner->fix();
Expand All @@ -104,7 +103,6 @@ public function testThatFixInvalidFileReportsToErrorManager(): void

$path = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..').\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'invalid';
$runner = new Runner(
new RunnerConfig(true, false, new ParallelConfig()),
Finder::create()->in($path),
[
new Fixer\ClassNotation\VisibilityRequiredFixer(),
Expand All @@ -114,7 +112,9 @@ public function testThatFixInvalidFileReportsToErrorManager(): void
null,
$errorsManager,
new Linter(),
new NullCacheManager()
true,
new NullCacheManager(),
new Directory($path)
);
$changed = $runner->fix();
$pathToInvalidFile = $path.\DIRECTORY_SEPARATOR.'somefile.php';
Expand Down Expand Up @@ -146,15 +146,16 @@ public function testThatDiffedFileIsPassedToDiffer(): void
];

$runner = new Runner(
new RunnerConfig(true, true, new ParallelConfig()),
Finder::create()->in($path),
$fixers,
$differ,
null,
new ErrorsManager(),
new Linter(),
true,
new NullCacheManager(),
new Directory($path)
new Directory($path),
true
);

$runner->fix();
Expand Down
6 changes: 2 additions & 4 deletions tests/Test/AbstractIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
use PhpCsFixer\Linter\LinterInterface;
use PhpCsFixer\Linter\ProcessLinter;
use PhpCsFixer\PhpunitConstraintIsIdenticalString\Constraint\IsIdenticalString;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
use PhpCsFixer\Runner\Runner;
use PhpCsFixer\Runner\RunnerConfig;
use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\WhitespacesFixerConfig;
Expand Down Expand Up @@ -257,13 +255,13 @@ protected function doTest(IntegrationCase $case): void
$errorsManager = new ErrorsManager();
$fixers = self::createFixers($case);
$runner = new Runner(
new RunnerConfig(false, false, new ParallelConfig()),
new \ArrayIterator([new \SplFileInfo($tmpFile)]),
$fixers,
new UnifiedDiffer(),
null,
$errorsManager,
$this->linter,
false,
new NullCacheManager()
);

Expand Down Expand Up @@ -317,13 +315,13 @@ protected function doTest(IntegrationCase $case): void
}

$runner = new Runner(
new RunnerConfig(false, false, new ParallelConfig()),
new \ArrayIterator([new \SplFileInfo($tmpFile)]),
array_reverse($fixers),
new UnifiedDiffer(),
null,
$errorsManager,
$this->linter,
false,
new NullCacheManager()
);

Expand Down

0 comments on commit c471778

Please sign in to comment.