Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.84% covered (warning)
86.84%
33 / 38
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RectorConfig
86.84% covered (warning)
86.84%
33 / 38
0.00% covered (danger)
0.00%
0 / 2
9.18
0.00% covered (danger)
0.00%
0 / 1
 configure
89.66% covered (warning)
89.66%
26 / 29
0.00% covered (danger)
0.00%
0 / 1
5.03
 applySafeMigrationSet
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
4.18
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Config;
21
22use Composer\InstalledVersions;
23use Ergebnis\Rector\Rules\Faker\GeneratorPropertyFetchToMethodCallRector;
24use FastForward\DevTools\Rector\AddMissingMethodPhpDocRector;
25use FastForward\DevTools\Rector\RemoveEmptyDocBlockRector;
26use FastForward\DevTools\Path\ManagedWorkspace;
27use FastForward\DevTools\Path\WorkingProjectPathResolver;
28use Rector\Config\RectorConfig as RectorConfigInterface;
29use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
30use Rector\DeadCode\Rector\ClassMethod\RemoveMixedDocblockOverruledByNativeTypeRector;
31use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
32use Rector\Set\ValueObject\SetList;
33use function Safe\getcwd;
34
35/**
36 * Provides the default Rector configuration.
37 *
38 * Consumers can use this as a starting point and extend it:
39 *
40 *     return \FastForward\DevTools\Config\RectorConfig::configure(
41 *         static function (\Rector\Config\RectorConfig $rectorConfig): void {
42 *             $rectorConfig->rules([
43 *                 // custom rules
44 *             ]);
45 *         }
46 *     );
47 *
48 * @see https://github.com/rectorphp/rector
49 */
50 class RectorConfig
51{
52    /**
53     * @var list<string> stable default Rector sets; configure() also loads legacy sets on older Rector
54     */
55    public const array DEFAULT_SETS = [
56        SetList::DEAD_CODE,
57        SetList::CODE_QUALITY,
58        SetList::CODING_STYLE,
59        SetList::TYPE_DECLARATION,
60        SetList::PRIVATIZATION,
61    ];
62
63    /**
64     * @var list<class-string> the default Rector rules applied on top of the configured sets
65     */
66    public const array DEFAULT_RULES = [
67        GeneratorPropertyFetchToMethodCallRector::class,
68        AddMissingMethodPhpDocRector::class,
69        RemoveEmptyDocBlockRector::class,
70    ];
71
72    /**
73     * @var list<class-string> the Rector rules that SHOULD be skipped by default
74     */
75    public const array DEFAULT_SKIPPED_RULES = [
76        RemoveUselessReturnTagRector::class,
77        RemoveUselessParamTagRector::class,
78        RemoveMixedDocblockOverruledByNativeTypeRector::class,
79    ];
80
81    /**
82     * Creates the default Rector configuration.
83     *
84     * @param callable|null $customize optional callback to customize the configuration
85     *
86     * @return callable the configuration callback
87     */
88    public static function configure(?callable $customize = null): callable
89    {
90        return static function (RectorConfigInterface $rectorConfig) use ($customize): void {
91            $workingDirectory = getcwd();
92            $paths = WorkingProjectPathResolver::getToolingSourcePaths();
93            $skipPaths = WorkingProjectPathResolver::getToolingExcludedDirectories();
94            $skipRules = self::DEFAULT_SKIPPED_RULES;
95
96            $sets = self::DEFAULT_SETS;
97
98            // On older Rector releases the rules still live in these sets.
99            // Newer releases include them in the stable sets and removed the constants.
100            foreach (['INSTANCEOF', 'EARLY_RETURN'] as $legacySet) {
101                $constant = SetList::class . '::' . $legacySet;
102                if (\defined($constant)) {
103                    $set = \constant($constant);
104                    if (\is_string($set)) {
105                        $sets[] = $set;
106                    }
107                }
108            }
109
110            $rectorConfig->sets($sets);
111            $rectorConfig->paths($paths);
112            $rectorConfig->skip([...$skipPaths, ...$skipRules]);
113            $rectorConfig->cacheDirectory(
114                ManagedWorkspace::getCacheDirectory(ManagedWorkspace::RECTOR, $workingDirectory)
115            );
116            $rectorConfig->importNames();
117            $rectorConfig->removeUnusedImports();
118            $rectorConfig->fileExtensions(['php']);
119            $rectorConfig->parallel(600);
120            $rectorConfig->rules(self::DEFAULT_RULES);
121
122            $phpSets = RectorConfigInterface::configure()->withPhpSets();
123            $phpSets($rectorConfig);
124
125            self::applySafeMigrationSet($rectorConfig);
126
127            if (null !== $customize) {
128                $customize($rectorConfig);
129            }
130        };
131    }
132
133    /**
134     * Applies the optional Safe migration callback when the package is installed.
135     *
136     * @param RectorConfigInterface $rectorConfig
137     */
138    public static function applySafeMigrationSet(RectorConfigInterface $rectorConfig): void
139    {
140        if (! InstalledVersions::isInstalled('thecodingmachine/safe', false)) {
141            return;
142        }
143
144        $packageLocation = InstalledVersions::getInstallPath('thecodingmachine/safe');
145        $safeRectorMigrateFile = $packageLocation . '/rector-migrate.php';
146
147        if (! file_exists($safeRectorMigrateFile)) {
148            return;
149        }
150
151        $callback = require_once $safeRectorMigrateFile;
152
153        if (\is_callable($callback)) {
154            $callback($rectorConfig);
155        }
156    }
157}