Skip to content

Commit ba2e178

Browse files
authored
extend config configuration with all options (#3455)
1 parent ea3e2a7 commit ba2e178

File tree

1 file changed

+253
-4
lines changed

1 file changed

+253
-4
lines changed
Lines changed: 253 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,270 @@
11
## Provide Config
22

3-
By default, Rector picks `rector.php` in your root directory as a configuration file. To change that, use `--config` option in CLI run:
3+
By default, Rector picks `rector.php` in your project root as the configuration file.
4+
To change that, use:
45

56
```bash
67
vendor/bin/rector process --config rector-custom-config.php
78
```
89

10+
<br>
11+
12+
**Pro tip**: You can see current default values in the Rector config file:
13+
14+
* on Github: [config/config.php](https://github.com/rectorphp/rector-src/blob/main/config/config.php)
15+
* or locally in: `/vendor/rector/rector/config/config.php`
16+
17+
<br>
18+
919
## Spacing and Indents
1020

11-
By default, Rector prints code with 4 spaces indent and unix newline.
12-
If you have other preference, change it:
21+
Default formatting is **4 spaces** and **UNIX newlines**.
22+
You can override:
23+
24+
```php
25+
use Rector\Config\RectorConfig;
26+
27+
return RectorConfig::configure()
28+
->withIndent(indentChar: ' ', indentSize: 4);
29+
```
30+
31+
<br>
32+
33+
## Paths to Process
34+
35+
Define which directories or files Rector should refactor:
1336

1437
```php
1538
<?php
1639

1740
use Rector\Config\RectorConfig;
1841

1942
return RectorConfig::configure()
20-
->withIndent(indentChar: ' ', indentSize: 4);
43+
->withPaths([
44+
__DIR__ . '/src',
45+
__DIR__ . '/tests',
46+
__DIR__ . '/packages/Domain',
47+
]);
48+
```
49+
50+
You can also include all the `*.php` files from root directory:
51+
52+
```php
53+
use Rector\Config\RectorConfig;
54+
55+
return RectorConfig::configure()
56+
->withPaths([
57+
__DIR__ . '/src',
58+
])
59+
->withRootFiles();
60+
```
61+
62+
<br>
63+
64+
## File Extensions
65+
66+
By default, Rector processes only `.php` files. To add more extensions, use:
67+
68+
```php
69+
use Rector\Config\RectorConfig;
70+
71+
return RectorConfig::configure()
72+
->withFileExtensions(['php', 'phtml']);
73+
```
74+
75+
<br>
76+
77+
## PHP Version
78+
79+
Rector reads the PHP version from `composer.json`. It's very rate to use different PHP version than one provided by `composer.json`, as it might use newer syntax and break your code. If you still need different version, you can override it manually:
80+
81+
```php
82+
use Rector\ValueObject\PhpVersion;
83+
84+
return RectorConfig::configure()
85+
->withPhpVersion(PhpVersion::PHP_81);
86+
```
87+
88+
<br>
89+
90+
## Using Sets
91+
92+
Enable built-in Rector sets:
93+
94+
```php
95+
use Rector\Config\RectorConfig;
96+
97+
return RectorConfig::configure()
98+
->withPreparedSets(deadCode: true, codeQuality: true);
99+
```
100+
101+
Check [Levels](/documentation/levels) and [Set List](/documentation/set-lists) for more.
102+
103+
<br>
104+
105+
## Composer-Based Sets
106+
107+
Rector can auto-enable sets based on installed packages:
108+
109+
```php
110+
use Rector\Config\RectorConfig;
111+
112+
return RectorConfig::configure()
113+
->withComposerBased(
114+
symfony: true,
115+
doctrine: true,
116+
phpunit: true,
117+
);
118+
```
119+
120+
<br>
121+
122+
## Skipping Rules or Paths
123+
124+
```php
125+
use Rector\Config\RectorConfig;
126+
127+
return RectorConfig::configure()
128+
->withSkip([
129+
__DIR__ . '/src/Legacy/*',
130+
__DIR__ . '/tests/Fixtures',
131+
]);
132+
```
133+
134+
Skip specific rule:
135+
136+
```php
137+
use Rector\Config\RectorConfig;
138+
139+
return RectorConfig::configure()
140+
->withSkip([
141+
SomeRule::class,
142+
]);
143+
```
144+
145+
Skip rule for specific files:
146+
147+
```php
148+
use Rector\Config\RectorConfig;
149+
150+
return RectorConfig::configure()
151+
->withSkip([
152+
SomeRule::class => [
153+
__DIR__ . '/src/SpecialCase.php',
154+
],
155+
]);
156+
```
157+
158+
<br>
159+
160+
## Parallel Execution
161+
162+
Rector runs in parallel mode by default. You can customize the parallel execution like this:
163+
164+
```php
165+
use Rector\Config\RectorConfig;
166+
167+
return RectorConfig::configure()
168+
->withParallel(
169+
timeoutSeconds: 600,
170+
maxNumberOfProcesses: 8,
171+
jobSize: 20,
172+
);
173+
```
174+
175+
Or disable:
176+
177+
```php
178+
use Rector\Config\RectorConfig;
179+
180+
return RectorConfig::configure()
181+
->withoutParallel();
182+
```
183+
184+
<br>
185+
186+
## Cache & Temp Directories
187+
188+
By default, Rector uses `sys_get_temp_dir() . '/rector_cached_files'` path to store cache files. E.g. to verify if files were changed since last run, to run only on new or changed files. You can customize it:
189+
190+
```php
191+
use Rector\Config\RectorConfig;
192+
193+
return RectorConfig::configure()
194+
->withCache(__DIR__ . '/var/rector');
195+
```
196+
197+
198+
199+
<br>
200+
201+
## Autoloading & Bootstrap
202+
203+
Rector loads project `vendor/autoload.php` by default. In case you don't use composer autoload or some of your `/vendor` dependencies are not loaded, you can include directories:
204+
205+
```php
206+
use Rector\Config\RectorConfig;
207+
208+
return RectorConfig::configure()
209+
->withAutoloadPaths([
210+
__DIR__ . '/lib',
211+
__DIR__ . '/vendor/legacy/package/src',
212+
]);
213+
```
214+
215+
You can also include exact files that contain e.g. constant definitions:
216+
217+
```php
218+
use Rector\Config\RectorConfig;
219+
220+
return RectorConfig::configure()
221+
->withBootstrapFiles([
222+
__DIR__ . '/config/constants.php',
223+
]);
224+
```
225+
226+
<br>
227+
228+
## PHPStan integration
229+
230+
Rector load `phpstan.neon` and `phpstan.neon.dist` by default if they exist in your project root. Extensions are ignored on purpose, as some of them run project code (e.g. Doctrine) and breaks idea of static analysis. Most extensions do not bring any value to Rector, as Rector works mostly with native type declaration.
231+
232+
Still, there is a way to load PHPStan extension configs, in case they are needed for your specific Rector rule use case:
233+
234+
```php
235+
use Rector\Config\RectorConfig;
236+
237+
return RectorConfig::configure()
238+
->withPHPStanConfigs([
239+
__DIR__ . '/config/custom-phpstan-extension.neon',
240+
]);
241+
```
242+
243+
<br>
244+
245+
## Symfony Integration
246+
247+
Some [Symfony Rector rules](https://getrector.com/find-rule?activeRectorSetGroup=symfony) require container metadata. Provide Symfony Container to let Rector access services (name and type mostly) and route definitions.
248+
249+
If your Symfony project was not run yet, dump the container first:
250+
251+
```bash
252+
bin/console debug:container
253+
```
254+
255+
Use in config:
256+
257+
```php
258+
use Rector\Config\RectorConfig;
259+
260+
return RectorConfig::configure()
261+
// in XML
262+
->withSymfonyContainerXml(
263+
__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml'
264+
);
265+
266+
// or PHP (depending on version)
267+
->withSymfonyContainerPhp(
268+
__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.php'
269+
);
21270
```

0 commit comments

Comments
 (0)