Skip to content

Commit

Permalink
Enhancement: Extract Config\Rules as a value object
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Sep 16, 2023
1 parent e2825e9 commit 3fbf8ee
Show file tree
Hide file tree
Showing 35 changed files with 305 additions and 145 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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]
- Extracted `Config\Rules` as a value object ([#873]), by [@localheinz]

### Changed

Expand Down Expand Up @@ -1170,6 +1171,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#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
[#873]: https://github.com/ergebnis/php-cs-fixer-config/pull/873

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ file headers will be added to PHP files, for example:
use Ergebnis\PhpCsFixer\Config;

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

$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
Expand Down
74 changes: 36 additions & 38 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,118 +6,116 @@
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php53.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php54.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php55.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php56.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php70.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php71.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php72.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php73.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php74.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php80.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php81.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/RuleSet/Php82.php">
<MixedReturnTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->rules]]></code>
<code>array</code>
</MixedReturnTypeCoercion>
</MixedArgumentTypeCoercion>
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/Rules.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[\array_merge(
$this->value,
$other->value,
)]]></code>
</MixedArgumentTypeCoercion>
<MixedReturnTypeCoercion>
<code><![CDATA[$this->value]]></code>
<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><![CDATA[$this->rules]]></code>
<code>array</code>
<code>iterable</code>
</MixedReturnTypeCoercion>
</file>
Expand Down
13 changes: 6 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ final class Factory
/**
* Creates a configuration based on a rule set.
*
* @param array<string, array<string, mixed>|bool> $overrideRules
*
* @throws \RuntimeException
*/
public static function fromRuleSet(
RuleSet $ruleSet,
array $overrideRules = [],
?Rules $overrideRules = null,
): Config {
$currentPhpVersion = PhpVersion::current();

Expand All @@ -42,10 +40,11 @@ public static function fromRuleSet(

$config->registerCustomFixers($ruleSet->customFixers());
$config->setRiskyAllowed(true);
$config->setRules(\array_merge(
$ruleSet->rules(),
$overrideRules,
));
$config->setRules($ruleSet->rules()->toArray());

if ($overrideRules instanceof Rules) {
$config->setRules($ruleSet->rules()->merge($overrideRules)->toArray());
}

return $config;
}
Expand Down
6 changes: 2 additions & 4 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ public function customFixers(): iterable;
public function name(): Name;

/**
* Returns an array of rules along with their configuration.
*
* @return array<string, array<string, mixed>|bool>
* Returns rules along with their configuration.
*/
public function rules(): array;
public function rules(): Rules;

/**
* Returns the minimum required PHP version.
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php53.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php53 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -823,9 +824,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php54.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php54 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -825,9 +826,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php55.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php55 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -834,9 +835,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php56.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php56 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -834,9 +835,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php70 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -834,9 +835,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php71.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php71 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -837,9 +838,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php72 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -837,9 +838,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
5 changes: 3 additions & 2 deletions src/RuleSet/Php73.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\PhpVersion;
use Ergebnis\PhpCsFixer\Config\Rules;

final class Php73 extends AbstractRuleSet implements ExplicitRuleSet
{
Expand Down Expand Up @@ -838,9 +839,9 @@ public function name(): Name
return Name::fromPhpVersion($this->targetPhpVersion());
}

public function rules(): array
public function rules(): Rules
{
return $this->rules;
return Rules::fromArray($this->rules);
}

public function targetPhpVersion(): PhpVersion
Expand Down
Loading

0 comments on commit 3fbf8ee

Please sign in to comment.