Skip to content

Commit 1b5836c

Browse files
committed
1 parent 018d7d7 commit 1b5836c

File tree

11 files changed

+198
-28
lines changed

11 files changed

+198
-28
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/vendor/
2-
/.php_cs.cache
2+
/.php-cs-fixer.cache

.php-cs-fixer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
declare(strict_types=1);
44

55
use PhpCsFixer\Finder;
6-
use Ticketswap\PhpCsFixerConfig\ConfigFactory;
6+
use Ticketswap\PhpCsFixerConfig\PhpCsFixerConfigFactory;
7+
use Ticketswap\PhpCsFixerConfig\RuleSet\TicketSwapRuleSet;
78

89
$finder = Finder::create()
910
->in(__DIR__ . '/src')
1011
->append([__DIR__ . '/.php-cs-fixer.php']);
1112

12-
return ConfigFactory::create()->setFinder($finder);
13+
return PhpCsFixerConfigFactory::create(TicketSwapRuleSet::create())->setFinder($finder);

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ composer require --dev ticketswap/php-cs-fixer-config
1414

1515
Create a [`.php-cs-fixer.php`](.php-cs-fixer.php) file in the root of your project:
1616

17+
<!-- source: .php-cs-fixer.php -->
1718
```php
1819
<?php
1920

2021
declare(strict_types=1);
2122

2223
use PhpCsFixer\Finder;
23-
use Ticketswap\PhpCsFixerConfig\ConfigFactory;
24+
use Ticketswap\PhpCsFixerConfig\PhpCsFixerConfigFactory;
25+
use Ticketswap\PhpCsFixerConfig\RuleSet\TicketSwapRuleSet;
2426

2527
$finder = Finder::create()
2628
->in(__DIR__ . '/src')
27-
->in(__DIR__ . '/tests')
2829
->append([__DIR__ . '/.php-cs-fixer.php']);
2930

30-
return ConfigFactory::create()->setFinder($finder);
31+
return PhpCsFixerConfigFactory::create(TicketSwapRuleSet::create())->setFinder($finder);
3132
```
3233

3334
Adjust the paths in the `Finder` to match your project structure.
@@ -44,4 +45,4 @@ To check for violations without fixing:
4445

4546
```bash
4647
vendor/bin/php-cs-fixer check --diff
47-
```
48+
```

captainhook.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
{
1313
"action": "\\CaptainHook\\App\\Hook\\PHP\\Action\\Linting"
1414
},
15+
{
16+
"action": "\\Ruudk\\ReadmeExamplesSyncHook\\SyncReadmeExamples"
17+
},
1518
{
1619
"action": "vendor/bin/php-cs-fixer fix"
1720
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"require-dev": {
1616
"captainhook/captainhook-phar": "^5.25",
17-
"ergebnis/composer-normalize": "^2.47"
17+
"ergebnis/composer-normalize": "^2.47",
18+
"ruudk/readme-examples-sync-hook": "^1.0"
1819
},
1920
"autoload": {
2021
"psr-4": {

composer.lock

Lines changed: 53 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Fixers.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketswap\PhpCsFixerConfig;
6+
7+
use PhpCsFixer\Fixer\FixerInterface;
8+
9+
final readonly class Fixers
10+
{
11+
/**
12+
* @var list<FixerInterface>
13+
*/
14+
public array $value;
15+
16+
public function __construct(
17+
FixerInterface ...$value,
18+
) {
19+
$this->value = array_values($value);
20+
}
21+
22+
public function merge(self $customFixers) : self
23+
{
24+
return new self(...$this->value, ...$customFixers->value);
25+
}
26+
}

src/PhpCsFixerConfigFactory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketswap\PhpCsFixerConfig;
6+
7+
use PhpCsFixer\Config;
8+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
9+
use RuntimeException;
10+
11+
final class PhpCsFixerConfigFactory
12+
{
13+
/**
14+
* Creates a configuration based on a rule set.
15+
*
16+
* @throws RuntimeException
17+
*/
18+
public static function create(RuleSet $ruleSet) : Config
19+
{
20+
$config = new Config($ruleSet->name);
21+
22+
$config->setUnsupportedPhpVersionAllowed(true);
23+
$config->registerCustomFixers($ruleSet->customFixers->value);
24+
$config->setParallelConfig(ParallelConfigFactory::detect());
25+
$config->setRiskyAllowed(true);
26+
$config->setRules($ruleSet->rules->value);
27+
28+
return $config;
29+
}
30+
}

src/RuleSet.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ticketswap\PhpCsFixerConfig;
6+
7+
final readonly class RuleSet
8+
{
9+
public function __construct(
10+
public Fixers $customFixers,
11+
public string $name,
12+
public Rules $rules,
13+
) {}
14+
15+
/**
16+
* Returns a new rule set with custom fixers.
17+
*/
18+
public function withCustomFixers(Fixers $customFixers) : self
19+
{
20+
return new self(
21+
$this->customFixers->merge($customFixers),
22+
$this->name,
23+
$this->rules,
24+
);
25+
}
26+
27+
/**
28+
* Returns a new rule set with merged rules.
29+
*/
30+
public function withRules(Rules $rules) : self
31+
{
32+
return new self(
33+
$this->customFixers,
34+
$this->name,
35+
$this->rules->merge($rules),
36+
);
37+
}
38+
}

src/ConfigFactory.php renamed to src/RuleSet/TicketSwapRuleSet.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace Ticketswap\PhpCsFixerConfig;
5+
namespace Ticketswap\PhpCsFixerConfig\RuleSet;
66

77
use ErickSkrauch\PhpCsFixer\Fixer\Whitespace\LineBreakAfterStatementsFixer;
8-
use PhpCsFixer\Config;
9-
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
108
use PhpCsFixer\WhitespacesFixerConfig;
119
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
1210
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
@@ -27,13 +25,14 @@
2725
use Symplify\CodingStandard\TokenRunner\Traverser\ArrayBlockInfoFinder;
2826
use Symplify\CodingStandard\TokenRunner\Whitespace\IndentResolver;
2927
use Symplify\CodingStandard\TokenRunner\Wrapper\FixerWrapper\ArrayWrapperFactory;
28+
use Ticketswap\PhpCsFixerConfig\Fixers;
29+
use Ticketswap\PhpCsFixerConfig\NameWrapper;
30+
use Ticketswap\PhpCsFixerConfig\Rules;
31+
use Ticketswap\PhpCsFixerConfig\RuleSet;
3032

31-
final readonly class ConfigFactory
33+
final class TicketSwapRuleSet
3234
{
33-
/**
34-
* @param array<string, array<string, mixed>|bool> $rules
35-
*/
36-
public static function create(array $rules = []) : Config
35+
public static function create() : RuleSet
3736
{
3837
$whitespacesFixerConfig = new WhitespacesFixerConfig(' ', "\n");
3938
$blockfinder = new BlockFinder();
@@ -52,11 +51,8 @@ public static function create(array $rules = []) : Config
5251
$paramNewliner = new ParamNewliner($blockfinder, $tokensNewliner);
5352
$methodNameResolver = new MethodNameResolver();
5453

55-
return new Config()
56-
->setUnsupportedPhpVersionAllowed(true)
57-
->setCacheFile('.php_cs.cache')
58-
->setRiskyAllowed(true)
59-
->registerCustomFixers([
54+
return new RuleSet(
55+
new Fixers(
6056
new NameWrapper(
6157
new LineBreakAfterStatementsFixer(),
6258
'ErickSkrauch/line_break_after_statements',
@@ -77,9 +73,9 @@ public static function create(array $rules = []) : Config
7773
new StandaloneLineConstructorParamFixer($paramNewliner, $methodNameResolver),
7874
'Symplify/standalone_line_constructor_param_fixer',
7975
),
80-
])
81-
->setParallelConfig(ParallelConfigFactory::detect())
82-
->setRules([
76+
),
77+
'TicketSwap',
78+
new Rules([
8379
'ErickSkrauch/line_break_after_statements' => true,
8480
'Symplify/array_list_item_newline_fixer' => true,
8581
'Symplify/array_opener_and_closer_newline_fixer' => true,
@@ -284,8 +280,7 @@ public static function create(array $rules = []) : Config
284280
'identical' => false,
285281
'less_and_greater' => null,
286282
],
287-
288-
...$rules,
289-
]);
283+
]),
284+
);
290285
}
291286
}

0 commit comments

Comments
 (0)