-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECSConfig.php
More file actions
167 lines (149 loc) · 5.57 KB
/
ECSConfig.php
File metadata and controls
167 lines (149 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Config;
use FastForward\DevTools\Path\WorkingProjectPathResolver;
use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer;
use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer;
use PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer;
use PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocAddMissingParamAnnotationFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer;
use Symplify\EasyCodingStandard\Configuration\ECSConfigBuilder;
use function Safe\getcwd;
/**
* Provides the default ECS configuration.
*
* Consumers can use this as a starting point and extend it:
*
* $config = \FastForward\DevTools\Config\ECSConfig::configure();
* $config->withRules([CustomRule::class]);
* $config->withConfiguredRule(PhpdocAlignFixer::class, ['align' => 'right']);
* return $config;
*
* @see https://github.com/symplify/easy-coding-standard
*/
final class ECSConfig
{
/**
* @var array{symfony: bool, symfonyRisky: bool, auto: bool, autoRisky: bool} the PHP-CS-Fixer sets applied by default
*/
public const array DEFAULT_PHP_CS_FIXER_SETS = [
'symfony' => true,
'symfonyRisky' => true,
'auto' => true,
'autoRisky' => true,
];
/**
* @var array{psr12: bool, common: bool, symplify: bool, strict: bool, cleanCode: bool} the prepared ECS sets applied by default
*/
public const array DEFAULT_PREPARED_SETS = [
'psr12' => true,
'common' => true,
'symplify' => true,
'strict' => true,
'cleanCode' => true,
];
/**
* @var list<class-string> the ECS/CS Fixer rules that SHOULD be skipped by default
*/
public const array DEFAULT_SKIPPED_RULES = [
PhpdocToCommentFixer::class,
NoSuperfluousPhpdocTagsFixer::class,
NoEmptyPhpdocFixer::class,
PhpdocNoEmptyReturnFixer::class,
GlobalNamespaceImportFixer::class,
GeneralPhpdocAnnotationRemoveFixer::class,
];
/**
* @var array<class-string, array<string, mixed>> the configured ECS rules applied by default
*/
public const array DEFAULT_CONFIGURED_RULES = [
PhpdocAlignFixer::class => [
'align' => 'left',
],
PhpUnitTestCaseStaticMethodCallsFixer::class => [
'call_type' => 'self',
],
PhpdocAddMissingParamAnnotationFixer::class => [
'only_untyped' => false,
],
];
/**
* Creates the default ECS configuration.
*
* @param callable|null $customize optional callback to customize the configuration builder
*
* @return ECSConfigBuilder the configured ECS configuration builder
*/
public static function configure(?callable $customize = null): ECSConfigBuilder
{
$workingDirectory = getcwd();
$config = new ECSConfigBuilder();
self::applyDefaultPathsAndSkips($config, $workingDirectory);
self::applyDefaultRulesAndSets($config);
if (null !== $customize) {
$customize($config);
}
return $config;
}
/**
* Applies the default repository paths and skipped rules to an ECS builder.
*
* @param ECSConfigBuilder $config
* @param string $workingDirectory
*/
public static function applyDefaultPathsAndSkips(
ECSConfigBuilder $config,
string $workingDirectory
): ECSConfigBuilder {
$paths = WorkingProjectPathResolver::getToolingSourcePaths($workingDirectory);
$skipPaths = WorkingProjectPathResolver::getToolingExcludedDirectories();
return $config
->withPaths($paths)
->withSkip([...$skipPaths, ...self::DEFAULT_SKIPPED_RULES]);
}
/**
* Applies the default ECS sets, root files, and configured rules to an ECS builder.
*
* @param ECSConfigBuilder $config
*/
public static function applyDefaultRulesAndSets(ECSConfigBuilder $config): ECSConfigBuilder
{
$config
->withRootFiles()
->withPhpCsFixerSets(
symfony: self::DEFAULT_PHP_CS_FIXER_SETS['symfony'],
symfonyRisky: self::DEFAULT_PHP_CS_FIXER_SETS['symfonyRisky'],
auto: self::DEFAULT_PHP_CS_FIXER_SETS['auto'],
autoRisky: self::DEFAULT_PHP_CS_FIXER_SETS['autoRisky'],
)
->withPreparedSets(
psr12: self::DEFAULT_PREPARED_SETS['psr12'],
common: self::DEFAULT_PREPARED_SETS['common'],
symplify: self::DEFAULT_PREPARED_SETS['symplify'],
strict: self::DEFAULT_PREPARED_SETS['strict'],
cleanCode: self::DEFAULT_PREPARED_SETS['cleanCode'],
);
foreach (self::DEFAULT_CONFIGURED_RULES as $rule => $configuration) {
$config->withConfiguredRule($rule, $configuration);
}
return $config;
}
}