Skip to content

Commit 766824e

Browse files
committed
Add support for rule customisation policies
1 parent 7199d5a commit 766824e

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ containing your desired options.
153153
| isRiskyAllowed | `bool` | `false` |
154154
| usingCache | `bool` | `true` |
155155
| customRules | `array` | `[]` |
156+
| ruleCustomisers | `RuleCustomisationPolicyInterface` or `null` | `null` |
156157

157158
```diff
158159
<?php

phpstan-baseline.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,61 @@
22

33
$ignoreErrors = [];
44
$ignoreErrors[] = [
5-
'message' => '#^Call to method __construct\\(\\) of internal class PhpCsFixer\\\\FixerFactory from outside its root namespace PhpCsFixer\\.$#',
5+
'rawMessage' => 'Access to constant VERSION of internal class PhpCsFixer\\Console\\Application from outside its root namespace PhpCsFixer.',
6+
'identifier' => 'classConstant.internalClass',
7+
'count' => 1,
8+
'path' => __DIR__ . '/src/Factory.php',
9+
];
10+
$ignoreErrors[] = [
11+
'rawMessage' => 'Comparison operation ">" between int<80100, 80599> and 80599 is always false.',
12+
'identifier' => 'greater.alwaysFalse',
13+
'count' => 1,
14+
'path' => __DIR__ . '/src/Ruleset/AbstractRuleset.php',
15+
];
16+
$ignoreErrors[] = [
17+
'rawMessage' => 'Call to method __construct() of internal class PhpCsFixer\\FixerFactory from outside its root namespace PhpCsFixer.',
618
'identifier' => 'method.internalClass',
719
'count' => 1,
820
'path' => __DIR__ . '/src/Test/FixerProvider.php',
921
];
1022
$ignoreErrors[] = [
11-
'message' => '#^Call to method __construct\\(\\) of internal class PhpCsFixer\\\\RuleSet\\\\RuleSet from outside its root namespace PhpCsFixer\\.$#',
23+
'rawMessage' => 'Call to method __construct() of internal class PhpCsFixer\\RuleSet\\RuleSet from outside its root namespace PhpCsFixer.',
1224
'identifier' => 'method.internalClass',
1325
'count' => 1,
1426
'path' => __DIR__ . '/src/Test/FixerProvider.php',
1527
];
1628
$ignoreErrors[] = [
17-
'message' => '#^Call to method getFixers\\(\\) of internal class PhpCsFixer\\\\FixerFactory from outside its root namespace PhpCsFixer\\.$#',
29+
'rawMessage' => 'Call to method getFixers() of internal class PhpCsFixer\\FixerFactory from outside its root namespace PhpCsFixer.',
1830
'identifier' => 'method.internalClass',
1931
'count' => 1,
2032
'path' => __DIR__ . '/src/Test/FixerProvider.php',
2133
];
2234
$ignoreErrors[] = [
23-
'message' => '#^Call to method getRules\\(\\) of internal class PhpCsFixer\\\\RuleSet\\\\RuleSet from outside its root namespace PhpCsFixer\\.$#',
35+
'rawMessage' => 'Call to method getRules() of internal class PhpCsFixer\\RuleSet\\RuleSet from outside its root namespace PhpCsFixer.',
2436
'identifier' => 'method.internalClass',
2537
'count' => 1,
2638
'path' => __DIR__ . '/src/Test/FixerProvider.php',
2739
];
2840
$ignoreErrors[] = [
29-
'message' => '#^Call to method registerBuiltInFixers\\(\\) of internal class PhpCsFixer\\\\FixerFactory from outside its root namespace PhpCsFixer\\.$#',
41+
'rawMessage' => 'Call to method registerBuiltInFixers() of internal class PhpCsFixer\\FixerFactory from outside its root namespace PhpCsFixer.',
3042
'identifier' => 'method.internalClass',
3143
'count' => 1,
3244
'path' => __DIR__ . '/src/Test/FixerProvider.php',
3345
];
3446
$ignoreErrors[] = [
35-
'message' => '#^Instantiation of internal class PhpCsFixer\\\\FixerFactory\\.$#',
47+
'rawMessage' => 'Instantiation of internal class PhpCsFixer\\FixerFactory.',
3648
'identifier' => 'new.internalClass',
3749
'count' => 1,
3850
'path' => __DIR__ . '/src/Test/FixerProvider.php',
3951
];
4052
$ignoreErrors[] = [
41-
'message' => '#^Instantiation of internal class PhpCsFixer\\\\RuleSet\\\\RuleSet\\.$#',
53+
'rawMessage' => 'Instantiation of internal class PhpCsFixer\\RuleSet\\RuleSet.',
4254
'identifier' => 'new.internalClass',
4355
'count' => 1,
4456
'path' => __DIR__ . '/src/Test/FixerProvider.php',
4557
];
4658
$ignoreErrors[] = [
47-
'message' => '#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEmpty\\(\\) with non\\-empty\\-array\\<string, PhpCsFixer\\\\Fixer\\\\FixerInterface\\> will always evaluate to false\\.$#',
59+
'rawMessage' => 'Call to static method PHPUnit\\Framework\\Assert::assertEmpty() with non-empty-array<string, PhpCsFixer\\Fixer\\FixerInterface> will always evaluate to false.',
4860
'identifier' => 'staticMethod.impossibleType',
4961
'count' => 1,
5062
'path' => __DIR__ . '/tests/Test/FixerProviderTest.php',

src/Factory.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Nexus\CsConfig\Ruleset\ConfigurableAllowedUnsupportedPhpVersionRulesetInterface;
1717
use Nexus\CsConfig\Ruleset\RulesetInterface;
1818
use PhpCsFixer\Config;
19+
use PhpCsFixer\Config\RuleCustomisationPolicyInterface;
1920
use PhpCsFixer\ConfigInterface;
21+
use PhpCsFixer\Console\Application;
2022
use PhpCsFixer\Finder;
2123
use PhpCsFixer\Fixer\FixerInterface;
2224
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
@@ -38,6 +40,7 @@ final class Factory
3840
* lineEnding: non-empty-string,
3941
* isRiskyAllowed: bool,
4042
* usingCache: bool,
43+
* ruleCustomisers: null|RuleCustomisationPolicyInterface,
4144
* rules: array<string, array<string, mixed>|bool>
4245
* } $options Array of resolved options
4346
*/
@@ -61,6 +64,7 @@ private function __construct(
6164
* lineEnding?: non-empty-string,
6265
* isRiskyAllowed?: bool,
6366
* usingCache?: bool,
67+
* ruleCustomisers?: null|RuleCustomisationPolicyInterface,
6468
* customRules?: array<string, array<string, mixed>|bool>
6569
* } $options
6670
*/
@@ -90,6 +94,7 @@ public static function create(RulesetInterface $ruleset, array $overrides = [],
9094
$options['lineEnding'] ??= "\n";
9195
$options['isRiskyAllowed'] ??= $ruleset->willAutoActivateIsRiskyAllowed();
9296
$options['usingCache'] ??= true;
97+
$options['ruleCustomisers'] ??= null;
9398
$options['rules'] = array_merge($ruleset->getRules(), $overrides, $options['customRules'] ?? []);
9499

95100
return new self($ruleset, $options);
@@ -169,11 +174,18 @@ private function invoke(array $overrides = []): ConfigInterface
169174

170175
// @todo v4.0.0 Cleanup
171176
if ($this->ruleset instanceof ConfigurableAllowedUnsupportedPhpVersionRulesetInterface) {
177+
/** @var Config $config */
172178
$config = $config->setUnsupportedPhpVersionAllowed(
173179
$this->ruleset->isUnsupportedPhpVersionAllowed(),
174180
);
175181
}
176182

183+
// @todo v4.0.0 Cleanup
184+
if (version_compare(Application::VERSION, '3.92.0', '>=')) {
185+
/** @var Config $config */
186+
$config = $config->setRuleCustomisationPolicy($this->options['ruleCustomisers']);
187+
}
188+
177189
return $config;
178190
}
179191
}

tests/FactoryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Nexus\CsConfig\Ruleset\Nexus81;
1818
use Nexus\CsConfig\Ruleset\RulesetInterface;
1919
use PhpCsFixer\Config;
20+
use PhpCsFixer\Config\RuleCustomisationPolicyInterface;
2021
use PhpCsFixer\Finder;
2122
use PHPUnit\Framework\Attributes\CoversClass;
2223
use PHPUnit\Framework\MockObject\MockObject;
@@ -91,14 +92,19 @@ public function testFactoryReturnsDefaultOptionsWhenNoOptionsGiven(): void
9192

9293
public function testFactoryConsumesPassedOptionsToIt(): void
9394
{
95+
$stub = self::createStub(RuleCustomisationPolicyInterface::class);
96+
$stub->method('getPolicyVersionForCache')->willReturn('v1');
97+
9498
$options = [
9599
'cacheFile' => __DIR__.'/../../build/.php-cs-fixer.cache',
96100
'format' => 'junit',
97101
'hideProgress' => true,
98102
'indent' => "\t",
99103
'lineEnding' => "\r\n",
100104
'usingCache' => false,
105+
'ruleCustomisers' => $stub,
101106
];
107+
/** @var Config $config */
102108
$config = Factory::create($this->mockRuleset(), [], $options)->forProjects();
103109

104110
self::assertSame($options['cacheFile'], $config->getCacheFile());
@@ -107,6 +113,7 @@ public function testFactoryConsumesPassedOptionsToIt(): void
107113
self::assertSame($options['indent'], $config->getIndent());
108114
self::assertSame($options['lineEnding'], $config->getLineEnding());
109115
self::assertFalse($config->getUsingCache());
116+
self::assertSame($stub, $config->getRuleCustomisationPolicy());
110117
}
111118

112119
public function testCreateForLibraryCreatesPreformattedLicense(): void

0 commit comments

Comments
 (0)