Skip to content

Commit 507cfcf

Browse files
authored
Added syntax sugar to configure parallel run (#19)
For more details see #19 Key changes: * Added syntax sugar to configure parallel run for PHP CS Fixer * [Tests] Added coverage for configuring parallel run * [Doc] Documented parallel run configuration * [CS] Enabled parallel run in the local Ibexa Code Style config
1 parent c88854b commit 507cfcf

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
->files()->name('*.php');
1515

1616
$configFactory = new InternalConfigFactory();
17-
$configFactory->withRuleSet(new Sets\Ibexa50RuleSet());
17+
$configFactory->runInParallel()->withRuleSet(new Sets\Ibexa50RuleSet());
1818

1919
$config = $configFactory->buildConfig();
2020
$config->setFinder($finder);

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ $config->setFinder(
4343
return $config;
4444
```
4545

46+
> [!TIP]
47+
> You can configure a parallel run of PHP CS Fixer by either calling:
48+
> ```php
49+
> $factory->runInParallel();
50+
> ```
51+
> or
52+
> ```php
53+
> InternalConfigFactory::build(runInParallel: true)
54+
> ```
55+
4656
### Ibexa packages
4757
4858
Create a `.php-cs-fixer.php` file in your project root directory with the following content:

src/lib/PhpCsFixer/InternalConfigFactory.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Composer\InstalledVersions;
1212
use Ibexa\CodeStyle\PhpCsFixer\Sets\RuleSetInterface;
1313
use PhpCsFixer\ConfigInterface;
14+
use PhpCsFixer\ParallelAwareConfigInterface;
15+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
1416

1517
/**
1618
* Factory for Config instance that should be used for all internal Ibexa packages.
@@ -24,6 +26,8 @@ final class InternalConfigFactory
2426

2527
private RuleSetInterface $ruleSet;
2628

29+
private bool $runInParallel = false;
30+
2731
/**
2832
* @param array<string, mixed> $rules
2933
*/
@@ -46,6 +50,13 @@ public function getRuleSet(): RuleSetInterface
4650
return $this->ruleSet ??= $this->createRuleSetFromPackage(InstalledVersions::getRootPackage());
4751
}
4852

53+
public function runInParallel(bool $runInParallel = true): self
54+
{
55+
$this->runInParallel = $runInParallel;
56+
57+
return $this;
58+
}
59+
4960
/**
5061
* @param array{name: string, version: string, pretty_version?: string} $package
5162
*/
@@ -78,11 +89,15 @@ public function buildConfig(): ConfigInterface
7889
$this->customRules,
7990
));
8091

92+
if ($this->runInParallel && $config instanceof ParallelAwareConfigInterface) {
93+
$config->setParallelConfig(ParallelConfigFactory::detect());
94+
}
95+
8196
return $config;
8297
}
8398

84-
public static function build(): ConfigInterface
99+
public static function build(bool $runInParallel = false): ConfigInterface
85100
{
86-
return (new self())->buildConfig();
101+
return (new self())->runInParallel($runInParallel)->buildConfig();
87102
}
88103
}

tests/lib/PhpCsFixer/InternalConfigFactoryTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory;
1212
use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa46RuleSet;
1313
use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa50RuleSet;
14+
use PhpCsFixer\ParallelAwareConfigInterface;
1415
use PHPUnit\Framework\TestCase;
1516
use ReflectionClass;
1617
use ReflectionMethod;
@@ -40,6 +41,8 @@ protected function setUp(): void
4041
*
4142
* @param array{name: string, version: string, pretty_version?: string} $package
4243
* @param class-string $expectedRuleSetClass
44+
*
45+
* @throws \ReflectionException
4346
*/
4447
public function testVersionBasedRuleSetSelection(
4548
array $package,
@@ -98,4 +101,39 @@ public function testWithRuleSet(): void
98101

99102
self::assertSame($customRuleSet, $this->factory->getRuleSet());
100103
}
104+
105+
public function testRunInParallel(): void
106+
{
107+
// Note: sequential test instead of separate test cases on purpose
108+
109+
// sanity check
110+
/** @var ParallelAwareConfigInterface $config */
111+
$config = $this->factory->buildConfig();
112+
self::assertSame(1, $config->getParallelConfig()->getMaxProcesses());
113+
114+
$this->factory->runInParallel();
115+
/** @var ParallelAwareConfigInterface $config */
116+
$config = $this->factory->buildConfig();
117+
self::assertGreaterThan(1, $config->getParallelConfig()->getMaxProcesses());
118+
119+
// reset test
120+
$this->factory->runInParallel(false);
121+
/** @var ParallelAwareConfigInterface $config */
122+
$config = $this->factory->buildConfig();
123+
self::assertSame(1, $config->getParallelConfig()->getMaxProcesses());
124+
}
125+
126+
public function testBuildWithoutRunInParallel(): void
127+
{
128+
$config = InternalConfigFactory::build();
129+
self::assertInstanceOf(ParallelAwareConfigInterface::class, $config);
130+
self::assertSame(1, $config->getParallelConfig()->getMaxProcesses());
131+
}
132+
133+
public function testBuildWithRunInParallel(): void
134+
{
135+
$config = InternalConfigFactory::build(true);
136+
self::assertInstanceOf(ParallelAwareConfigInterface::class, $config);
137+
self::assertGreaterThan(1, $config->getParallelConfig()->getMaxProcesses());
138+
}
101139
}

0 commit comments

Comments
 (0)