diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index c9585edc44e..dac0b4ecb4e 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -14,10 +14,10 @@ use PhpCsFixer\Config; use PhpCsFixer\Finder; -use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; return (new Config()) - ->setParallelConfig(ParallelConfig::detect()) + ->setParallelConfig(ParallelConfigFactory::detect()) ->setRiskyAllowed(true) ->setRules([ '@PHP74Migration' => true, diff --git a/doc/usage.rst b/doc/usage.rst index 62871b66b0f..0439338dd1b 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -21,14 +21,14 @@ If you do not have config file, you can run following command to fix non-hidden, php php-cs-fixer.phar fix . -You can also fix files in parallel, utilising more CPU cores. You can do this by using config class that implements ``PhpCsFixer\Runner\Parallel\ParallelConfig\ParallelRunnerConfigInterface``, and use ``setParallelConfig()`` method. Recommended way is to utilise auto-detecting parallel configuration: +You can also fix files in parallel, utilising more CPU cores. You can do this by using config class that implements ``PhpCsFixer\Runner\Parallel\ParallelConfig\ParallelAwareConfigInterface``, and use ``setParallelConfig()`` method. Recommended way is to utilise auto-detecting parallel configuration: .. code-block:: php setParallelConfig(ParallelConfig::detect()) + ->setParallelConfig(ParallelConfigFactory::detect()) ; However, in some case you may want to fine-tune parallelisation with explicit values (e.g. in environments where auto-detection does not work properly and suggests more cores than it should): diff --git a/src/Cache/FileCacheManager.php b/src/Cache/FileCacheManager.php index 1e3f64dc135..91887e8017d 100644 --- a/src/Cache/FileCacheManager.php +++ b/src/Cache/FileCacheManager.php @@ -14,6 +14,8 @@ namespace PhpCsFixer\Cache; +use PhpCsFixer\Tokenizer\CodeHasher; + /** * Class supports caching information about state of fixing files. * @@ -136,6 +138,6 @@ private function writeCache(): void private function calcHash(string $content): string { - return md5($content); + return CodeHasher::calculateCodeHash($content); } } diff --git a/src/Config.php b/src/Config.php index 066d8d8409a..8c3d8dfd81a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -16,13 +16,14 @@ use PhpCsFixer\Fixer\FixerInterface; use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; /** * @author Fabien Potencier * @author Katsuhiro Ogawa * @author Dariusz Rumiński */ -class Config implements ConfigInterface, ParallelRunnerConfigInterface +class Config implements ConfigInterface, ParallelAwareConfigInterface { private string $cacheFile = '.php-cs-fixer.cache'; @@ -48,7 +49,7 @@ class Config implements ConfigInterface, ParallelRunnerConfigInterface private string $name; - private ?ParallelConfig $parallelRunnerConfig; + private ?ParallelConfig $parallelConfig; /** * @var null|string @@ -125,7 +126,9 @@ public function getName(): string public function getParallelConfig(): ParallelConfig { - return $this->parallelRunnerConfig ?? ParallelConfig::sequential(); + $this->parallelConfig ??= ParallelConfigFactory::sequential(); + + return $this->parallelConfig; } public function getPhpExecutable(): ?string @@ -201,7 +204,7 @@ public function setLineEnding(string $lineEnding): ConfigInterface public function setParallelConfig(ParallelConfig $config): ConfigInterface { - $this->parallelRunnerConfig = $config; + $this->parallelConfig = $config; return $this; } diff --git a/src/Console/Command/FixCommand.php b/src/Console/Command/FixCommand.php index 71d2e9bafd3..39a27ed7c43 100644 --- a/src/Console/Command/FixCommand.php +++ b/src/Console/Command/FixCommand.php @@ -214,7 +214,7 @@ protected function configure(): void new InputOption('format', '', InputOption::VALUE_REQUIRED, 'To output results in other formats.'), new InputOption('stop-on-violation', '', InputOption::VALUE_NONE, 'Stop execution on first violation.'), new InputOption('show-progress', '', InputOption::VALUE_REQUIRED, 'Type of progress indicator (none, dots).'), - new InputOption('sequential', 's', InputOption::VALUE_NONE, 'Enforce sequential analysis.'), + new InputOption('sequential', '', InputOption::VALUE_NONE, 'Enforce sequential analysis.'), ] ); } diff --git a/src/Console/Command/WorkerCommand.php b/src/Console/Command/WorkerCommand.php index 09c5131dbb2..0c71fcf60b9 100644 --- a/src/Console/Command/WorkerCommand.php +++ b/src/Console/Command/WorkerCommand.php @@ -21,7 +21,7 @@ use PhpCsFixer\Error\ErrorsManager; use PhpCsFixer\FixerFileProcessedEvent; use PhpCsFixer\Runner\Parallel\ParallelAction; -use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\Runner\Parallel\ParallelisationException; use PhpCsFixer\Runner\Parallel\ReadonlyCacheManager; use PhpCsFixer\Runner\Runner; @@ -77,43 +77,13 @@ protected function configure(): void { $this->setDefinition( [ - new InputOption( - 'port', - null, - InputOption::VALUE_REQUIRED, - 'Specifies parallelisation server\'s port.' - ), - new InputOption( - 'identifier', - null, - InputOption::VALUE_REQUIRED, - 'Specifies parallelisation process\' identifier.' - ), - new InputOption( - 'allow-risky', - '', - InputOption::VALUE_REQUIRED, - 'Are risky fixers allowed (can be `yes` or `no`).' - ), + new InputOption('port', null, InputOption::VALUE_REQUIRED, 'Specifies parallelisation server\'s port.'), + new InputOption('identifier', null, InputOption::VALUE_REQUIRED, 'Specifies parallelisation process\' identifier.'), + new InputOption('allow-risky', '', InputOption::VALUE_REQUIRED, 'Are risky fixers allowed (can be `yes` or `no`).'), new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a config file.'), - new InputOption( - 'dry-run', - '', - InputOption::VALUE_NONE, - 'Only shows which files would have been modified.' - ), - new InputOption( - 'rules', - '', - InputOption::VALUE_REQUIRED, - 'List of rules that should be run against configured paths.' - ), - new InputOption( - 'using-cache', - '', - InputOption::VALUE_REQUIRED, - 'Should cache be used (can be `yes` or `no`).' - ), + new InputOption('dry-run', '', InputOption::VALUE_NONE, 'Only shows which files would have been modified.'), + new InputOption('rules', '', InputOption::VALUE_REQUIRED, 'List of rules that should be run against configured paths.'), + new InputOption('using-cache', '', InputOption::VALUE_REQUIRED, 'Should cache be used (can be `yes` or `no`).'), new InputOption('cache-file', '', InputOption::VALUE_REQUIRED, 'The path to the cache file.'), new InputOption('diff', '', InputOption::VALUE_NONE, 'Prints diff for each file.'), ] @@ -182,7 +152,7 @@ function (ConnectionInterface $connection) use ($loop, $runner, $identifier): vo /** @var iterable $files */ $files = $json['files']; - foreach ($files as $i => $absolutePath) { + foreach ($files as $absolutePath) { $relativePath = $this->configurationResolver->getDirectory()->getRelativePathTo($absolutePath); // Reset events because we want to collect only those coming from analysed files chunk @@ -237,7 +207,7 @@ private function createRunner(InputInterface $input): Runner 'dry-run' => $input->getOption('dry-run'), 'rules' => $passedRules, 'path' => [], - 'path-mode' => ConfigurationResolver::PATH_MODE_OVERRIDE, + 'path-mode' => ConfigurationResolver::PATH_MODE_OVERRIDE, // IMPORTANT! WorkerCommand is called with file that already passed filtering, so here we can rely on PATH_MODE_OVERRIDE. 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file'), 'diff' => $input->getOption('diff'), @@ -258,7 +228,7 @@ private function createRunner(InputInterface $input): Runner new ReadonlyCacheManager($this->configurationResolver->getCacheManager()), $this->configurationResolver->getDirectory(), $this->configurationResolver->shouldStopOnViolation(), - ParallelConfig::sequential(), // IMPORTANT! Worker must run in sequential mode + ParallelConfigFactory::sequential(), // IMPORTANT! Worker must run in sequential mode. null, $this->configurationResolver->getConfigFile() ); diff --git a/src/Console/ConfigurationResolver.php b/src/Console/ConfigurationResolver.php index 7f8b7bb7d2e..2fbcf4fbe1f 100644 --- a/src/Console/ConfigurationResolver.php +++ b/src/Console/ConfigurationResolver.php @@ -35,10 +35,11 @@ use PhpCsFixer\FixerFactory; use PhpCsFixer\Linter\Linter; use PhpCsFixer\Linter\LinterInterface; -use PhpCsFixer\ParallelRunnerConfigInterface; +use PhpCsFixer\ParallelAwareConfigInterface; use PhpCsFixer\RuleSet\RuleSet; use PhpCsFixer\RuleSet\RuleSetInterface; use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\StdinFileInfo; use PhpCsFixer\ToolInfoInterface; use PhpCsFixer\Utils; @@ -281,9 +282,9 @@ public function getParallelConfig(): ParallelConfig { $config = $this->getConfig(); - return true !== $this->options['sequential'] && $config instanceof ParallelRunnerConfigInterface + return true !== $this->options['sequential'] && $config instanceof ParallelAwareConfigInterface ? $config->getParallelConfig() - : ParallelConfig::sequential(); + : ParallelConfigFactory::sequential(); } public function getConfigFile(): ?string @@ -966,6 +967,7 @@ private function isCachingAllowedForRuntime(): bool { return $this->toolInfo->isInstalledAsPhar() || $this->toolInfo->isInstalledByComposer() - || $this->toolInfo->isRunInsideDocker(); + || $this->toolInfo->isRunInsideDocker() + || filter_var(getenv('PHP_CS_FIXER_ENFORCE_CACHE'), FILTER_VALIDATE_BOOL); } } diff --git a/src/ParallelRunnerConfigInterface.php b/src/ParallelAwareConfigInterface.php similarity index 81% rename from src/ParallelRunnerConfigInterface.php rename to src/ParallelAwareConfigInterface.php index f6a0ce1c837..6716cf3ce1d 100644 --- a/src/ParallelRunnerConfigInterface.php +++ b/src/ParallelAwareConfigInterface.php @@ -19,9 +19,9 @@ /** * @author Greg Korba * - * @TODO 4.0 Include parallel runner config in main config + * @TODO 4.0 Include parallel runner config in main ConfigInterface */ -interface ParallelRunnerConfigInterface extends ConfigInterface +interface ParallelAwareConfigInterface extends ConfigInterface { public function getParallelConfig(): ParallelConfig; diff --git a/src/Runner/Parallel/ParallelConfig.php b/src/Runner/Parallel/ParallelConfig.php index 8f77e79b196..2cfa9acef7f 100644 --- a/src/Runner/Parallel/ParallelConfig.php +++ b/src/Runner/Parallel/ParallelConfig.php @@ -14,10 +14,6 @@ namespace PhpCsFixer\Runner\Parallel; -use Fidry\CpuCoreCounter\CpuCoreCounter; -use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder; -use Fidry\CpuCoreCounter\Finder\FinderRegistry; - /** * @author Greg Korba */ @@ -63,25 +59,4 @@ public function getProcessTimeout(): int { return $this->processTimeout; } - - public static function sequential(): self - { - return new self(1); - } - - /** - * @param positive-int $filesPerProcess - * @param positive-int $processTimeout - */ - public static function detect( - int $filesPerProcess = self::DEFAULT_FILES_PER_PROCESS, - int $processTimeout = self::DEFAULT_PROCESS_TIMEOUT - ): self { - $counter = new CpuCoreCounter([ - ...FinderRegistry::getDefaultLogicalFinders(), - new DummyCpuCoreFinder(1), - ]); - - return new self($counter->getCount(), $filesPerProcess, $processTimeout); - } } diff --git a/src/Runner/Parallel/ParallelConfigFactory.php b/src/Runner/Parallel/ParallelConfigFactory.php new file mode 100644 index 00000000000..01c44a0292b --- /dev/null +++ b/src/Runner/Parallel/ParallelConfigFactory.php @@ -0,0 +1,53 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Runner\Parallel; + +use Fidry\CpuCoreCounter\CpuCoreCounter; +use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder; +use Fidry\CpuCoreCounter\Finder\FinderRegistry; + +/** + * @author Dariusz Rumiński + */ +final class ParallelConfigFactory +{ + private function __construct() {} + + public static function sequential(): ParallelConfig + { + return new ParallelConfig(1); + } + + /** + * @param null|positive-int $filesPerProcess + * @param null|positive-int $processTimeout + */ + public static function detect( + ?int $filesPerProcess = null, + ?int $processTimeout = null + ): ParallelConfig { + $counter = new CpuCoreCounter([ + ...FinderRegistry::getDefaultLogicalFinders(), + new DummyCpuCoreFinder(1), + ]); + + return new ParallelConfig( + ...array_filter( + [$counter->getCount(), $filesPerProcess, $processTimeout], + static fn ($value): bool => null !== $value + ) + ); + } +} diff --git a/src/Runner/Parallel/ProcessFactory.php b/src/Runner/Parallel/ProcessFactory.php index a50f9680bcd..d737b5c3880 100644 --- a/src/Runner/Parallel/ProcessFactory.php +++ b/src/Runner/Parallel/ProcessFactory.php @@ -49,6 +49,8 @@ public function create( } /** + * @private + * * @return list */ public function getCommandArgs(int $serverPort, ProcessIdentifier $identifier, RunnerConfig $runnerConfig): array diff --git a/src/Runner/Runner.php b/src/Runner/Runner.php index c8280494c72..0b7bd84463f 100644 --- a/src/Runner/Runner.php +++ b/src/Runner/Runner.php @@ -34,6 +34,7 @@ use PhpCsFixer\Preg; use PhpCsFixer\Runner\Parallel\ParallelAction; use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\Runner\Parallel\ParallelisationException; use PhpCsFixer\Runner\Parallel\ProcessFactory; use PhpCsFixer\Runner\Parallel\ProcessIdentifier; @@ -122,7 +123,7 @@ public function __construct( $this->cacheManager = $cacheManager; $this->directory = $directory ?? new Directory(''); $this->stopOnViolation = $stopOnViolation; - $this->parallelConfig = $parallelConfig ?? ParallelConfig::sequential(); + $this->parallelConfig = $parallelConfig ?? ParallelConfigFactory::sequential(); $this->input = $input; $this->configFile = $configFile; } @@ -132,6 +133,8 @@ public function __construct( */ public function setFileIterator(iterable $fileIterator): void { + // @TODO consider to drop this method and make iterator parameter obligatory in constructor, + // more in https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7777/files#r1590447581 $this->fileIterator = $fileIterator; } @@ -166,13 +169,14 @@ private function fixParallel(): array } $processPool = new ProcessPool($server); + $maxFilesPerProcess = $this->parallelConfig->getFilesPerProcess(); $fileIterator = $this->getFilteringFileIterator(); $fileIterator->rewind(); - $fileChunk = function () use ($fileIterator): array { + $getFileChunk = static function () use ($fileIterator, $maxFilesPerProcess): array { $files = []; - while (\count($files) < $this->parallelConfig->getFilesPerProcess()) { + while (\count($files) < $maxFilesPerProcess) { $current = $fileIterator->current(); if (null === $current) { @@ -188,13 +192,13 @@ private function fixParallel(): array }; // [REACT] Handle worker's handshake (init connection) - $server->on('connection', static function (ConnectionInterface $connection) use ($processPool, $fileChunk): void { + $server->on('connection', static function (ConnectionInterface $connection) use ($processPool, $getFileChunk): void { $jsonInvalidUtf8Ignore = \defined('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0; $decoder = new Decoder($connection, true, 512, $jsonInvalidUtf8Ignore); $encoder = new Encoder($connection, $jsonInvalidUtf8Ignore); // [REACT] Bind connection when worker's process requests "hello" action (enables 2-way communication) - $decoder->on('data', static function (array $data) use ($processPool, $fileChunk, $decoder, $encoder): void { + $decoder->on('data', static function (array $data) use ($processPool, $getFileChunk, $decoder, $encoder): void { if (ParallelAction::RUNNER_HELLO !== $data['action']) { return; } @@ -202,16 +206,16 @@ private function fixParallel(): array $identifier = ProcessIdentifier::fromRaw($data['identifier']); $process = $processPool->getProcess($identifier); $process->bindConnection($decoder, $encoder); - $job = $fileChunk(); + $fileChunk = $getFileChunk(); - if (0 === \count($job)) { + if (0 === \count($fileChunk)) { $process->request(['action' => ParallelAction::WORKER_THANK_YOU]); $processPool->endProcessIfKnown($identifier); return; } - $process->request(['action' => ParallelAction::WORKER_RUN, 'files' => $job]); + $process->request(['action' => ParallelAction::WORKER_RUN, 'files' => $fileChunk]); }); }); @@ -237,7 +241,7 @@ private function fixParallel(): array $processPool->addProcess($identifier, $process); $process->start( // [REACT] Handle workers' responses (multiple actions possible) - function (array $workerResponse) use ($processPool, $process, $identifier, $fileChunk, &$changed): void { + function (array $workerResponse) use ($processPool, $process, $identifier, $getFileChunk, &$changed): void { // File analysis result (we want close-to-realtime progress with frequent cache savings) if (ParallelAction::RUNNER_RESULT === $workerResponse['action']) { $fileAbsolutePath = $workerResponse['file']; @@ -282,16 +286,16 @@ function (array $workerResponse) use ($processPool, $process, $identifier, $file if (ParallelAction::RUNNER_GET_FILE_CHUNK === $workerResponse['action']) { // Request another chunk of files, if still available - $job = $fileChunk(); + $fileChunk = $getFileChunk(); - if (0 === \count($job)) { + if (0 === \count($fileChunk)) { $process->request(['action' => ParallelAction::WORKER_THANK_YOU]); $processPool->endProcessIfKnown($identifier); return; } - $process->request(['action' => ParallelAction::WORKER_RUN, 'files' => $job]); + $process->request(['action' => ParallelAction::WORKER_RUN, 'files' => $fileChunk]); return; } diff --git a/tests/AutoReview/ProjectCodeTest.php b/tests/AutoReview/ProjectCodeTest.php index 70559bce1fe..470f9334233 100644 --- a/tests/AutoReview/ProjectCodeTest.php +++ b/tests/AutoReview/ProjectCodeTest.php @@ -566,7 +566,7 @@ public function testAllCodeContainSingleClassy(string $className): void $classyEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nextTokenOfKind); - self::assertNull($tokens->getNextNonWhitespace($classyEndIndex), sprintf('File for "%s" should only contains a single classy.', $className)); + self::assertNull($tokens->getNextMeaningfulToken($classyEndIndex), sprintf('File for "%s" should only contains a single classy.', $className)); } /** diff --git a/tests/Console/Command/FixCommandTest.php b/tests/Console/Command/FixCommandTest.php index 03d5f4c9917..47b32772097 100644 --- a/tests/Console/Command/FixCommandTest.php +++ b/tests/Console/Command/FixCommandTest.php @@ -84,7 +84,7 @@ public function testSequentialRun(): void \$config = require '{$pathToDistConfig}'; \$config->setRules(['header_comment' => ['header' => 'SEQUENTIAL!']]); - \$config->setParallelConfig(\\PhpCsFixer\\Runner\\Parallel\\ParallelConfig::sequential()); + \$config->setParallelConfig(\\PhpCsFixer\\Runner\\Parallel\\ParallelConfigFactory::sequential()); return \$config; PHP; diff --git a/tests/Console/Command/WorkerCommandTest.php b/tests/Console/Command/WorkerCommandTest.php index 7cb99db4e68..3e688594840 100644 --- a/tests/Console/Command/WorkerCommandTest.php +++ b/tests/Console/Command/WorkerCommandTest.php @@ -21,7 +21,7 @@ use PhpCsFixer\Console\Command\WorkerCommand; use PhpCsFixer\FixerFileProcessedEvent; use PhpCsFixer\Runner\Parallel\ParallelAction; -use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\Runner\Parallel\ParallelisationException; use PhpCsFixer\Runner\Parallel\ProcessFactory; use PhpCsFixer\Runner\Parallel\ProcessIdentifier; @@ -90,7 +90,7 @@ public function testWorkerCommunicatesWithTheServer(): void $process = new Process(implode(' ', $processFactory->getCommandArgs( $serverPort, // @phpstan-ignore-line $processIdentifier, - new RunnerConfig(true, false, ParallelConfig::sequential()) + new RunnerConfig(true, false, ParallelConfigFactory::sequential()) ))); /** diff --git a/tests/Console/ConfigurationResolverTest.php b/tests/Console/ConfigurationResolverTest.php index 06d1c3a4d91..2a390ad2dcf 100644 --- a/tests/Console/ConfigurationResolverTest.php +++ b/tests/Console/ConfigurationResolverTest.php @@ -32,6 +32,7 @@ use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\Tests\TestCase; use PhpCsFixer\Tokenizer\Tokens; use PhpCsFixer\ToolInfoInterface; @@ -60,7 +61,7 @@ public function testResolveParallelConfig(): void public function testDefaultParallelConfigFallbacksToSequential(): void { $parallelConfig = $this->createConfigurationResolver([])->getParallelConfig(); - $defaultParallelConfig = ParallelConfig::sequential(); + $defaultParallelConfig = ParallelConfigFactory::sequential(); self::assertSame($defaultParallelConfig->getMaxProcesses(), $parallelConfig->getMaxProcesses()); self::assertSame($defaultParallelConfig->getFilesPerProcess(), $parallelConfig->getFilesPerProcess()); diff --git a/tests/Runner/Parallel/ParallelConfigFactoryTest.php b/tests/Runner/Parallel/ParallelConfigFactoryTest.php new file mode 100644 index 00000000000..0fa0f72c80c --- /dev/null +++ b/tests/Runner/Parallel/ParallelConfigFactoryTest.php @@ -0,0 +1,51 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Runner\Parallel; + +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; +use PhpCsFixer\Tests\TestCase; + +/** + * @internal + * + * @covers \PhpCsFixer\Runner\Parallel\ParallelConfigFactory + * + * @TODO Test `detect()` method, but first discuss the best way to do it. + */ +final class ParallelConfigFactoryTest extends TestCase +{ + public function testSequentialConfigHasExactlyOneProcess(): void + { + $config = ParallelConfigFactory::sequential(); + + self::assertSame(1, $config->getMaxProcesses()); + } + + public function testDetectConfigurationWithoutParams(): void + { + $config = ParallelConfigFactory::detect(); + + self::assertSame(10, $config->getFilesPerProcess()); + self::assertSame(120, $config->getProcessTimeout()); + } + + public function testDetectConfigurationWithParams(): void + { + $config = ParallelConfigFactory::detect(22, 2_200); + + self::assertSame(22, $config->getFilesPerProcess()); + self::assertSame(2_200, $config->getProcessTimeout()); + } +} diff --git a/tests/Runner/Parallel/ParallelConfigTest.php b/tests/Runner/Parallel/ParallelConfigTest.php index 5e3128709c1..3c5cedff0db 100644 --- a/tests/Runner/Parallel/ParallelConfigTest.php +++ b/tests/Runner/Parallel/ParallelConfigTest.php @@ -21,8 +21,6 @@ * @internal * * @covers \PhpCsFixer\Runner\Parallel\ParallelConfig - * - * @TODO Test `detect()` method, but first discuss the best way to do it. */ final class ParallelConfigTest extends TestCase { @@ -60,19 +58,4 @@ public function testGettersAreReturningProperValues(): void self::assertSame(10, $config->getFilesPerProcess()); self::assertSame(120, $config->getProcessTimeout()); } - - public function testSequentialConfigHasExactlyOneProcess(): void - { - $config = ParallelConfig::sequential(); - - self::assertSame(1, $config->getMaxProcesses()); - } - - public function testDetectConfiguration(): void - { - $config = ParallelConfig::detect(1, 100); - - self::assertSame(1, $config->getFilesPerProcess()); - self::assertSame(100, $config->getProcessTimeout()); - } } diff --git a/tests/Runner/Parallel/ProcessFactoryTest.php b/tests/Runner/Parallel/ProcessFactoryTest.php index 0e1a41fed47..b49e73fbe1f 100644 --- a/tests/Runner/Parallel/ProcessFactoryTest.php +++ b/tests/Runner/Parallel/ProcessFactoryTest.php @@ -16,7 +16,7 @@ use PhpCsFixer\Console\Command\FixCommand; use PhpCsFixer\Preg; -use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\Runner\Parallel\ProcessFactory; use PhpCsFixer\Runner\Parallel\ProcessIdentifier; use PhpCsFixer\Runner\RunnerConfig; @@ -128,6 +128,6 @@ public static function provideCreateCases(): iterable private static function createRunnerConfig(bool $dryRun): RunnerConfig { - return new RunnerConfig($dryRun, false, ParallelConfig::sequential()); + return new RunnerConfig($dryRun, false, ParallelConfigFactory::sequential()); } } diff --git a/tests/Runner/Parallel/ProcessPoolTest.php b/tests/Runner/Parallel/ProcessPoolTest.php index cab6bccd13d..9dab7011e6f 100644 --- a/tests/Runner/Parallel/ProcessPoolTest.php +++ b/tests/Runner/Parallel/ProcessPoolTest.php @@ -15,7 +15,7 @@ namespace PhpCsFixer\Tests\Runner\Parallel; use PhpCsFixer\Console\Command\FixCommand; -use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixer\Runner\Parallel\ParallelisationException; use PhpCsFixer\Runner\Parallel\Process; use PhpCsFixer\Runner\Parallel\ProcessFactory; @@ -124,7 +124,7 @@ private function createProcess(ProcessIdentifier $identifier): Process new RunnerConfig( true, false, - ParallelConfig::sequential() + ParallelConfigFactory::sequential() ), $identifier, 10_000