diff --git a/CHANGELOG.md b/CHANGELOG.md index d4d52f75..f36dc3d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/README.md b/README.md index 7bb8ba2e..876c6ea1 100644 --- a/README.md +++ b/README.md @@ -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'); diff --git a/infection.json b/infection.json index ac634aed..413dcddb 100644 --- a/infection.json +++ b/infection.json @@ -3,8 +3,8 @@ "logs": { "text": ".build/infection/infection-log.txt" }, - "minCoveredMsi": 99, - "minMsi": 99, + "minCoveredMsi": 98, + "minMsi": 98, "phpUnit": { "configDir": "test\/Unit" }, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e1092275..76fa349e 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -6,118 +6,116 @@ - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules - + rules]]> - array - + $rules + + + value, + $other->value, + )]]> + + + value]]> + |bool>]]> + + customFixers]]> - rules]]> - array iterable diff --git a/src/Factory.php b/src/Factory.php index b97874aa..970a39c1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -20,13 +20,11 @@ final class Factory /** * Creates a configuration based on a rule set. * - * @param array|bool> $overrideRules - * * @throws \RuntimeException */ public static function fromRuleSet( RuleSet $ruleSet, - array $overrideRules = [], + ?Rules $overrideRules = null, ): Config { $currentPhpVersion = PhpVersion::current(); @@ -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; } diff --git a/src/RuleSet.php b/src/RuleSet.php index 181b9439..e2ae72d8 100644 --- a/src/RuleSet.php +++ b/src/RuleSet.php @@ -28,11 +28,9 @@ public function customFixers(): iterable; public function name(): Name; /** - * Returns an array of rules along with their configuration. - * - * @return array|bool> + * Returns rules along with their configuration. */ - public function rules(): array; + public function rules(): Rules; /** * Returns the minimum required PHP version. diff --git a/src/RuleSet/Php53.php b/src/RuleSet/Php53.php index 3863aa23..d9811bdf 100644 --- a/src/RuleSet/Php53.php +++ b/src/RuleSet/Php53.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php54.php b/src/RuleSet/Php54.php index 77ccd69e..bd8ed1f0 100644 --- a/src/RuleSet/Php54.php +++ b/src/RuleSet/Php54.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php55.php b/src/RuleSet/Php55.php index d53504ba..980a0f51 100644 --- a/src/RuleSet/Php55.php +++ b/src/RuleSet/Php55.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php56.php b/src/RuleSet/Php56.php index 4fd54128..d2491e5d 100644 --- a/src/RuleSet/Php56.php +++ b/src/RuleSet/Php56.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php70.php b/src/RuleSet/Php70.php index 3011bed3..d0d2a5eb 100644 --- a/src/RuleSet/Php70.php +++ b/src/RuleSet/Php70.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php71.php b/src/RuleSet/Php71.php index da52de3a..a285af8d 100644 --- a/src/RuleSet/Php71.php +++ b/src/RuleSet/Php71.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php72.php b/src/RuleSet/Php72.php index 9ba2cc30..5ec8f6a5 100644 --- a/src/RuleSet/Php72.php +++ b/src/RuleSet/Php72.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php73.php b/src/RuleSet/Php73.php index d84022ad..32bd9f5d 100644 --- a/src/RuleSet/Php73.php +++ b/src/RuleSet/Php73.php @@ -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 { @@ -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 diff --git a/src/RuleSet/Php74.php b/src/RuleSet/Php74.php index 159ca199..8f8f7528 100644 --- a/src/RuleSet/Php74.php +++ b/src/RuleSet/Php74.php @@ -15,6 +15,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; final class Php74 extends AbstractRuleSet implements ExplicitRuleSet { @@ -841,9 +842,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 diff --git a/src/RuleSet/Php80.php b/src/RuleSet/Php80.php index 49f321f2..0701c121 100644 --- a/src/RuleSet/Php80.php +++ b/src/RuleSet/Php80.php @@ -15,6 +15,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; final class Php80 extends AbstractRuleSet implements ExplicitRuleSet { @@ -850,9 +851,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 diff --git a/src/RuleSet/Php81.php b/src/RuleSet/Php81.php index b9a86dbd..895cc41e 100644 --- a/src/RuleSet/Php81.php +++ b/src/RuleSet/Php81.php @@ -15,6 +15,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; final class Php81 extends AbstractRuleSet implements ExplicitRuleSet { @@ -852,9 +853,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 diff --git a/src/RuleSet/Php82.php b/src/RuleSet/Php82.php index 66db95ec..7b31c7f2 100644 --- a/src/RuleSet/Php82.php +++ b/src/RuleSet/Php82.php @@ -15,6 +15,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; final class Php82 extends AbstractRuleSet implements ExplicitRuleSet { @@ -852,9 +853,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 diff --git a/src/Rules.php b/src/Rules.php new file mode 100644 index 00000000..d2c748b2 --- /dev/null +++ b/src/Rules.php @@ -0,0 +1,51 @@ +|bool> $value + */ + private function __construct(array $value) + { + $this->value = $value; + } + + /** + * @param array|bool> $value + */ + public static function fromArray(array $value): self + { + return new self($value); + } + + /** + * @return array|bool> + */ + public function toArray(): array + { + return $this->value; + } + + public function merge(self $other): self + { + return new self(\array_merge( + $this->value, + $other->value, + )); + } +} diff --git a/test/Double/Config/RuleSet/DummyRuleSet.php b/test/Double/Config/RuleSet/DummyRuleSet.php index d522295a..887bf8a9 100644 --- a/test/Double/Config/RuleSet/DummyRuleSet.php +++ b/test/Double/Config/RuleSet/DummyRuleSet.php @@ -15,6 +15,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; final class DummyRuleSet implements RuleSet @@ -22,7 +23,7 @@ final class DummyRuleSet implements RuleSet public function __construct( private readonly iterable $customFixers, private readonly Name $name, - private readonly array $rules, + private readonly Rules $rules, private readonly PhpVersion $phpVersion, ) { } @@ -37,7 +38,7 @@ public function name(): Name return $this->name; } - public function rules(): array + public function rules(): Rules { return $this->rules; } diff --git a/test/Unit/FactoryTest.php b/test/Unit/FactoryTest.php index 204ace61..323f85c2 100644 --- a/test/Unit/FactoryTest.php +++ b/test/Unit/FactoryTest.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\Test; use PhpCsFixer\Fixer; use PHPUnit\Framework; @@ -26,6 +27,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class FactoryTest extends Framework\TestCase { use Test\Util\Helper; @@ -41,7 +43,7 @@ public function testFromRuleSetThrowsRuntimeExceptionWhenCurrentPhpVersionIsLess $ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet( [], Name::fromString(self::faker()->word()), - [], + Rules::fromArray([]), $targetPhpVersion, ); @@ -64,12 +66,12 @@ public function testFromRuleSetCreatesConfigWhenCurrentPhpVersionIsEqualToOrGrea $this->createStub(Fixer\FixerInterface::class), ]; - $rules = [ + $rules = Rules::fromArray([ 'foo' => true, 'bar' => [ 'baz' => true, ], - ]; + ]); $ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet( $customFixers, @@ -82,7 +84,7 @@ public function testFromRuleSetCreatesConfigWhenCurrentPhpVersionIsEqualToOrGrea self::assertEquals($customFixers, $config->getCustomFixers()); self::assertTrue($config->getRiskyAllowed()); - self::assertSame($rules, $config->getRules()); + self::assertSame($rules->toArray(), $config->getRules()); self::assertTrue($config->getUsingCache()); } @@ -111,12 +113,12 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void $this->createStub(Fixer\FixerInterface::class), ]; - $rules = [ + $rules = Rules::fromArray([ 'foo' => true, 'bar' => [ 'baz' => true, ], - ]; + ]); $ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet( $customFixers, @@ -129,9 +131,9 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void ), ); - $overrideRules = [ + $overrideRules = Rules::fromArray([ 'foo' => false, - ]; + ]); $config = Factory::fromRuleSet( $ruleSet, @@ -140,7 +142,7 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void self::assertEquals($customFixers, $config->getCustomFixers()); self::assertTrue($config->getRiskyAllowed()); - self::assertEquals(\array_merge($rules, $overrideRules), $config->getRules()); + self::assertEquals($rules->merge($overrideRules)->toArray(), $config->getRules()); self::assertTrue($config->getUsingCache()); } } diff --git a/test/Unit/RuleSet/AbstractRuleSetTestCase.php b/test/Unit/RuleSet/AbstractRuleSetTestCase.php index fcb4005d..c39595a3 100644 --- a/test/Unit/RuleSet/AbstractRuleSetTestCase.php +++ b/test/Unit/RuleSet/AbstractRuleSetTestCase.php @@ -15,6 +15,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PhpCsFixer\Fixer; use PhpCsFixer\FixerConfiguration; @@ -140,7 +141,7 @@ final public function testRuleSetDoesNotConfigureRulesThatAreNotRegistered(): vo $fixersThatAreRegistered = self::fixersThatAreRegistered(); $rulesWithoutRulesThatAreNotRegistered = \array_filter( - $rules, + $rules->toArray(), static function (string $nameOfRule) use ($fixersThatAreRegistered): bool { if (\str_starts_with($nameOfRule, '@')) { return true; @@ -154,7 +155,7 @@ static function (string $nameOfRule) use ($fixersThatAreRegistered): bool { \ARRAY_FILTER_USE_KEY, ); - self::assertEquals($rulesWithoutRulesThatAreNotRegistered, $rules, \sprintf( + self::assertEquals($rulesWithoutRulesThatAreNotRegistered, $rules->toArray(), \sprintf( 'Failed asserting that rule set "%s" does not configure rules that are not registered.', static::className(), )); @@ -167,7 +168,7 @@ final public function testRuleSetDoesNotConfigureRulesThatAreDeprecated(): void $fixersThatAreRegistered = self::fixersThatAreRegistered(); $rulesWithoutRulesThatAreDeprecated = \array_filter( - $rules, + $rules->toArray(), static function (string $nameOfRule) use ($fixersThatAreRegistered): bool { if (!\array_key_exists($nameOfRule, $fixersThatAreRegistered)) { return true; @@ -180,7 +181,7 @@ static function (string $nameOfRule) use ($fixersThatAreRegistered): bool { \ARRAY_FILTER_USE_KEY, ); - self::assertEquals($rulesWithoutRulesThatAreDeprecated, $rules, \sprintf( + self::assertEquals($rulesWithoutRulesThatAreDeprecated, $rules->toArray(), \sprintf( 'Failed asserting that rule set "%s" does not configure rules that are deprecated.', static::className(), )); @@ -190,7 +191,7 @@ final public function testRuleSetDoesNotConfigureRulesUsingDeprecatedConfigurati { $rules = self::createRuleSet()->rules(); - $namesOfRules = \array_keys($rules); + $namesOfRules = \array_keys($rules->toArray()); $fixersThatAreRegistered = self::fixersThatAreRegistered(); @@ -229,10 +230,10 @@ final public function testRuleSetDoesNotConfigureRulesUsingDeprecatedConfigurati } return $ruleConfigurationWithoutDeprecatedConfigurationOptions; - }, $namesOfRules, $rules), + }, $namesOfRules, $rules->toArray()), ); - self::assertEquals($rulesWithoutDeprecatedConfigurationOptions, $rules, \sprintf( + self::assertEquals($rulesWithoutDeprecatedConfigurationOptions, $rules->toArray(), \sprintf( 'Failed asserting that rule set "%s" does not configure rules using deprecated configuration options.', static::className(), )); @@ -242,9 +243,9 @@ final public function testRulesAndConfigurationOptionsAreSortedInRuleSet(): void { $rules = self::createRuleSet()->rules(); - $sorted = self::sort($rules); + $sorted = self::sort($rules->toArray()); - self::assertSame($sorted, $rules, \sprintf( + self::assertSame($sorted, $rules->toArray(), \sprintf( 'Failed asserting that rules and configuration options are sorted by name in rule set "%s".', static::className(), )); @@ -254,9 +255,9 @@ final public function testRulesAndConfigurationOptionsAreSortedInRuleSetTest(): { $rules = $this->expectedRules(); - $sorted = self::sort($rules); + $sorted = self::sort($rules->toArray()); - self::assertSame($sorted, $rules, \sprintf( + self::assertSame($sorted, $rules->toArray(), \sprintf( 'Failed asserting that rules and configuration options are sorted by name in rule set test "%s".', static::class, )); @@ -266,8 +267,8 @@ final public function testHeaderCommentFixerIsDisabledByDefault(): void { $rules = self::createRuleSet()->rules(); - self::assertArrayHasKey('header_comment', $rules); - self::assertFalse($rules['header_comment']); + self::assertArrayHasKey('header_comment', $rules->toArray()); + self::assertFalse($rules->toArray()['header_comment']); } #[Framework\Attributes\DataProvider('provideValidHeader')] @@ -275,7 +276,7 @@ final public function testHeaderCommentFixerIsEnabledIfHeaderIsProvided(string $ { $rules = self::createRuleSet($header)->rules(); - self::assertArrayHasKey('header_comment', $rules); + self::assertArrayHasKey('header_comment', $rules->toArray()); $expected = [ 'comment_type' => 'PHPDoc', @@ -284,7 +285,7 @@ final public function testHeaderCommentFixerIsEnabledIfHeaderIsProvided(string $ 'separate' => 'both', ]; - self::assertEquals($expected, $rules['header_comment']); + self::assertEquals($expected, $rules->toArray()['header_comment']); } /** @@ -311,7 +312,7 @@ abstract protected function expectedCustomFixers(): iterable; abstract protected function expectedName(): Name; - abstract protected function expectedRules(): array; + abstract protected function expectedRules(): Rules; abstract protected function expectedTargetPhpVersion(): PhpVersion; diff --git a/test/Unit/RuleSet/ExplicitRuleSetTestCase.php b/test/Unit/RuleSet/ExplicitRuleSetTestCase.php index 8963791f..226c416f 100644 --- a/test/Unit/RuleSet/ExplicitRuleSetTestCase.php +++ b/test/Unit/RuleSet/ExplicitRuleSetTestCase.php @@ -31,14 +31,14 @@ final public function testRuleSetDoesNotConfigureRuleSets(): void $rules = self::createRuleSet()->rules(); $rulesWithoutRulesForRuleSets = \array_filter( - $rules, + $rules->toArray(), static function (string $nameOfRule): bool { return !\str_starts_with($nameOfRule, '@'); }, \ARRAY_FILTER_USE_KEY, ); - self::assertEquals($rulesWithoutRulesForRuleSets, $rules, \sprintf( + self::assertEquals($rulesWithoutRulesForRuleSets, $rules->toArray(), \sprintf( 'Failed asserting that rule set "%s" does not configure rules for rule sets.', static::className(), )); @@ -63,10 +63,10 @@ final public function testRuleSetConfiguresAllRulesThatAreNotDeprecated(): void $rulesWithRulesThatAreNotDeprecated = \array_merge( $rulesThatAreNotDeprecated, - $rules, + $rules->toArray(), ); - self::assertEquals($rulesWithRulesThatAreNotDeprecated, $rules, \sprintf( + self::assertEquals($rulesWithRulesThatAreNotDeprecated, $rules->toArray(), \sprintf( 'Failed asserting that rule set "%s" configures all non-deprecated fixers.', static::className(), )); @@ -76,7 +76,7 @@ final public function testRuleSetConfiguresAllRulesThatAreConfigurableAndNotDepr { $rules = self::createRuleSet()->rules(); - $namesOfRules = \array_keys($rules); + $namesOfRules = \array_keys($rules->toArray()); $fixersThatAreRegistered = self::fixersThatAreRegistered(); @@ -133,10 +133,10 @@ final public function testRuleSetConfiguresAllRulesThatAreConfigurableAndNotDepr $ruleConfiguration, $diff, ); - }, $namesOfRules, $rules), + }, $namesOfRules, $rules->toArray()), ); - self::assertEquals($rulesWithAllNonDeprecatedConfigurationOptions, $rules, \sprintf( + self::assertEquals($rulesWithAllNonDeprecatedConfigurationOptions, $rules->toArray(), \sprintf( 'Failed asserting that rule set "%s" configures configurable rules using all non-deprecated configuration options.', static::className(), )); diff --git a/test/Unit/RuleSet/Php53Test.php b/test/Unit/RuleSet/Php53Test.php index 743abfc6..92b88296 100644 --- a/test/Unit/RuleSet/Php53Test.php +++ b/test/Unit/RuleSet/Php53Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php53Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 5.3)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -834,7 +836,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php54Test.php b/test/Unit/RuleSet/Php54Test.php index 84d70145..efbb5ea0 100644 --- a/test/Unit/RuleSet/Php54Test.php +++ b/test/Unit/RuleSet/Php54Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php54Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 5.4)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -836,7 +838,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php55Test.php b/test/Unit/RuleSet/Php55Test.php index 0cff1643..746e1838 100644 --- a/test/Unit/RuleSet/Php55Test.php +++ b/test/Unit/RuleSet/Php55Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php55Test extends ExplicitRuleSetTestCase { public function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 5.5)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -845,7 +847,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php56Test.php b/test/Unit/RuleSet/Php56Test.php index 543abf4c..889eba05 100644 --- a/test/Unit/RuleSet/Php56Test.php +++ b/test/Unit/RuleSet/Php56Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php56Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 5.6)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -845,7 +847,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php70Test.php b/test/Unit/RuleSet/Php70Test.php index 1eb0d3fc..f4d65b00 100644 --- a/test/Unit/RuleSet/Php70Test.php +++ b/test/Unit/RuleSet/Php70Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php70Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 7.0)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -845,7 +847,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php71Test.php b/test/Unit/RuleSet/Php71Test.php index a9a9821a..f0e638b3 100644 --- a/test/Unit/RuleSet/Php71Test.php +++ b/test/Unit/RuleSet/Php71Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php71Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 7.1)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -848,7 +850,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php72Test.php b/test/Unit/RuleSet/Php72Test.php index 3ff62d8f..fcc805cd 100644 --- a/test/Unit/RuleSet/Php72Test.php +++ b/test/Unit/RuleSet/Php72Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php72Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 7.2)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -848,7 +850,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php73Test.php b/test/Unit/RuleSet/Php73Test.php index 6e0708de..b71604ce 100644 --- a/test/Unit/RuleSet/Php73Test.php +++ b/test/Unit/RuleSet/Php73Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php73Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 7.3)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -849,7 +851,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php74Test.php b/test/Unit/RuleSet/Php74Test.php index a3d51d3b..3647b560 100644 --- a/test/Unit/RuleSet/Php74Test.php +++ b/test/Unit/RuleSet/Php74Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php74Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 7.4)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -852,7 +854,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php80Test.php b/test/Unit/RuleSet/Php80Test.php index ea8b4108..c9dd080a 100644 --- a/test/Unit/RuleSet/Php80Test.php +++ b/test/Unit/RuleSet/Php80Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php80Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 8.0)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -861,7 +863,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php81Test.php b/test/Unit/RuleSet/Php81Test.php index ecd30c62..78bfb5e3 100644 --- a/test/Unit/RuleSet/Php81Test.php +++ b/test/Unit/RuleSet/Php81Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php81Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 8.1)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -863,7 +865,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RuleSet/Php82Test.php b/test/Unit/RuleSet/Php82Test.php index 6732ae32..8604f14c 100644 --- a/test/Unit/RuleSet/Php82Test.php +++ b/test/Unit/RuleSet/Php82Test.php @@ -16,6 +16,7 @@ use Ergebnis\PhpCsFixer\Config\Factory; use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; +use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; use PHPUnit\Framework; @@ -27,6 +28,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Major::class)] #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] +#[Framework\Attributes\UsesClass(Rules::class)] final class Php82Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): iterable @@ -39,9 +41,9 @@ protected function expectedName(): Name return Name::fromString('ergebnis (PHP 8.2)'); } - protected function expectedRules(): array + protected function expectedRules(): Rules { - return [ + return Rules::fromArray([ 'align_multiline_comment' => [ 'comment_type' => 'all_multiline', ], @@ -863,7 +865,7 @@ protected function expectedRules(): array 'identical' => true, 'less_and_greater' => true, ], - ]; + ]); } protected function expectedTargetPhpVersion(): PhpVersion diff --git a/test/Unit/RulesTest.php b/test/Unit/RulesTest.php new file mode 100644 index 00000000..6385410a --- /dev/null +++ b/test/Unit/RulesTest.php @@ -0,0 +1,72 @@ + true, + 'bar' => [ + 'baz' => [ + 'qux', + 'quz', + ], + ], + ]; + + $rules = Rules::fromArray($value); + + self::assertSame($value, $rules->toArray()); + } + + public function testMergeReturnsRulesMergedWithRules(): void + { + $one = Rules::fromArray([ + 'foo' => true, + 'bar' => [ + 'baz' => [ + 'qux', + 'quz', + ], + ], + ]); + + $two = Rules::fromArray([ + 'foo' => false, + 'bar' => [ + 'quux' => [], + ], + 'baz' => true, + ]); + + $merged = $one->merge($two); + + $expected = Rules::fromArray([ + 'foo' => false, + 'bar' => [ + 'quux' => [], + ], + 'baz' => true, + ]); + + self::assertEquals($expected, $merged); + } +}