diff --git a/README.md b/README.md index 05f5eb0..9fa0e00 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ $config->setFinder( return $config; ``` +> [!TIP] +> You can configure a parallel run of PHP CS Fixer by calling: +> ```php +> $factory->runInParallel(); +> ``` + ### Ibexa packages Create a `.php-cs-fixer.php` file in your project root directory with the following content: diff --git a/src/lib/PhpCsFixer/InternalConfigFactory.php b/src/lib/PhpCsFixer/InternalConfigFactory.php index 2035fd9..4ed924d 100644 --- a/src/lib/PhpCsFixer/InternalConfigFactory.php +++ b/src/lib/PhpCsFixer/InternalConfigFactory.php @@ -11,6 +11,8 @@ use Composer\InstalledVersions; use Ibexa\CodeStyle\PhpCsFixer\Sets\RuleSetInterface; use PhpCsFixer\ConfigInterface; +use PhpCsFixer\ParallelAwareConfigInterface; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; /** * Factory for Config instance that should be used for all internal Ibexa packages. @@ -24,6 +26,8 @@ final class InternalConfigFactory private RuleSetInterface $ruleSet; + private bool $runInParallel = false; + /** * @param array $rules */ @@ -46,6 +50,13 @@ public function getRuleSet(): RuleSetInterface return $this->ruleSet ??= $this->createRuleSetFromPackage(InstalledVersions::getRootPackage()); } + public function runInParallel(bool $runInParallel = true): self + { + $this->runInParallel = $runInParallel; + + return $this; + } + /** * @param array{name: string, version: string, pretty_version?: string} $package */ @@ -78,6 +89,10 @@ public function buildConfig(): ConfigInterface $this->customRules, )); + if ($this->runInParallel && $config instanceof ParallelAwareConfigInterface) { + $config->setParallelConfig(ParallelConfigFactory::detect()); + } + return $config; } diff --git a/tests/lib/PhpCsFixer/InternalConfigFactoryTest.php b/tests/lib/PhpCsFixer/InternalConfigFactoryTest.php index e85f80c..be2d4a9 100644 --- a/tests/lib/PhpCsFixer/InternalConfigFactoryTest.php +++ b/tests/lib/PhpCsFixer/InternalConfigFactoryTest.php @@ -11,6 +11,7 @@ use Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory; use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa46RuleSet; use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa50RuleSet; +use PhpCsFixer\ParallelAwareConfigInterface; use PHPUnit\Framework\TestCase; use ReflectionClass; use ReflectionMethod; @@ -40,6 +41,8 @@ protected function setUp(): void * * @param array{name: string, version: string, pretty_version?: string} $package * @param class-string $expectedRuleSetClass + * + * @throws \ReflectionException */ public function testVersionBasedRuleSetSelection( array $package, @@ -98,4 +101,25 @@ public function testWithRuleSet(): void self::assertSame($customRuleSet, $this->factory->getRuleSet()); } + + public function testRunInParallel(): void + { + // Note: sequential test instead of separate test cases on purpose + + // sanity check + /** @var ParallelAwareConfigInterface $config */ + $config = $this->factory->buildConfig(); + self::assertSame(1, $config->getParallelConfig()->getMaxProcesses()); + + $this->factory->runInParallel(); + /** @var ParallelAwareConfigInterface $config */ + $config = $this->factory->buildConfig(); + self::assertGreaterThan(1, $config->getParallelConfig()->getMaxProcesses()); + + // reset test + $this->factory->runInParallel(false); + /** @var ParallelAwareConfigInterface $config */ + $config = $this->factory->buildConfig(); + self::assertSame(1, $config->getParallelConfig()->getMaxProcesses()); + } }