Skip to content

Commit 7830995

Browse files
committed
Refactor
Auto enable custom fixers.
1 parent 4f7b74a commit 7830995

File tree

6 files changed

+35
-39
lines changed

6 files changed

+35
-39
lines changed

src/Fixers.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
final readonly class Fixers
1010
{
1111
/**
12-
* @var list<FixerInterface>
12+
* @var array<class-string<FixerInterface>, FixerInterface>
1313
*/
1414
public array $value;
1515

1616
public function __construct(
1717
FixerInterface ...$value,
1818
) {
19-
$this->value = array_values($value);
19+
$this->value = array_combine(
20+
array_map(fn($fixer) => $fixer::class, $value),
21+
array_values($value),
22+
);
2023
}
2124

2225
public function merge(self $customFixers) : self

src/NameWrapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
{
1515
public function __construct(
1616
private FixerInterface $inner,
17-
private string $name,
1817
) {}
1918

2019
#[Override]
@@ -44,7 +43,7 @@ public function getDefinition() : FixerDefinitionInterface
4443
#[Override]
4544
public function getName() : string
4645
{
47-
return $this->name;
46+
return 'Custom/' . str_replace('\\', '_', strtolower($this->inner->getName()));
4847
}
4948

5049
#[Override]

src/PhpCsFixerConfigFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class PhpCsFixerConfigFactory
1717
*/
1818
public static function create(RuleSet $ruleSet) : Config
1919
{
20-
$config = new Config($ruleSet->name);
20+
$config = new Config();
2121

2222
$config->setUnsupportedPhpVersionAllowed(true);
2323
$config->registerCustomFixers($ruleSet->customFixers->value);

src/RuleSet.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66

77
final readonly class RuleSet
88
{
9+
public Fixers $customFixers;
10+
public Rules $rules;
11+
912
public function __construct(
10-
public Fixers $customFixers,
11-
public string $name,
12-
public Rules $rules,
13-
) {}
13+
Fixers $customFixers,
14+
Rules $rules,
15+
) {
16+
$this->customFixers = $customFixers;
17+
18+
$enable = [];
19+
foreach ($customFixers->value as $customFixer) {
20+
$enable[$customFixer->getName()] = true;
21+
}
22+
23+
$this->rules = new Rules($enable)->merge($rules);
24+
}
1425

1526
/**
1627
* Returns a new rule set with custom fixers.
@@ -19,7 +30,6 @@ public function withCustomFixers(Fixers $customFixers) : self
1930
{
2031
return new self(
2132
$this->customFixers->merge($customFixers),
22-
$this->name,
2333
$this->rules,
2434
);
2535
}
@@ -31,8 +41,15 @@ public function withRules(Rules $rules) : self
3141
{
3242
return new self(
3343
$this->customFixers,
34-
$this->name,
3544
$this->rules->merge($rules),
3645
);
3746
}
47+
48+
public function merge(self $other) : self
49+
{
50+
return new self(
51+
$this->customFixers->merge($other->customFixers),
52+
$this->rules->merge($other->rules),
53+
);
54+
}
3855
}

src/RuleSet/TicketSwapRuleSet.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,13 @@ public static function create() : RuleSet
5353

5454
return new RuleSet(
5555
new Fixers(
56-
new NameWrapper(
57-
new LineBreakAfterStatementsFixer(),
58-
'ErickSkrauch/line_break_after_statements',
59-
),
60-
new NameWrapper(
61-
new ArrayListItemNewlineFixer($arrayItemNewliner, $arrayAnalyzer, $arrayBlockInfoFinder),
62-
'Symplify/array_list_item_newline_fixer',
63-
),
64-
new NameWrapper(
65-
new ArrayOpenerAndCloserNewlineFixer($arrayBlockInfoFinder, $whitespacesFixerConfig, $arrayAnalyzer),
66-
'Symplify/array_opener_and_closer_newline_fixer',
67-
),
68-
new NameWrapper(
69-
new StandaloneLineInMultilineArrayFixer($arrayWrapperFactory, $tokensNewliner, $blockfinder),
70-
'Symplify/standalone_line_in_multiline_array_fixer',
71-
),
72-
new NameWrapper(
73-
new StandaloneLineConstructorParamFixer($paramNewliner, $methodNameResolver),
74-
'Symplify/standalone_line_constructor_param_fixer',
75-
),
56+
new LineBreakAfterStatementsFixer(),
57+
new NameWrapper(new ArrayListItemNewlineFixer($arrayItemNewliner, $arrayAnalyzer, $arrayBlockInfoFinder)),
58+
new NameWrapper(new ArrayOpenerAndCloserNewlineFixer($arrayBlockInfoFinder, $whitespacesFixerConfig, $arrayAnalyzer)),
59+
new NameWrapper(new StandaloneLineInMultilineArrayFixer($arrayWrapperFactory, $tokensNewliner, $blockfinder)),
60+
new NameWrapper(new StandaloneLineConstructorParamFixer($paramNewliner, $methodNameResolver)),
7661
),
77-
'TicketSwap',
7862
new Rules([
79-
'ErickSkrauch/line_break_after_statements' => true,
80-
'Symplify/array_list_item_newline_fixer' => true,
81-
'Symplify/array_opener_and_closer_newline_fixer' => true,
82-
'Symplify/standalone_line_in_multiline_array_fixer' => true,
83-
'Symplify/standalone_line_constructor_param_fixer' => true,
84-
8563
// Rule sets
8664
'@PER-CS2.0' => true,
8765

src/RuleSet/TicketSwapTestingStyle.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static function create() : RuleSet
1414
{
1515
return new RuleSet(
1616
new Fixers(),
17-
'TicketSwap Testing Style',
1817
new Rules([
1918
'php_unit_method_casing' => [
2019
'case' => 'snake_case',

0 commit comments

Comments
 (0)