Skip to content

Commit c1558f2

Browse files
authored
[FEATURE] Configure Rector and Fractor with RectorSettings::apply() and FractorSettings::apply() (#152)
* [FEATURE] Configure Rector and Fractor with RectorSettings::apply() and FractorSettings::apply() The templates now pass the configuration builder of Rector and Fractor to apply(), which adds the shared configuration and returns it. Projects configure everything else on the builder. apply() uses the PHP sets of withPhpSets() instead of the per-version set files, which Rector 2.9 deprecates, and the TYPO3 sets up to the lowest TYPO3 version. RectorSettings::sets() uses SetList::PHP_VERSION_BASED_SET instead of LevelSetList and SetList::PHP_*; its $entirety parameter and the one of FractorSettings::sets() are ignored, the sets always contain all upgrades. Older rector.php and fractor.php files keep working. RectorSettings::getLevel() is deprecated and will be removed in 13. * [BUGFIX] Keep the $entirety parameter and use withPhpSets() without a PHP requirement Rector removed the unused $entirety parameter of sets(); passing false now triggers a deprecation instead. withPhpVersion() only accepts the PhpVersion constants, so the fallback without require.php calls withPhpSets() with the version that runs Rector. * [TASK] Document that sets(false) triggers a deprecation * [TASK] Make the builder of apply() optional and deprecate the old helper methods RectorSettings::apply() and FractorSettings::apply() create the builder with RectorConfig::configure() and FractorConfiguration::configure() when none is passed, the templates call apply() without arguments. RectorSettings::sets(), RectorSettings::skip(), FractorSettings::sets(), FractorSettings::rules() and FractorSettings::options() are deprecated, apply() uses private methods instead. RectorSettings::getLevel() and filterOutDeprecatedClasses() are removed. The filter also removed the skip of Configuration/ExtensionBuilder, because it treated the path as a class name. * [TASK] Use the generated rector.php and fractor.php in the packages The packages no longer commit rector.php and fractor.php, they are ignored and the plugin creates them from the templates on composer install. split.sh removes ignored files that are still in the package repositories. * [TASK] Document apply() without arguments and the deprecated helper methods The README explains how projects can keep rector.php and fractor.php out of git when they use the templates unchanged.
1 parent 74239e1 commit c1558f2

20 files changed

Lines changed: 251 additions & 394 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This repository is a monorepo: the shared GrumPHP configuration in the root and
1010
## Rector version and deprecated rules
1111

1212
- `composer.json` and `packages/rector-p/composer.json` allow only one minor version of Rector with a tilde constraint (e.g. `~2.8.0`), because Rector removes deprecated code in minor releases. See [Rector version](CONTRIBUTING.md#rector-version).
13-
- `phpstan/phpstan-deprecation-rules` reports the use of deprecated Rector classes, e.g. rules in `RectorSettings::skip()`. Fix these findings, do not put them into the baseline.
14-
- When you remove a rule from `RectorSettings::skip()` because Rector deprecated it, raise the minimum Rector version in both `composer.json` files to the first version in which the rule is deprecated. `filterOutDeprecatedClasses()` already drops deprecated rules, so the skip entry was only needed for older versions; with the raised minimum they can no longer be installed. If Rector names a replacement rule, skip the replacement and make sure the minimum version contains it. Example: `FlipTypeControlToUseExclusiveTypeRector`, `BinaryOpNullableToInstanceofRector` and `ObjectExplicitBoolCompareRector` are deprecated since Rector 2.8.0, which also brings their replacement `ExplicitNullableObjectInstanceRector`, so the constraint is at least `~2.8.0`.
13+
- `phpstan/phpstan-deprecation-rules` reports the use of deprecated Rector classes, e.g. rules in the skips of `RectorSettings` (`internalSkip()`). Fix these findings, do not put them into the baseline.
14+
- When you remove a rule from the skips because Rector deprecated it, raise the minimum Rector version in both `composer.json` files to the first version in which the rule is deprecated. The skips are not filtered by the installed Rector version, so every skipped rule must exist in the one minor version that the constraint allows. If Rector names a replacement rule, skip the replacement and make sure the minimum version contains it. Example: `FlipTypeControlToUseExclusiveTypeRector`, `BinaryOpNullableToInstanceofRector` and `ObjectExplicitBoolCompareRector` are deprecated since Rector 2.8.0, which also brings their replacement `ExplicitNullableObjectInstanceRector`, so the constraint is at least `~2.8.0`.
1515

1616
## Package repositories and major versions
1717

‎CONTRIBUTING.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Please open issues and pull requests in [andersundsehr/grumphp-config](https://g
1313
| `src/RectorSettings.php`, `src/FractorSettings.php` | Rule sets and skips used by the generated `rector.php` and `fractor.php` |
1414
| `src/VersionUtility.php` | Reads the lowest PHP and TYPO3 version from the project's `composer.json` |
1515
| `grumphp.yml` | The shared GrumPHP configuration that projects import; also the configuration for this repository |
16-
| `rector.php`, `fractor.php`, `phpstan.neon` | Templates that the plugin copies into projects; also the configuration for this repository |
16+
| `rector.php`, `fractor.php`, `phpstan.neon` | Templates that the plugin copies into projects; also the configuration for this repository. The packages do not commit `rector.php` and `fractor.php`, they get them from the plugin |
1717
| `phpstan-disallowed-calls.neon` | Debug calls that PHPStan reports in projects (`spaze/phpstan-disallowed-calls`); `extra.phpstan.includes` in `composer.json` lets `phpstan/extension-installer` load it automatically |
1818
| `phpstan-baseline.neon` | PHPStan baseline of this repository for CI runs with TYPO3 packages; new projects get an empty baseline instead |
1919
| `phpstan-baseline-without-typo3.neon` | PHPStan baseline of this repository for CI runs without TYPO3 packages |
@@ -174,7 +174,7 @@ For each package, `split.sh`
174174

175175
1. clones `github.com/andersundsehr/<package>` into `tmp/<package>`,
176176
2. checks out the branch `<major>.x` of the package repository, e.g. `12.x`. Every major version has its own branch there, `main` is no longer used. The major version comes from the `dev-main as <version>` aliases in `require-dev` of the root `composer.json`. A new branch starts at the last `<major>.*` tag, or at the default branch for a new major version,
177-
3. copies `packages/<package>` into the clone, without files ignored by `.gitignore`,
177+
3. copies `packages/<package>` into the clone, without files ignored by `.gitignore`, and removes ignored files that are still in the package repository,
178178
4. commits the changes with the subject, author and date of the current monorepo commit and a link to it, and pushes them to that branch,
179179
5. if the current monorepo commit has a tag and the package commit has none yet: tags the package commit with the same name, pushes the tag and creates a GitHub release for it.
180180

@@ -188,7 +188,7 @@ The script sets the global git user only if none is configured. Delete `tmp/` af
188188

189189
## Adding a package
190190

191-
1. Create `packages/<name>/` with `composer.json`, `README.md`, `LICENSE.txt`, `.gitignore`, `grumphp.yml`, `phpstan.neon`, `phpstan-baseline.neon` and `rector.php`. Copy the `repositories`, `require-dev`, `config`, `extra.branch-alias` and `scripts` sections from an existing package and set `support.source` to the package folder.
191+
1. Create `packages/<name>/` with `composer.json`, `README.md`, `LICENSE.txt`, `.gitignore`, `grumphp.yml`, `phpstan.neon` and `phpstan-baseline.neon`. Copy the `.gitignore` from an existing package: `rector.php` and `fractor.php` are not committed, the plugin creates them from the templates in the root on `composer install`. Copy the `repositories`, `require-dev`, `config`, `extra.branch-alias` and `scripts` sections from an existing package and set `support.source` to the package folder.
192192
2. Require the package in the root `composer.json`: in `require` with the current version constraint and in `require-dev` as `dev-main as <version>`.
193193
3. Add the package to the `rm -rf` lists in `grumphp-run-all.sh`.
194194
4. Add the package to the `test-packages` and `code-split` matrices in the workflow.

‎README.md‎

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ Composer asks whether it may run the Composer plugins that come with this packag
7878

7979
`rector.php` and `fractor.php` are also created after `composer dump-autoload`.
8080

81+
### Using the templates unchanged
82+
83+
If you do not need any changes in `rector.php` or `fractor.php`, you can delete the file and add it to your `.gitignore`:
84+
85+
```gitignore
86+
/rector.php
87+
/fractor.php
88+
```
89+
90+
The plugin creates the files again after every `composer install`, `composer update` and `composer dump-autoload`, so everyone who installs the project gets them, and after deleting them you get the current templates. The packages in this repository do it this way. As soon as you need your own skips, sets or rules, remove the line from `.gitignore` and commit the file.
91+
8192
In addition, on every `composer install` and `composer update` the plugin
8293

8394
- adds `convention.*` parameters that are new in the installed version to your `grumphp.yml`, as long as the first import in that file is `vendor/pluswerk/grumphp-config/grumphp.yml`. Your own values stay unchanged. The file is rewritten in this case, so YAML comments in it are lost.
@@ -214,28 +225,36 @@ Add more [GrumPHP tasks](https://github.com/phpro/grumphp/blob/v2.x/doc/tasks.md
214225

215226
## Rector
216227

217-
The generated `rector.php`
228+
The generated `rector.php` calls `RectorSettings::apply()` and returns the Rector configuration builder:
229+
230+
```php
231+
return RectorSettings::apply()
232+
->withSkip([
233+
__DIR__ . '/src/Example',
234+
]);
235+
```
218236

237+
`RectorSettings::apply()`
238+
239+
- creates the builder with `RectorConfig::configure()`; to configure a builder of your own, pass it as the first parameter,
219240
- checks all PHP files that are tracked in git,
220241
- runs in parallel and caches results in `var/cache/rector`,
221242
- imports class names and removes unused imports,
222-
- uses the rule sets from `RectorSettings::sets(true)` and skips the rules from `RectorSettings::skip()`.
223-
224-
Add your own sets, rules and skips to `rector.php`. The file belongs to your project.
225-
226-
### `RectorSettings::sets(bool $entirety = false)`
243+
- uses the prepared sets `deadCode`, `codeQuality`, `codingStyle`, `typeDeclarations` and `privatization`,
244+
- uses the PHP sets (`withPhpSets()`) for the lowest PHP version that your `composer.json` allows in `require.php` (for `^8.2` that is PHP 8.2). Without a PHP requirement the PHP version that runs Rector is used,
245+
- in TYPO3 projects: uses the TYPO3 sets up to the lowest TYPO3 version that your `composer.json` allows in `require.typo3/cms-core`, plus the TYPO3 `CODE_QUALITY` and `GENERAL` sets. TYPO3 10 to 14 are supported, `dev-main` counts as 14,
246+
- skips the [rules below](#skipped-rules).
227247

228-
Returns these sets:
248+
The file belongs to your project. Call more methods of the builder after `apply()`:
229249

230-
- `CODE_QUALITY`, `CODING_STYLE`, `DEAD_CODE`, `PRIVATIZATION`, `TYPE_DECLARATION`
231-
- the PHP set for the lowest PHP version that your `composer.json` allows in `require.php` (for `^8.2` that is PHP 8.2). Without a PHP requirement the PHP version that runs Rector is used.
232-
- in TYPO3 projects: the TYPO3 set for the lowest TYPO3 version that your `composer.json` allows in `require.typo3/cms-core`, plus the TYPO3 `CODE_QUALITY` and `GENERAL` sets. TYPO3 10 to 14 are supported, `dev-main` counts as 14.
250+
- `withSkip()`, `withSets()` and `withRules()` add to the configuration.
251+
- `withPaths()` and `withCache()` replace it.
252+
- `withPhpSets()` can only be called once. To use your own PHP version, call `RectorSettings::apply(phpSets: false)` and then `withPhpSets(php83: true)`.
253+
- `RectorSettings::apply(typo3: false)` leaves out the TYPO3 sets.
233254

234-
With `$entirety = true` (the default in the generated file) the "up to" level sets are used, e.g. `UP_TO_PHP_82` instead of `PHP_82`, so all upgrades up to that version are checked.
255+
### Skipped rules
235256

236-
### `RectorSettings::skip()`
237-
238-
Skips rules that change code in ways we do not want:
257+
`RectorSettings::apply()` skips rules that change code in ways we do not want:
239258

240259
| Rule | Reason |
241260
| --- | --- |
@@ -251,15 +270,22 @@ In TYPO3 projects it also skips:
251270
- name importing in `ClassAliasMap.php`,
252271
- `declare(strict_types=1)` in `ext_emconf.php`.
253272

254-
Skip entries for rules that no longer exist in the installed Rector version, or that Rector marks as deprecated, are removed automatically. This keeps the configuration working across Rector versions.
273+
### Older configuration files
255274

256-
### `RectorSettings::getLevel(int $level = 9999)`
275+
Older `rector.php` and `fractor.php` files build the configuration from the methods below. They keep working in version 12, but they are marked as `@deprecated` and will be removed in version 13, so PHPStan reports these calls (see [PHPStan](#phpstan)):
257276

258-
Helper for introducing Rector step by step. It returns the number given with `--level=N` on the Rector command line, or `$level` if there is none. It removes the option from the arguments, so Rector does not reject it, and passes the value to Rector's parallel worker processes through the environment variable `RECTOR_CLI_OPTION_LEVEL`. Use the return value in your `rector.php` wherever Rector expects a level.
277+
| Method | Returns |
278+
| --- | --- |
279+
| `RectorSettings::sets()` | The prepared sets above, `SetList::PHP_VERSION_BASED_SET` and the TYPO3 sets |
280+
| `RectorSettings::skip()` | The [skipped rules](#skipped-rules) |
281+
| `RectorSettings::setsTypo3()`, `RectorSettings::skipTypo3()` | Empty arrays, the TYPO3 sets and skips are part of `sets()` and `skip()` |
282+
| `FractorSettings::sets()` | The TYPO3 set |
283+
| `FractorSettings::rules()` | The rule for projects without TYPO3 |
284+
| `FractorSettings::options()` | The TypoScript options |
259285

260-
### Deprecated methods
286+
`RectorSettings::sets(false)` and `FractorSettings::sets(false)` are no longer supported and also trigger a deprecation at runtime, the sets always contain all upgrades.
261287

262-
`RectorSettings::setsTypo3()` and `RectorSettings::skipTypo3()` return empty arrays. The TYPO3 sets and skips are part of `sets()` and `skip()` now. Older `rector.php` files may still call them; you can remove these calls. Both are marked as `@deprecated`, so PHPStan reports these calls (see [PHPStan](#phpstan)).
288+
To switch to `apply()`, replace your `rector.php` and `fractor.php` with the templates ([`rector.php`](rector.php), [`fractor.php`](fractor.php)) and move your own skips, sets and rules into them. Without changes of your own, [delete them and add them to `.gitignore`](#using-the-templates-unchanged).
263289

264290
### Introducing Rector in large projects
265291

@@ -273,11 +299,14 @@ vendor/bin/rector-p
273299

274300
[Fractor](https://github.com/andreaswolf/fractor) refactors non-PHP files in TYPO3 projects, such as TypoScript, FlexForms and YAML. The generated `fractor.php`
275301

302+
- calls `FractorSettings::apply()` and returns the Fractor configuration builder, like `rector.php`; to configure a builder of your own, pass it to `apply()`,
276303
- checks all files that are tracked in git,
277-
- uses the TYPO3 set for the lowest TYPO3 version that your `composer.json` allows (`FractorSettings::sets(true)`),
278-
- formats TypoScript with 2 spaces, indented conditions and conditions closed with `[end]` (`FractorSettings::options()`).
304+
- uses the TYPO3 sets up to the lowest TYPO3 version that your `composer.json` allows,
305+
- formats TypoScript with 2 spaces, indented conditions and conditions closed with `[end]`.
306+
307+
Call more methods of the builder after `apply()`, e.g. `withSkip()`. `withOptions()` replaces the options of `apply()`. Older `fractor.php` files keep working, see [Older configuration files](#older-configuration-files).
279308

280-
In projects without TYPO3 there are no sets. Because Fractor needs at least one rule, `FractorSettings::rules()` then adds a single TypoScript rule, which has no effect on projects without TypoScript.
309+
In projects without TYPO3 there are no sets. Because Fractor needs at least one rule, `apply()` then adds a single TypoScript rule, which has no effect on projects without TypoScript.
281310

282311
## PHPStan
283312

@@ -329,7 +358,7 @@ composer update pluswerk/grumphp-config --with-all-dependencies
329358

330359
- Changes to the shared `grumphp.yml` take effect immediately.
331360
- New `convention.*` parameters are added to your `grumphp.yml` automatically.
332-
- `rector.php`, `fractor.php`, `phpstan.neon` and `phpstan-baseline.neon` are not updated, because they belong to your project. To get the current templates, delete the files and run `composer update` again; then move your own changes back into the new files and regenerate the baseline.
361+
- `rector.php`, `fractor.php`, `phpstan.neon` and `phpstan-baseline.neon` are not updated, because they belong to your project. To get the current templates, delete the files and run `composer update` again; then move your own changes back into the new files and regenerate the baseline. `rector.php` and `fractor.php` without changes of your own can stay out of git, see [Using the templates unchanged](#using-the-templates-unchanged).
333362

334363
### From 11 to 12
335364

@@ -340,7 +369,7 @@ composer require --dev pluswerk/grumphp-config:^12.0.0 --with-all-dependencies
340369
- Version 12 installs [`phpstan/phpstan-deprecation-rules`](#phpstan), so PHPStan reports the use of deprecated code.
341370
- Version 12 installs [`spaze/phpstan-disallowed-calls`](#phpstan) with a list of debug functions, so PHPStan reports calls like `var_dump()` or `DebuggerUtility::var_dump()`.
342371
- Version 12 installs [`phpstan/phpstan-phpunit`](#phpstan), so PHPStan understands PHPUnit tests better and checks some assertions.
343-
- Remove the calls of `RectorSettings::setsTypo3()` and `RectorSettings::skipTypo3()` from your `rector.php`, see [Deprecated methods](#deprecated-methods).
372+
- Switch your `rector.php` and `fractor.php` to `apply()`, see [Older configuration files](#older-configuration-files).
344373
- Fix the other findings, or add them to `phpstan-baseline.neon` with `vendor/bin/phpstan analyse --generate-baseline`.
345374

346375
## Packages in this repository

‎fractor.php‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22

33
declare(strict_types=1);
44

5-
use a9f\Fractor\Configuration\FractorConfiguration;
65
use PLUS\GrumPHPConfig\FractorSettings;
76

8-
return FractorConfiguration::configure()
9-
->withPaths(array_filter(explode("\n", (string)shell_exec("git ls-files | xargs ls -d 2>/dev/null"))))
10-
->withSets([
11-
...FractorSettings::sets(true),
12-
])
13-
->withRules([
14-
...FractorSettings::rules(),
15-
])
16-
->withOptions([
17-
...FractorSettings::options(),
7+
return FractorSettings::apply()
8+
// add your own sets, rules and skips, see https://github.com/andreaswolf/fractor
9+
->withSkip([
10+
// fractor should not touch these files
11+
//__DIR__ . '/Resources/Private/Example',
1812
]);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/vendor/
22
composer.lock
33
var/
4+
# created by pluswerk/grumphp-config on composer install, see "Using the template unchanged" in its README
5+
/rector.php
6+
/fractor.php

‎packages/grumphp-bom-task/rector.php‎

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/vendor/
22
composer.lock
33
var/
4+
# created by pluswerk/grumphp-config on composer install, see "Using the template unchanged" in its README
5+
/rector.php
6+
/fractor.php

‎packages/grumphp-fractor-task/fractor.php‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎packages/grumphp-fractor-task/rector.php‎

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/vendor/
22
composer.lock
33
var/
4+
# created by pluswerk/grumphp-config on composer install, see "Using the template unchanged" in its README
5+
/rector.php
6+
/fractor.php

0 commit comments

Comments
 (0)