Skip to content

Commit

Permalink
Merge pull request #886 from ergebnis/feature/named
Browse files Browse the repository at this point in the history
Enhancement: Extract named constructor
  • Loading branch information
localheinz committed Sep 18, 2023
2 parents 34fffc3 + 4ba852a commit 3b18622
Show file tree
Hide file tree
Showing 28 changed files with 249 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Expand Up @@ -26,7 +26,7 @@

$license->save();

$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php81($license->header()));
$config = PhpCsFixer\Config\Factory::fromRuleSet(PhpCsFixer\Config\RuleSet\Php81::create($license->header()));

$config->getFinder()
->exclude([
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,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]

### Fixed

Expand Down Expand Up @@ -1196,6 +1197,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#881]: https://github.com/ergebnis/php-cs-fixer-config/pull/881
[#883]: https://github.com/ergebnis/php-cs-fixer-config/pull/883
[#885]: https://github.com/ergebnis/php-cs-fixer-config/pull/885
[#886]: https://github.com/ergebnis/php-cs-fixer-config/pull/886

[@dependabot]: https://github.com/apps/dependabot
[@linuxjuggler]: https://github.com/linuxjuggler
Expand Down
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -50,7 +50,7 @@ declare(strict_types=1);

use Ergebnis\PhpCsFixer\Config;

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php82());
$config = Config\Factory::fromRuleSet(Config\RuleSet\Php82::create());

$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
Expand Down Expand Up @@ -87,8 +87,8 @@ All configuration examples use the caching feature, and if you want to use it as
+@see https://github.com/ergebnis/php-cs-fixer-config
+EOF;

-$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php82());
+$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php82($header));
-$config = Config\Factory::fromRuleSet(Config\RuleSet\Php82::create());
+$config = Config\Factory::fromRuleSet(Config\RuleSet\Php82::create($header));

$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
Expand Down Expand Up @@ -125,8 +125,8 @@ 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\Rules::fromArray([
-$config = Config\Factory::fromRuleSet(Config\RuleSet\Php82::create());
+$config = Config\Factory::fromRuleSet(Config\RuleSet\Php82::create(), Config\Rules::fromArray([
+ 'mb_str_functions' => false,
+ 'strict_comparison' => false,
+]));
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php53.php
Expand Up @@ -26,7 +26,19 @@ final class Php53 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -846,10 +858,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php54.php
Expand Up @@ -26,7 +26,19 @@ final class Php54 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -848,10 +860,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php55.php
Expand Up @@ -26,7 +26,19 @@ final class Php55 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -857,10 +869,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php56.php
Expand Up @@ -26,7 +26,19 @@ final class Php56 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -857,10 +869,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php70.php
Expand Up @@ -26,7 +26,19 @@ final class Php70 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -857,10 +869,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php71.php
Expand Up @@ -26,7 +26,19 @@ final class Php71 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -860,10 +872,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php72.php
Expand Up @@ -26,7 +26,19 @@ final class Php72 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -860,10 +872,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php73.php
Expand Up @@ -26,7 +26,19 @@ final class Php73 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -861,10 +873,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down
24 changes: 19 additions & 5 deletions src/RuleSet/Php74.php
Expand Up @@ -26,7 +26,19 @@ final class Php74 implements RuleSet
private readonly PhpVersion $phpVersion;
private readonly Rules $rules;

public function __construct(?string $header = null)
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(?string $header = null): self
{
$fixers = Fixers::empty();
$phpVersion = PhpVersion::create(
Expand Down Expand Up @@ -864,10 +876,12 @@ public function __construct(?string $header = null)
]));
}

$this->customFixers = $fixers;
$this->name = $name;
$this->phpVersion = $phpVersion;
$this->rules = $rules;
return new self(
$fixers,
$name,
$phpVersion,
$rules,
);
}

public function customFixers(): Fixers
Expand Down

0 comments on commit 3b18622

Please sign in to comment.