Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Turn Config\RuleSet into value object #888

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<code>new FixerFactory()</code>
<code>registerBuiltInFixers</code>
</InternalMethod>
</file>
<file src="test/Unit/RuleSetTest.php">
<PossiblyUnusedMethod>
<code>provideValidHeader</code>
</PossiblyUnusedMethod>
Expand Down
61 changes: 55 additions & 6 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
])),
);
}
}
60 changes: 3 additions & 57 deletions src/RuleSet/Php53.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,17 @@
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),
PhpVersion\Minor::fromInt(3),
PhpVersion\Patch::fromInt(0),
);

return new self(
return RuleSet::create(
Fixers::empty(),
Name::fromString(\sprintf(
'ergebnis (PHP %d.%d)',
Expand Down Expand Up @@ -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',
],
])),
);
}
}
60 changes: 3 additions & 57 deletions src/RuleSet/Php54.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,17 @@
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),
PhpVersion\Minor::fromInt(4),
PhpVersion\Patch::fromInt(0),
);

return new self(
return RuleSet::create(
Fixers::empty(),
Name::fromString(\sprintf(
'ergebnis (PHP %d.%d)',
Expand Down Expand Up @@ -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',
],
])),
);
}
}
60 changes: 3 additions & 57 deletions src/RuleSet/Php55.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,17 @@
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),
PhpVersion\Minor::fromInt(5),
PhpVersion\Patch::fromInt(0),
);

return new self(
return RuleSet::create(
Fixers::empty(),
Name::fromString(\sprintf(
'ergebnis (PHP %d.%d)',
Expand Down Expand Up @@ -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',
],
])),
);
}
}
Loading