Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add Config\RuleSet::withRules() to allow overriding Config\Rules #891

Merged
merged 1 commit into from
Sep 18, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ For a full diff see [`5.16.0...main`][5.16.0...main].
- Extracted `Config\PhpVersion` as a value object ([#871]), by [@localheinz]
- Extracted `Config\Rules` as a value object ([#874]), by [@localheinz]
- Extracted `Config\Fixers` as a value object ([#883]), by [@localheinz]
- Added `Config\RuleSet::withRules()` to allow overriding `Config\Rules` ([#891]), by [@localheinz]

### Changed

Expand Down Expand Up @@ -1202,6 +1203,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#886]: https://github.com/ergebnis/php-cs-fixer-config/pull/886
[#887]: https://github.com/ergebnis/php-cs-fixer-config/pull/887
[#888]: https://github.com/ergebnis/php-cs-fixer-config/pull/888
[#891]: https://github.com/ergebnis/php-cs-fixer-config/pull/891

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ This will enable and configure the [`HeaderCommentFixer`](https://github.com/Fri

use Ergebnis\PhpCsFixer\Config;

$ruleSet = Config\RuleSet\Php82::create();

-$config = Config\Factory::fromRuleSet($ruleSet);
+$config = Config\Factory::fromRuleSet($ruleSet, Config\Rules::fromArray([
-$ruleSet = Config\RuleSet\Php82::create();
+$ruleSet = Config\RuleSet\Php82::create()->withRules(Config\Rules::fromArray([
+ 'mb_str_functions' => false,
+ 'strict_comparison' => false,
+]));
+]);

$config = Config\Factory::fromRuleSet($ruleSet);

$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
Expand Down
13 changes: 13 additions & 0 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public function rules(): Rules
return $this->rules;
}

/**
* Returns a new rule set with merged rules.
*/
public function withRules(Rules $rules): self
{
return new self(
$this->customFixers,
$this->name,
$this->phpVersion,
$this->rules->merge($rules),
);
}

/**
* Returns a new rule set with rules where the header_comment fixer is enabled to add a header.
*
Expand Down
40 changes: 40 additions & 0 deletions test/Unit/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ public function testCreateReturnsRuleSet(): void
self::assertSame($rules, $ruleSet->rules());
}

public function testWithRulesReturnsRuleSetWithMergedRules(): void
{
$faker = self::faker();

$rules = Rules::fromArray([
'foo' => true,
'bar' => false,
]);

$ruleSet = RuleSet::create(
Fixers::fromFixers(
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
),
Name::fromString($faker->word()),
PhpVersion::create(
PhpVersion\Major::fromInt($faker->numberBetween(0)),
PhpVersion\Minor::fromInt($faker->numberBetween(0, 99)),
PhpVersion\Patch::fromInt($faker->numberBetween(0, 99)),
),
Rules::fromArray([
'foo' => false,
'quz' => true,
]),
);

$mutated = $ruleSet->withRules($rules);

self::assertNotSame($ruleSet, $mutated);

self::assertEquals($ruleSet->customFixers(), $mutated->customFixers());
self::assertEquals($ruleSet->name(), $mutated->name());
self::assertEquals($ruleSet->phpVersion(), $mutated->phpVersion());

$expected = $ruleSet->rules()->merge($rules);

self::assertEquals($expected, $mutated->rules());
}

#[Framework\Attributes\DataProvider('provideValidHeader')]
public function testWithHeaderReturnsRuleSetWithEnabledHeaderCommentFixer(string $header): void
{
Expand Down