Skip to content
Draft
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions src/lib/PhpCsFixer/InternalConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,6 +26,8 @@ final class InternalConfigFactory

private RuleSetInterface $ruleSet;

private bool $runInParallel = false;

/**
* @param array<string, mixed> $rules
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -78,6 +89,10 @@ public function buildConfig(): ConfigInterface
$this->customRules,
));

if ($this->runInParallel && $config instanceof ParallelAwareConfigInterface) {
$config->setParallelConfig(ParallelConfigFactory::detect());
}

return $config;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/lib/PhpCsFixer/InternalConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}
}