Skip to content

Commit

Permalink
Merge pull request #883 from ergebnis/feature/custom-fixers
Browse files Browse the repository at this point in the history
Enhancement: Extract `Config\Fixers` as a value object
  • Loading branch information
localheinz authored Sep 18, 2023
2 parents b257a53 + 2558a62 commit 266eda3
Show file tree
Hide file tree
Showing 33 changed files with 272 additions and 74 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For a full diff see [`5.16.0...main`][5.16.0...main].
- Extracted `Config\Name` as a value object ([#870]), by [@localheinz]
- 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]

### Changed

Expand Down Expand Up @@ -1194,6 +1195,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#880]: https://github.com/ergebnis/php-cs-fixer-config/pull/880
[#881]: https://github.com/ergebnis/php-cs-fixer-config/pull/881
[#882]: https://github.com/ergebnis/php-cs-fixer-config/pull/882
[#883]: https://github.com/ergebnis/php-cs-fixer-config/pull/883

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
19 changes: 10 additions & 9 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Fixers.php">
<PropertyTypeCoercion>
<code>$value</code>
</PropertyTypeCoercion>
</file>
<file src="src/Rules.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[\array_merge(
Expand All @@ -12,12 +17,6 @@
<code><![CDATA[array<string, array<string, mixed>|bool>]]></code>
</MixedReturnTypeCoercion>
</file>
<file src="test/Double/Config/RuleSet/DummyRuleSet.php">
<MixedReturnTypeCoercion>
<code><![CDATA[$this->customFixers]]></code>
<code>iterable</code>
</MixedReturnTypeCoercion>
</file>
<file src="test/EndToEnd/RuleSet/AbstractRuleSetTestCase.php">
<InternalClass>
<code>Command\FixCommandExitStatusCalculator::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG</code>
Expand All @@ -28,6 +27,11 @@
<code>provideTargetPhpVersionLessThanOrEqualToCurrentPhpVersion</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/FixersTest.php">
<InvalidArgument>
<code>$value</code>
</InvalidArgument>
</file>
<file src="test/Unit/PhpVersion/MinorTest.php">
<PossiblyUnusedMethod>
<code>provideValidValue</code>
Expand All @@ -49,9 +53,6 @@
<code>new FixerFactory()</code>
<code>registerBuiltInFixers</code>
</InternalMethod>
<InvalidArgument>
<code><![CDATA[static::createRuleSet()->customFixers()]]></code>
</InvalidArgument>
<PossiblyUnusedMethod>
<code>provideValidHeader</code>
</PossiblyUnusedMethod>
Expand Down
2 changes: 1 addition & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function fromRuleSet(

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

$config->registerCustomFixers($ruleSet->customFixers());
$config->registerCustomFixers($ruleSet->customFixers()->toArray());
$config->setRiskyAllowed(true);
$config->setRules($ruleSet->rules()->toArray());

Expand Down
71 changes: 71 additions & 0 deletions src/Fixers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config;

use PhpCsFixer\Fixer;

final class Fixers
{
/**
* @var list<Fixer\FixerInterface>
*/
private readonly array $value;

private function __construct(Fixer\FixerInterface ...$value)
{
$this->value = $value;
}

public static function empty(): self
{
return new self();
}

public static function fromFixers(Fixer\FixerInterface ...$value): self
{
return new self(...$value);
}

/**
* @param iterable<Fixer\FixerInterface> $iterable
*
* @throws \InvalidArgumentException
*/
public static function fromIterable(iterable $iterable): self
{
$value = [];

foreach ($iterable as $iterated) {
if (!$iterated instanceof Fixer\FixerInterface) {
throw new \InvalidArgumentException(\sprintf(
'Expected iterable to contain only instances of %s, got %s instead.',
Fixer\FixerInterface::class,
\get_debug_type($iterated),
));
}

$value[] = $iterated;
}

return new self(...$value);
}

/**
* @return list<Fixer\FixerInterface>
*/
public function toArray(): array
{
return $this->value;
}
}
6 changes: 1 addition & 5 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@

namespace Ergebnis\PhpCsFixer\Config;

use PhpCsFixer\Fixer;

interface RuleSet
{
/**
* Returns custom fixers required by this rule set.
*
* @return iterable<Fixer\FixerInterface>
*/
public function customFixers(): iterable;
public function customFixers(): Fixers;

/**
* Returns the name of the rule set.
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php53.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -845,9 +846,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php54.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -847,9 +848,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php55.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -856,9 +857,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php56.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -856,9 +857,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -857,9 +858,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php71.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -859,9 +860,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -859,9 +860,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php73.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -860,9 +861,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -863,9 +864,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -872,9 +873,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\ExplicitRuleSet;
use Ergebnis\PhpCsFixer\Config\Fixers;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;
Expand Down Expand Up @@ -874,9 +875,9 @@ public function __construct(?string $header = null)
];
}

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

public function name(): Name
Expand Down
Loading

0 comments on commit 266eda3

Please sign in to comment.