Rector - instant upgrades and automated refactoring
https://github.com/rectorphp/rector
Rector instantly upgrades and refactors the PHP code of your application.
Vortex comes with a pre-configured Rector configuration for Drupal projects, based on the configuration provided by Drupal Rector.
Rector refactors your PHP and Drupal code automatically and applies the same rules across the codebase. It fixes deprecated Drupal APIs to prepare the code for Drupal major version upgrades, and rewrites code to use PHP 8.4 language features.
Run Rector before upgrading Drupal major versions to fix deprecations, and periodically during development (weekly or monthly) to catch deprecations early. In code review, it runs in the CI pipeline as one of the code quality checks. When you refactor legacy code, run it to modernize older codebases in bulk.
Rector is often used ad hoc to perform bulk refactoring.
In Vortex, it's integrated as a regular code quality tool that you can run at any time to check for deprecated code and fix issues automatically. Running it continuously keeps the codebase up to date between major upgrades.
Usage
Check for violations
- Ahoy
- Docker Compose
ahoy lint-be
docker compose exec cli vendor/bin/rector --dry-run
Fix violations
- Ahoy
- Docker Compose
# Fix all back-end lint issues (Rector, then PHPCBF).
ahoy lint-be-fix
docker compose exec cli vendor/bin/rector process
Cache management
Rector caches parsed file information and analysis results in the system
temp directory (/tmp/rector_cached_files) to speed up later runs by
reusing results for unchanged files.
Clear the cache after changing the rector.php configuration (rules, skip
list, or paths), after updating the rector/rector or
palantirnet/drupal-rector packages, after switching git branches with
different Rector configurations, or when results look stale.
To clear the cache and check for violations:
- Ahoy
- Docker Compose
ahoy cli vendor/bin/rector process --clear-cache --dry-run
docker compose exec cli vendor/bin/rector process \
--clear-cache --dry-run
To clear the cache and apply changes:
- Ahoy
- Docker Compose
ahoy cli vendor/bin/rector process --clear-cache
docker compose exec cli vendor/bin/rector process --clear-cache
Configuration
All global configuration takes place in
the rector.php
file.
Targets include custom modules and themes, settings, and tests.
Config sets
Rector provides config sets that enable or disable rules in bulk.
Vortex enables the PHP 8.4 sets and the Drupal sets from Drupal
Rector. The Drupal sets are
resolved from the installed drupal/core version, so the rules track core
upgrades without changes to rector.php.
The config sets are meant to be adjusted per-project as needed. A full list of available config sets can be found on the Rules overview page.
Ignoring
See more on the Ignoring Rules Or Paths page.
Ignoring rules globally takes place in the
rector.php file:
->withSkip([
SimplifyIfReturnBoolRector::class,
])
To ignore all Rector rules within a file:
->withSkip([
'file.php',
'*/other_file.php',
])
To ignore a specific rule within a file:
->withSkip([
SimplifyIfReturnBoolRector::class => ['file.php'],
])
Rector doesn't support ignoring code blocks.
Continuous integration
Rector runs in the lint job of the continuous integration
pipeline and fails the build on
violations.
Ignoring failures
Set the VORTEX_CI_RECTOR_IGNORE_FAILURE environment variable to 1. The
tool still runs and reports violations.