-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectorConfig.php
More file actions
148 lines (129 loc) · 4.95 KB
/
RectorConfig.php
File metadata and controls
148 lines (129 loc) · 4.95 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
<?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 Composer\InstalledVersions;
use Ergebnis\Rector\Rules\Faker\GeneratorPropertyFetchToMethodCallRector;
use FastForward\DevTools\Rector\AddMissingMethodPhpDocRector;
use FastForward\DevTools\Rector\RemoveEmptyDocBlockRector;
use FastForward\DevTools\Path\ManagedWorkspace;
use FastForward\DevTools\Path\WorkingProjectPathResolver;
use Rector\Config\RectorConfig as RectorConfigInterface;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\Php\PhpVersionResolver\ComposerJsonPhpVersionResolver;
use Rector\Configuration\PhpLevelSetResolver;
use Rector\Set\ValueObject\SetList;
use function Safe\getcwd;
/**
* Provides the default Rector configuration.
*
* Consumers can use this as a starting point and extend it:
*
* return \FastForward\DevTools\Config\RectorConfig::configure(
* static function (\Rector\Config\RectorConfig $rectorConfig): void {
* $rectorConfig->rules([
* // custom rules
* ]);
* }
* );
*
* @see https://github.com/rectorphp/rector
*/
final class RectorConfig
{
/**
* @var list<string> the default Rector sets applied to Fast Forward projects
*/
public const array DEFAULT_SETS = [
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
SetList::INSTANCEOF,
SetList::EARLY_RETURN,
];
/**
* @var list<class-string> the default Rector rules applied on top of the configured sets
*/
public const array DEFAULT_RULES = [
GeneratorPropertyFetchToMethodCallRector::class,
AddMissingMethodPhpDocRector::class,
RemoveEmptyDocBlockRector::class,
];
/**
* @var list<class-string> the Rector rules that SHOULD be skipped by default
*/
public const array DEFAULT_SKIPPED_RULES = [
RemoveUselessReturnTagRector::class,
RemoveUselessParamTagRector::class,
];
/**
* Creates the default Rector configuration.
*
* @param callable|null $customize optional callback to customize the configuration
*
* @return callable the configuration callback
*/
public static function configure(?callable $customize = null): callable
{
return static function (RectorConfigInterface $rectorConfig) use ($customize): void {
$workingDirectory = getcwd();
$paths = WorkingProjectPathResolver::getToolingSourcePaths();
$skipPaths = WorkingProjectPathResolver::getToolingExcludedDirectories();
$skipRules = self::DEFAULT_SKIPPED_RULES;
$rectorConfig->sets(self::DEFAULT_SETS);
$rectorConfig->paths($paths);
$rectorConfig->skip([...$skipPaths, ...$skipRules]);
$rectorConfig->cacheDirectory(
ManagedWorkspace::getCacheDirectory(ManagedWorkspace::RECTOR, $workingDirectory)
);
$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();
$rectorConfig->fileExtensions(['php']);
$rectorConfig->parallel(600);
$rectorConfig->rules(self::DEFAULT_RULES);
$projectPhpVersion = ComposerJsonPhpVersionResolver::resolveFromCwdOrFail();
$phpLevelSets = PhpLevelSetResolver::resolveFromPhpVersion($projectPhpVersion);
$rectorConfig->sets($phpLevelSets);
self::applySafeMigrationSet($rectorConfig);
if (null !== $customize) {
$customize($rectorConfig);
}
};
}
/**
* Applies the optional Safe migration callback when the package is installed.
*
* @param RectorConfigInterface $rectorConfig
*/
public static function applySafeMigrationSet(RectorConfigInterface $rectorConfig): void
{
if (! InstalledVersions::isInstalled('thecodingmachine/safe', false)) {
return;
}
$packageLocation = InstalledVersions::getInstallPath('thecodingmachine/safe');
$safeRectorMigrateFile = $packageLocation . '/rector-migrate.php';
if (! file_exists($safeRectorMigrateFile)) {
return;
}
$callback = require_once $safeRectorMigrateFile;
if (\is_callable($callback)) {
$callback($rectorConfig);
}
}
}