Skip to content

Commit

Permalink
Enhancement: Add Config\RuleSet::withCustomFixers() to allow merging …
Browse files Browse the repository at this point in the history
…Config\Fixers
  • Loading branch information
localheinz committed Sep 18, 2023
1 parent 0580352 commit 06e6044
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ For a full diff see [`5.16.0...main`][5.16.0...main].
- 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]
- Added `Config\RuleSet::withCustomFixers()` to allow registering additional `Config\Fixers` ([#893]), by [@localheinz]

### Changed

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

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,40 @@ This will enable and configure the [`HeaderCommentFixer`](https://github.com/Fri
return $config;
```

### Configuring a rule set that registers and configures rules for custom fixers

:bulb: Optionally register and configure rules for custom fixers:

```diff
<?php

declare(strict_types=1);

use Ergebnis\PhpCsFixer\Config;
use FooBar\Fixer;

-$ruleSet = Config\RuleSet\Php82::create();
+$ruleSet = Config\RuleSet\Php82::create()
+ ->withCustomFixers(Config\Fixers::fromFixers(
+ new Fixer\BarBazFixer(),
+ new Fixer\QuzFixer(),
+ ))
+ ->withRules(Config\Rules::fromArray([
+ 'FooBar/bar_baz' => true,
+ 'FooBar/quz' => [
+ 'qux => false,
+ ],
+ ]))
+]);

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

$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');

return $config;
```

### Makefile

If you like [`Makefile`](https://www.gnu.org/software/make/manual/make.html#Introduction)s, create a `Makefile` with a `coding-standards` target:
Expand Down
8 changes: 8 additions & 0 deletions src/Fixers.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ public function toArray(): array
{
return $this->value;
}

public function merge(self $customFixers): self
{
return new self(...\array_merge(
$this->value,
$customFixers->value,
));
}
}
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 custom fixers.
*/
public function withCustomFixers(Fixers $customFixers): self
{
return new self(
$this->customFixers->merge($customFixers),
$this->name,
$this->phpVersion,
$this->rules,
);
}

/**
* Returns a new rule set with merged rules.
*/
Expand Down
26 changes: 26 additions & 0 deletions test/Unit/FixersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,30 @@ public function testFromIterableReturnsFixersWhenValueIsTraversable(): void

self::assertSame($value, $fixers->toArray());
}

public function testMergeReturnsFixersMergedWithFixers(): void
{
$one = Fixers::fromFixers(
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
);

$two = Fixers::fromFixers(
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
);

$mutated = $one->merge($two);

self::assertNotSame($one, $mutated);
self::assertNotSame($two, $mutated);

$expected = Fixers::fromFixers(...\array_merge(
$one->toArray(),
$two->toArray(),
));

self::assertEquals($expected, $mutated);
}
}
39 changes: 39 additions & 0 deletions test/Unit/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,45 @@ public function testCreateReturnsRuleSet(): void
self::assertSame($rules, $ruleSet->rules());
}

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

$customFixers = Fixers::fromFixers(
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
);

$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->withCustomFixers($customFixers);

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

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

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

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

0 comments on commit 06e6044

Please sign in to comment.