Skip to content

Commit

Permalink
Merge pull request #870 from ergebnis/feature/name
Browse files Browse the repository at this point in the history
Enhancement: Extract `Name` as value object
  • Loading branch information
localheinz authored Sep 16, 2023
2 parents ad0e96d + bf4d590 commit 5a49af4
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 59 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`5.15.1...main`][5.15.1...main].

### Added

- Extracted `Name` as a value object ([#870]), by [@localheinz]

### Fixed

- Fixed target PHP versions in `Config\Ruleset\Php53`, `Config\Ruleset\Php54`, `Config\Ruleset\Php55`, `Config\Ruleset\Php56`, and `Config\Ruleset\Php70` ([#864]), by [@localheinz]
Expand Down Expand Up @@ -1158,6 +1162,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#866]: https://github.com/ergebnis/php-cs-fixer-config/pull/866
[#867]: https://github.com/ergebnis/php-cs-fixer-config/pull/867
[#868]: https://github.com/ergebnis/php-cs-fixer-config/pull/868
[#870]: https://github.com/ergebnis/php-cs-fixer-config/pull/870

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
2 changes: 1 addition & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function fromRuleSet(
));
}

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

$config->setRiskyAllowed(true);
$config->setRules(\array_merge(
Expand Down
41 changes: 41 additions & 0 deletions src/Name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\PhpCsFixer\Config;

final class Name
{
private readonly string $value;

private function __construct(string $value)
{
$this->value = $value;
}

/**
* @throws \InvalidArgumentException
*/
public static function fromString(string $value): self
{
if ('' === \trim($value)) {
throw new \InvalidArgumentException('Value can not be blank or empty.');
}

return new self($value);
}

public function toString(): string
{
return $this->value;
}
}
2 changes: 1 addition & 1 deletion src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface RuleSet
/**
* Returns the name of the rule set.
*/
public function name(): string;
public function name(): Name;

/**
* Returns an array of rules along with their configuration.
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php53.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php53 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -810,9 +812,9 @@ final class Php53 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 5.3)';
return Name::fromString('ergebnis (PHP 5.3)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php54.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php54 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -812,9 +814,9 @@ final class Php54 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 5.4)';
return Name::fromString('ergebnis (PHP 5.4)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php55.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php55 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -821,9 +823,9 @@ final class Php55 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 5.5)';
return Name::fromString('ergebnis (PHP 5.5)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php56.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php56 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -821,9 +823,9 @@ final class Php56 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 5.6)';
return Name::fromString('ergebnis (PHP 5.6)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php70 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -821,9 +823,9 @@ final class Php70 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 7.0)';
return Name::fromString('ergebnis (PHP 7.0)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php71.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php71 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -824,9 +826,9 @@ final class Php71 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 7.1)';
return Name::fromString('ergebnis (PHP 7.1)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php72 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -824,9 +826,9 @@ final class Php72 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 7.2)';
return Name::fromString('ergebnis (PHP 7.2)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php73.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php73 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -825,9 +827,9 @@ final class Php73 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 7.3)';
return Name::fromString('ergebnis (PHP 7.3)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php74 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -828,9 +830,9 @@ final class Php74 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 7.4)';
return Name::fromString('ergebnis (PHP 7.4)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php80 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -837,9 +839,9 @@ final class Php80 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 8.0)';
return Name::fromString('ergebnis (PHP 8.0)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php81 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -839,9 +841,9 @@ final class Php81 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 8.1)';
return Name::fromString('ergebnis (PHP 8.1)');
}

public function rules(): array
Expand Down
6 changes: 4 additions & 2 deletions src/RuleSet/Php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Ergebnis\PhpCsFixer\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config\Name;

final class Php82 extends AbstractRuleSet implements ExplicitRuleSet
{
protected array $rules = [
Expand Down Expand Up @@ -839,9 +841,9 @@ final class Php82 extends AbstractRuleSet implements ExplicitRuleSet
],
];

public function name(): string
public function name(): Name
{
return 'ergebnis (PHP 8.2)';
return Name::fromString('ergebnis (PHP 8.2)');
}

public function rules(): array
Expand Down
9 changes: 5 additions & 4 deletions test/Double/Config/RuleSet/DummyRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

namespace Ergebnis\PhpCsFixer\Config\Test\Double\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\RuleSet;

final class DummyRuleSet implements Config\RuleSet
final class DummyRuleSet implements RuleSet
{
public function __construct(
private readonly string $name,
private readonly Name $name,
private readonly array $rules,
private readonly int $phpVersion,
) {
}

public function name(): string
public function name(): Name
{
return $this->name;
}
Expand Down
8 changes: 5 additions & 3 deletions test/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
namespace Ergebnis\PhpCsFixer\Config\Test\Unit;

use Ergebnis\PhpCsFixer\Config\Factory;
use Ergebnis\PhpCsFixer\Config\Name;
use Ergebnis\PhpCsFixer\Config\Test;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Factory::class)]
#[Framework\Attributes\UsesClass(Name::class)]
final class FactoryTest extends Framework\TestCase
{
use Test\Util\Helper;
Expand All @@ -27,7 +29,7 @@ public function testFromRuleSetThrowsRuntimeExceptionWhenCurrentPhpVersionIsLess
$targetPhpVersion = \PHP_VERSION_ID + 1;

$ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet(
self::faker()->word(),
Name::fromString(self::faker()->word()),
[],
$targetPhpVersion,
);
Expand All @@ -53,7 +55,7 @@ public function testFromRuleSetCreatesConfigWhenCurrentPhpVersionIsEqualToOrGrea
];

$ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet(
self::faker()->word(),
Name::fromString(self::faker()->word()),
$rules,
$targetPhpVersion,
);
Expand Down Expand Up @@ -92,7 +94,7 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void
];

$ruleSet = new Test\Double\Config\RuleSet\DummyRuleSet(
self::faker()->word(),
Name::fromString(self::faker()->word()),
$rules,
\PHP_VERSION_ID,
);
Expand Down
Loading

0 comments on commit 5a49af4

Please sign in to comment.