Skip to content

Commit

Permalink
Merge pull request #872 from ergebnis/feature/custom-fixer
Browse files Browse the repository at this point in the history
Enhancement: Allow `Config\RuleSet` to declare and configure custom fixers
  • Loading branch information
localheinz committed Sep 16, 2023
2 parents 2c5394a + 9cfb781 commit bb8e4e4
Show file tree
Hide file tree
Showing 31 changed files with 162 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,10 @@ For a full diff see [`5.15.1...main`][5.15.1...main].
- Extracted `Config\Name` as a value object ([#870]), by [@localheinz]
- Extracted `Config\PhpVersion` as a value object ([#871]), by [@localheinz]

### Changed

- Allow implementations of `Config\RuleSet` to declare and configure custom fixers ([#872]), by [@localheinz]

### Fixed

- Fixed target PHP versions in `Config\Ruleset\Php53`, `Config\Ruleset\Php54`, `Config\Ruleset\Php55`, `Config\Ruleset\Php56`, and `Config\Ruleset\Php70` ([#864]), by [@localheinz]
Expand Down Expand Up @@ -1165,6 +1169,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#868]: https://github.com/ergebnis/php-cs-fixer-config/pull/868
[#870]: https://github.com/ergebnis/php-cs-fixer-config/pull/870
[#871]: https://github.com/ergebnis/php-cs-fixer-config/pull/871
[#872]: https://github.com/ergebnis/php-cs-fixer-config/pull/872

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
2 changes: 2 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -115,8 +115,10 @@
</file>
<file src="test/Double/Config/RuleSet/DummyRuleSet.php">
<MixedReturnTypeCoercion>
<code><![CDATA[$this->customFixers]]></code>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
<code>iterable</code>
</MixedReturnTypeCoercion>
</file>
<file src="test/EndToEnd/RuleSet/AbstractRuleSetTestCase.php">
Expand Down
1 change: 1 addition & 0 deletions src/Factory.php
Expand Up @@ -40,6 +40,7 @@ public static function fromRuleSet(

$config = new Config($ruleSet->name()->toString());

$config->registerCustomFixers($ruleSet->customFixers());
$config->setRiskyAllowed(true);
$config->setRules(\array_merge(
$ruleSet->rules(),
Expand Down
7 changes: 7 additions & 0 deletions src/RuleSet.php
Expand Up @@ -13,8 +13,15 @@

namespace Ergebnis\PhpCsFixer\Config;

use PhpCsFixer\Fixer;

interface RuleSet
{
/**
* @return iterable<Fixer\FixerInterface>
*/
public function customFixers(): iterable;

/**
* Returns the name of the rule set.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php53.php
Expand Up @@ -813,6 +813,11 @@ final class Php53 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php54.php
Expand Up @@ -815,6 +815,11 @@ final class Php54 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php55.php
Expand Up @@ -824,6 +824,11 @@ final class Php55 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php56.php
Expand Up @@ -824,6 +824,11 @@ final class Php56 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php70.php
Expand Up @@ -824,6 +824,11 @@ final class Php70 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php71.php
Expand Up @@ -827,6 +827,11 @@ final class Php71 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php72.php
Expand Up @@ -827,6 +827,11 @@ final class Php72 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php73.php
Expand Up @@ -828,6 +828,11 @@ final class Php73 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php74.php
Expand Up @@ -831,6 +831,11 @@ final class Php74 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php80.php
Expand Up @@ -840,6 +840,11 @@ final class Php80 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php81.php
Expand Up @@ -842,6 +842,11 @@ final class Php81 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
5 changes: 5 additions & 0 deletions src/RuleSet/Php82.php
Expand Up @@ -842,6 +842,11 @@ final class Php82 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function customFixers(): iterable
{
yield from [];
}

public function name(): Name
{
return Name::fromPhpVersion($this->targetPhpVersion());
Expand Down
6 changes: 6 additions & 0 deletions test/Double/Config/RuleSet/DummyRuleSet.php
Expand Up @@ -20,12 +20,18 @@
final class DummyRuleSet implements RuleSet
{
public function __construct(
private readonly iterable $customFixers,
private readonly Name $name,
private readonly array $rules,
private readonly PhpVersion $phpVersion,
) {
}

public function customFixers(): iterable
{
return $this->customFixers;
}

public function name(): Name
{
return $this->name;
Expand Down
18 changes: 18 additions & 0 deletions test/Unit/FactoryTest.php
Expand Up @@ -17,6 +17,7 @@
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Test;
use PhpCsFixer\Fixer;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Factory::class)]
Expand All @@ -38,6 +39,7 @@ public function testFromRuleSetThrowsRuntimeExceptionWhenCurrentPhpVersionIsLess
);

$ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet(
[],
Name::fromString(self::faker()->word()),
[],
$targetPhpVersion,
Expand All @@ -56,6 +58,12 @@ public function testFromRuleSetThrowsRuntimeExceptionWhenCurrentPhpVersionIsLess
#[Framework\Attributes\DataProvider('provideTargetPhpVersionLessThanOrEqualToCurrentPhpVersion')]
public function testFromRuleSetCreatesConfigWhenCurrentPhpVersionIsEqualToOrGreaterThanTargetPhpVersion(PhpVersion $targetPhpVersion): void
{
$customFixers = [
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
];

$rules = [
'foo' => true,
'bar' => [
Expand All @@ -64,13 +72,15 @@ public function testFromRuleSetCreatesConfigWhenCurrentPhpVersionIsEqualToOrGrea
];

$ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet(
$customFixers,
Name::fromString(self::faker()->word()),
$rules,
$targetPhpVersion,
);

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

self::assertEquals($customFixers, $config->getCustomFixers());
self::assertTrue($config->getRiskyAllowed());
self::assertSame($rules, $config->getRules());
self::assertTrue($config->getUsingCache());
Expand All @@ -95,6 +105,12 @@ public static function provideTargetPhpVersionLessThanOrEqualToCurrentPhpVersion

public function testFromRuleSetCreatesConfigWithOverrideRules(): void
{
$customFixers = [
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
$this->createStub(Fixer\FixerInterface::class),
];

$rules = [
'foo' => true,
'bar' => [
Expand All @@ -103,6 +119,7 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void
];

$ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet(
$customFixers,
Name::fromString(self::faker()->word()),
$rules,
PhpVersion::create(
Expand All @@ -121,6 +138,7 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void
$overrideRules,
);

self::assertEquals($customFixers, $config->getCustomFixers());
self::assertTrue($config->getRiskyAllowed());
self::assertEquals(\array_merge($rules, $overrideRules), $config->getRules());
self::assertTrue($config->getUsingCache());
Expand Down
3 changes: 3 additions & 0 deletions test/Unit/RuleSet/AbstractRuleSetTestCase.php
Expand Up @@ -128,6 +128,7 @@ final public function testDefaults(): void
{
$ruleSet = self::createRuleSet();

self::assertEquals($this->expectedCustomFixers(), $ruleSet->customFixers());
self::assertEquals($this->expectedName(), $ruleSet->name());
self::assertEquals($this->expectedRules(), $ruleSet->rules());
self::assertEquals($this->expectedTargetPhpVersion(), $ruleSet->targetPhpVersion());
Expand Down Expand Up @@ -307,6 +308,8 @@ final public static function provideValidHeader(): \Generator
}
}

abstract protected function expectedCustomFixers(): iterable;

abstract protected function expectedName(): Name;

abstract protected function expectedRules(): array;
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php53Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php53Test extends ExplicitRuleSetTestCase
{
protected function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 5.3)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php54Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php54Test extends ExplicitRuleSetTestCase
{
protected function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 5.4)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php55Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php55Test extends ExplicitRuleSetTestCase
{
public function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 5.5)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php56Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php56Test extends ExplicitRuleSetTestCase
{
protected function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 5.6)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php70Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php70Test extends ExplicitRuleSetTestCase
{
protected function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 7.0)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php71Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php71Test extends ExplicitRuleSetTestCase
{
protected function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 7.1)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php72Test.php
Expand Up @@ -30,6 +30,11 @@
#[Framework\Attributes\UsesClass(PhpVersion\Patch::class)]
final class Php72Test extends ExplicitRuleSetTestCase
{
protected function expectedCustomFixers(): iterable
{
yield from [];
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 7.2)');
Expand Down

0 comments on commit bb8e4e4

Please sign in to comment.