From c73e112e8bec1c846ddd76e74b70494dbd9fd7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 18 Sep 2023 10:19:09 +0200 Subject: [PATCH] Enhancement: Turn Config\RuleSet into value object --- CHANGELOG.md | 2 + psalm-baseline.xml | 2 + src/RuleSet.php | 61 +++++++- src/RuleSet/Php53.php | 60 +------- src/RuleSet/Php54.php | 60 +------- src/RuleSet/Php55.php | 60 +------- src/RuleSet/Php56.php | 60 +------- src/RuleSet/Php70.php | 60 +------- src/RuleSet/Php71.php | 60 +------- src/RuleSet/Php72.php | 60 +------- src/RuleSet/Php73.php | 60 +------- src/RuleSet/Php74.php | 60 +------- src/RuleSet/Php80.php | 60 +------- src/RuleSet/Php81.php | 60 +------- src/RuleSet/Php82.php | 60 +------- test/Double/Config/RuleSet/DummyRuleSet.php | 59 -------- test/Unit/FactoryTest.php | 8 +- test/Unit/RuleSet/AbstractRuleSetTestCase.php | 45 ------ test/Unit/RuleSet/Php53Test.php | 1 + test/Unit/RuleSet/Php54Test.php | 1 + test/Unit/RuleSet/Php55Test.php | 1 + test/Unit/RuleSet/Php56Test.php | 1 + test/Unit/RuleSet/Php70Test.php | 1 + test/Unit/RuleSet/Php71Test.php | 1 + test/Unit/RuleSet/Php72Test.php | 1 + test/Unit/RuleSet/Php73Test.php | 1 + test/Unit/RuleSet/Php74Test.php | 1 + test/Unit/RuleSet/Php80Test.php | 1 + test/Unit/RuleSet/Php81Test.php | 1 + test/Unit/RuleSet/Php82Test.php | 1 + test/Unit/RuleSetTest.php | 130 ++++++++++++++++++ 31 files changed, 242 insertions(+), 797 deletions(-) delete mode 100644 test/Double/Config/RuleSet/DummyRuleSet.php create mode 100644 test/Unit/RuleSetTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9583e1df..baf80762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ For a full diff see [`5.16.0...main`][5.16.0...main]. - Allow implementations of `Config\RuleSet` to declare and configure custom fixers ([#872]), by [@localheinz] - Renamed `Config\RuleSet::targetPhpVersion()` to `Config\RuleSet::phpVersion()` ([#878]), by [@localheinz] - Reduced visibility of constructors and extracted named constructors `Config\RuleSet\Php53::create()`, `Config\RuleSet\Php54::create()`, `Config\RuleSet\Php55::create()`, `Config\RuleSet\Php56::create()`, `Config\RuleSet\Php70::create()`, `Config\RuleSet\Php71::create()`, `Config\RuleSet\Php72::create()`, `Config\RuleSet\Php73::create()`, `Config\RuleSet\Php74::create()`, `Config\RuleSet\Php80::create()`, `Config\RuleSet\Php81::create()`, `Config\RuleSet\Php82::create()` ([#886]), by [@localheinz] +- Turned `Config\RuleSet` into a value object returned from named constructors `Config\RuleSet\Php53::create()`, `Config\RuleSet\Php54::create()`, `Config\RuleSet\Php55::create()`, `Config\RuleSet\Php56::create()`, `Config\RuleSet\Php70::create()`, `Config\RuleSet\Php71::create()`, `Config\RuleSet\Php72::create()`, `Config\RuleSet\Php73::create()`, `Config\RuleSet\Php74::create()`, `Config\RuleSet\Php80::create()`, `Config\RuleSet\Php81::create()`, `Config\RuleSet\Php82::create()` ([#888]), by [@localheinz] ### Fixed @@ -1200,6 +1201,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0]. [#885]: https://github.com/ergebnis/php-cs-fixer-config/pull/885 [#886]: https://github.com/ergebnis/php-cs-fixer-config/pull/886 [#887]: https://github.com/ergebnis/php-cs-fixer-config/pull/887 +[#888]: https://github.com/ergebnis/php-cs-fixer-config/pull/888 [@dependabot]: https://github.com/apps/dependabot [@linuxjuggler]: https://github.com/linuxjuggler diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 066139fd..8a366e32 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -53,6 +53,8 @@ new FixerFactory() registerBuiltInFixers + + provideValidHeader diff --git a/src/RuleSet.php b/src/RuleSet.php index bb7f7fd7..83430c7c 100644 --- a/src/RuleSet.php +++ b/src/RuleSet.php @@ -13,32 +13,81 @@ namespace Ergebnis\PhpCsFixer\Config; -interface RuleSet +final class RuleSet { + private function __construct( + private readonly Fixers $customFixers, + private readonly Name $name, + private readonly PhpVersion $phpVersion, + private readonly Rules $rules, + ) { + } + + public static function create( + Fixers $customFixers, + Name $name, + PhpVersion $phpVersion, + Rules $rules, + ): self { + return new self( + $customFixers, + $name, + $phpVersion, + $rules, + ); + } + /** * Returns custom fixers required by this rule set. */ - public function customFixers(): Fixers; + public function customFixers(): Fixers + { + return $this->customFixers; + } /** * Returns the name of the rule set. */ - public function name(): Name; + public function name(): Name + { + return $this->name; + } /** * Returns the minimum required PHP version. */ - public function phpVersion(): PhpVersion; + public function phpVersion(): PhpVersion + { + return $this->phpVersion; + } /** * Returns rules along with their configuration. */ - public function rules(): Rules; + public function rules(): Rules + { + return $this->rules; + } /** * Returns rules along with the header_comment fixer enabled to add a header. * * @see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.27.0/doc/rules/comment/header_comment.rst */ - public function withHeader(string $header): self; + public function withHeader(string $header): self + { + return new self( + $this->customFixers, + $this->name, + $this->phpVersion, + $this->rules->merge(Rules::fromArray([ + 'header_comment' => [ + 'comment_type' => 'PHPDoc', + 'header' => \trim($header), + 'location' => 'after_declare_strict', + 'separate' => 'both', + ], + ])), + ); + } } diff --git a/src/RuleSet/Php53.php b/src/RuleSet/Php53.php index e28a8a87..a7b49961 100644 --- a/src/RuleSet/Php53.php +++ b/src/RuleSet/Php53.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php53 implements RuleSet +final class Php53 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(5), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -851,41 +834,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php54.php b/src/RuleSet/Php54.php index ad1c3bbf..eabaa39e 100644 --- a/src/RuleSet/Php54.php +++ b/src/RuleSet/Php54.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php54 implements RuleSet +final class Php54 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(5), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -853,41 +836,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php55.php b/src/RuleSet/Php55.php index ee4b5978..6be359b2 100644 --- a/src/RuleSet/Php55.php +++ b/src/RuleSet/Php55.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php55 implements RuleSet +final class Php55 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(5), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -862,41 +845,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php56.php b/src/RuleSet/Php56.php index 9ff49d92..54862c9f 100644 --- a/src/RuleSet/Php56.php +++ b/src/RuleSet/Php56.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php56 implements RuleSet +final class Php56 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(5), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -862,41 +845,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php70.php b/src/RuleSet/Php70.php index fbeccbea..c01925cb 100644 --- a/src/RuleSet/Php70.php +++ b/src/RuleSet/Php70.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php70 implements RuleSet +final class Php70 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(7), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -862,41 +845,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php71.php b/src/RuleSet/Php71.php index b8f60c0c..3874c68d 100644 --- a/src/RuleSet/Php71.php +++ b/src/RuleSet/Php71.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php71 implements RuleSet +final class Php71 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(7), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -865,41 +848,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php72.php b/src/RuleSet/Php72.php index 3e44cc9c..bc6eba58 100644 --- a/src/RuleSet/Php72.php +++ b/src/RuleSet/Php72.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php72 implements RuleSet +final class Php72 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(7), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -865,41 +848,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php73.php b/src/RuleSet/Php73.php index cac0d357..2fe00b36 100644 --- a/src/RuleSet/Php73.php +++ b/src/RuleSet/Php73.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php73 implements RuleSet +final class Php73 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(7), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -866,41 +849,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php74.php b/src/RuleSet/Php74.php index dad0075f..4062de8d 100644 --- a/src/RuleSet/Php74.php +++ b/src/RuleSet/Php74.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php74 implements RuleSet +final class Php74 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(7), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -869,41 +852,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php80.php b/src/RuleSet/Php80.php index 31a97b5f..f9f070b5 100644 --- a/src/RuleSet/Php80.php +++ b/src/RuleSet/Php80.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php80 implements RuleSet +final class Php80 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(8), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -878,41 +861,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php81.php b/src/RuleSet/Php81.php index a95316c7..6c36c872 100644 --- a/src/RuleSet/Php81.php +++ b/src/RuleSet/Php81.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php81 implements RuleSet +final class Php81 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(8), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -880,41 +863,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/src/RuleSet/Php82.php b/src/RuleSet/Php82.php index 3af9b75f..40d4d253 100644 --- a/src/RuleSet/Php82.php +++ b/src/RuleSet/Php82.php @@ -19,26 +19,9 @@ use Ergebnis\PhpCsFixer\Config\Rules; use Ergebnis\PhpCsFixer\Config\RuleSet; -final class Php82 implements RuleSet +final class Php82 { - private readonly Fixers $customFixers; - private readonly Name $name; - private readonly PhpVersion $phpVersion; - private readonly Rules $rules; - - private function __construct( - Fixers $customFixers, - Name $name, - PhpVersion $phpVersion, - Rules $rules, - ) { - $this->customFixers = $customFixers; - $this->name = $name; - $this->phpVersion = $phpVersion; - $this->rules = $rules; - } - - public static function create(): self + public static function create(): RuleSet { $phpVersion = PhpVersion::create( PhpVersion\Major::fromInt(8), @@ -46,7 +29,7 @@ public static function create(): self PhpVersion\Patch::fromInt(0), ); - return new self( + return RuleSet::create( Fixers::empty(), Name::fromString(\sprintf( 'ergebnis (PHP %d.%d)', @@ -880,41 +863,4 @@ public static function create(): self ]), ); } - - public function customFixers(): Fixers - { - return $this->customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - return new self( - $this->customFixers, - $this->name, - $this->phpVersion, - $this->rules->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])), - ); - } } diff --git a/test/Double/Config/RuleSet/DummyRuleSet.php b/test/Double/Config/RuleSet/DummyRuleSet.php deleted file mode 100644 index ad0f2e7c..00000000 --- a/test/Double/Config/RuleSet/DummyRuleSet.php +++ /dev/null @@ -1,59 +0,0 @@ -customFixers; - } - - public function name(): Name - { - return $this->name; - } - - public function phpVersion(): PhpVersion - { - return $this->phpVersion; - } - - public function rules(): Rules - { - return $this->rules; - } - - public function withHeader(string $header): RuleSet - { - throw new \BadMethodCallException(\sprintf( - 'Method "%s" is not implemented yet.', - __METHOD__, - )); - } -} diff --git a/test/Unit/FactoryTest.php b/test/Unit/FactoryTest.php index 7fc81639..4224f582 100644 --- a/test/Unit/FactoryTest.php +++ b/test/Unit/FactoryTest.php @@ -18,6 +18,7 @@ use Ergebnis\PhpCsFixer\Config\Name; use Ergebnis\PhpCsFixer\Config\PhpVersion; use Ergebnis\PhpCsFixer\Config\Rules; +use Ergebnis\PhpCsFixer\Config\RuleSet; use Ergebnis\PhpCsFixer\Config\Test; use PhpCsFixer\Fixer; use PHPUnit\Framework; @@ -30,6 +31,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class FactoryTest extends Framework\TestCase { use Test\Util\Helper; @@ -42,7 +44,7 @@ public function testFromRuleSetThrowsRuntimeExceptionWhenCurrentPhpVersionIsLess PhpVersion\Patch::fromInt(\PHP_RELEASE_VERSION + 1), ); - $ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet( + $ruleSet = RuleSet::create( Fixers::empty(), Name::fromString(self::faker()->word()), $phpVersion, @@ -75,7 +77,7 @@ public function testFromRuleSetCreatesConfigWhenCurrentPhpVersionIsEqualToOrGrea ], ]); - $ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet( + $ruleSet = RuleSet::create( $customFixers, Name::fromString(self::faker()->word()), $targetPhpVersion, @@ -122,7 +124,7 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void ], ]); - $ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet( + $ruleSet = RuleSet::create( $customFixers, Name::fromString(self::faker()->word()), PhpVersion::create( diff --git a/test/Unit/RuleSet/AbstractRuleSetTestCase.php b/test/Unit/RuleSet/AbstractRuleSetTestCase.php index aaf03499..08814a8b 100644 --- a/test/Unit/RuleSet/AbstractRuleSetTestCase.php +++ b/test/Unit/RuleSet/AbstractRuleSetTestCase.php @@ -180,51 +180,6 @@ final public function testHeaderCommentFixerIsDisabledByDefault(): void self::assertFalse($rules->toArray()['header_comment']); } - #[Framework\Attributes\DataProvider('provideValidHeader')] - final public function testWithHeaderReturnsRuleSetWithEnabledHeaderCommentFixer(string $header): void - { - $ruleSet = static::createRuleSet(); - - $mutatedRuleSet = $ruleSet->withHeader($header); - - self::assertNotSame($ruleSet, $mutatedRuleSet); - - self::assertEquals($ruleSet->customFixers(), $mutatedRuleSet->customFixers()); - self::assertEquals($ruleSet->name(), $mutatedRuleSet->name()); - self::assertEquals($ruleSet->phpVersion(), $mutatedRuleSet->phpVersion()); - - $expected = $ruleSet->rules()->merge(Rules::fromArray([ - 'header_comment' => [ - 'comment_type' => 'PHPDoc', - 'header' => \trim($header), - 'location' => 'after_declare_strict', - 'separate' => 'both', - ], - ])); - - self::assertEquals($expected, $mutatedRuleSet->rules()); - } - - /** - * @return \Generator - */ - final public static function provideValidHeader(): \Generator - { - $values = [ - 'string-empty' => '', - 'string-not-empty' => 'foo', - 'string-with-line-feed-only' => "\n", - 'string-with-spaces-only' => ' ', - 'string-with-tab-only' => "\t", - ]; - - foreach ($values as $key => $value) { - yield $key => [ - $value, - ]; - } - } - abstract protected function expectedCustomFixers(): Fixers; abstract protected function expectedName(): Name; diff --git a/test/Unit/RuleSet/Php53Test.php b/test/Unit/RuleSet/Php53Test.php index e644e346..17708e43 100644 --- a/test/Unit/RuleSet/Php53Test.php +++ b/test/Unit/RuleSet/Php53Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php53Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php54Test.php b/test/Unit/RuleSet/Php54Test.php index 032fa00b..a3024393 100644 --- a/test/Unit/RuleSet/Php54Test.php +++ b/test/Unit/RuleSet/Php54Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php54Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php55Test.php b/test/Unit/RuleSet/Php55Test.php index cf177b70..7bb82fa0 100644 --- a/test/Unit/RuleSet/Php55Test.php +++ b/test/Unit/RuleSet/Php55Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php55Test extends ExplicitRuleSetTestCase { protected function expectedCustomFixers(): Fixers diff --git a/test/Unit/RuleSet/Php56Test.php b/test/Unit/RuleSet/Php56Test.php index 11664741..75b51a8c 100644 --- a/test/Unit/RuleSet/Php56Test.php +++ b/test/Unit/RuleSet/Php56Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php56Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php70Test.php b/test/Unit/RuleSet/Php70Test.php index c99e6342..30538d64 100644 --- a/test/Unit/RuleSet/Php70Test.php +++ b/test/Unit/RuleSet/Php70Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php70Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php71Test.php b/test/Unit/RuleSet/Php71Test.php index 3877d4cd..a4f3fe2e 100644 --- a/test/Unit/RuleSet/Php71Test.php +++ b/test/Unit/RuleSet/Php71Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php71Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php72Test.php b/test/Unit/RuleSet/Php72Test.php index 595ed79a..388f16b2 100644 --- a/test/Unit/RuleSet/Php72Test.php +++ b/test/Unit/RuleSet/Php72Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php72Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php73Test.php b/test/Unit/RuleSet/Php73Test.php index 2d86b82e..2e00fbcb 100644 --- a/test/Unit/RuleSet/Php73Test.php +++ b/test/Unit/RuleSet/Php73Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php73Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php74Test.php b/test/Unit/RuleSet/Php74Test.php index 601e3d2f..75ef19a0 100644 --- a/test/Unit/RuleSet/Php74Test.php +++ b/test/Unit/RuleSet/Php74Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php74Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php80Test.php b/test/Unit/RuleSet/Php80Test.php index a261a4de..45fbf650 100644 --- a/test/Unit/RuleSet/Php80Test.php +++ b/test/Unit/RuleSet/Php80Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php80Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php81Test.php b/test/Unit/RuleSet/Php81Test.php index 35bbc401..9e52fe60 100644 --- a/test/Unit/RuleSet/Php81Test.php +++ b/test/Unit/RuleSet/Php81Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php81Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSet/Php82Test.php b/test/Unit/RuleSet/Php82Test.php index 01987dba..533539e6 100644 --- a/test/Unit/RuleSet/Php82Test.php +++ b/test/Unit/RuleSet/Php82Test.php @@ -30,6 +30,7 @@ #[Framework\Attributes\UsesClass(PhpVersion\Minor::class)] #[Framework\Attributes\UsesClass(PhpVersion\Patch::class)] #[Framework\Attributes\UsesClass(Rules::class)] +#[Framework\Attributes\UsesClass(RuleSet::class)] final class Php82Test extends ExplicitRuleSetTestCase { protected static function createRuleSet(): RuleSet diff --git a/test/Unit/RuleSetTest.php b/test/Unit/RuleSetTest.php new file mode 100644 index 00000000..0ac570d5 --- /dev/null +++ b/test/Unit/RuleSetTest.php @@ -0,0 +1,130 @@ +createStub(Fixer\FixerInterface::class), + $this->createStub(Fixer\FixerInterface::class), + $this->createStub(Fixer\FixerInterface::class), + ); + $name = Name::fromString($faker->word()); + $phpVersion = PhpVersion::create( + PhpVersion\Major::fromInt($faker->numberBetween(0)), + PhpVersion\Minor::fromInt($faker->numberBetween(0, 99)), + PhpVersion\Patch::fromInt($faker->numberBetween(0, 99)), + ); + $rules = Rules::fromArray([ + 'header_comment' => false, + ]); + + $ruleSet = RuleSet::create( + $customFixers, + $name, + $phpVersion, + $rules, + ); + + self::assertSame($customFixers, $ruleSet->customFixers()); + self::assertSame($name, $ruleSet->name()); + self::assertSame($phpVersion, $ruleSet->phpVersion()); + self::assertSame($rules, $ruleSet->rules()); + } + + #[Framework\Attributes\DataProvider('provideValidHeader')] + public function testWithHeaderReturnsRuleSetWithEnabledHeaderCommentFixer(string $header): void + { + $faker = self::faker(); + + $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, + 'header_comment' => false, + 'quz' => true, + ]), + ); + + $mutatedRuleSet = $ruleSet->withHeader($header); + + self::assertNotSame($ruleSet, $mutatedRuleSet); + + self::assertEquals($ruleSet->customFixers(), $mutatedRuleSet->customFixers()); + self::assertEquals($ruleSet->name(), $mutatedRuleSet->name()); + self::assertEquals($ruleSet->phpVersion(), $mutatedRuleSet->phpVersion()); + + $expected = $ruleSet->rules()->merge(Rules::fromArray([ + 'header_comment' => [ + 'comment_type' => 'PHPDoc', + 'header' => \trim($header), + 'location' => 'after_declare_strict', + 'separate' => 'both', + ], + ])); + + self::assertEquals($expected, $mutatedRuleSet->rules()); + } + + /** + * @return \Generator + */ + public static function provideValidHeader(): Generator + { + $values = [ + 'string-empty' => '', + 'string-not-empty' => 'foo', + 'string-with-line-feed-only' => "\n", + 'string-with-spaces-only' => ' ', + 'string-with-tab-only' => "\t", + ]; + + foreach ($values as $key => $value) { + yield $key => [ + $value, + ]; + } + } +}