Work with Rector - VilnaCRM-Org/user-service GitHub Wiki
make rector-apply
- command for applying rector
make rector-dry-run
- show diffs
Currently, there is a powerful tool installed in the project that automatically refactors code according to modern PHP standards and supported versions.
To understand how it works, it is recommended to read the documentation first:
βοΈ Configuration
The process is controlled via the rector.php config file located in the root directory of the project. This is the central file that defines Rector's behavior. Before starting, make sure rector.php is present and configured based on your needs.
It is best to manually create and adjust this file using the official documentation.
π How to Use
- Install Rector
composer require rector/rector --dev
- Run Rector
vendor/bin/rector
If the config file is missing, Rector will ask to generate one:
No "rector.php" config found. Should we generate it for you? [yes]:
yes
[OK] The config is added now. Re-run command to make Rector do the work!
- Example Config File (rector.php)
<?php
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
return RectorConfig::configure()
`->withPaths([`
`__DIR__ . '/src',`
`__DIR__ . '/tests',`
`])`
`->withPreparedSets(deadCode: true);`
This file returns a RectorConfig object via configure() method with a chained setup. In this example:
-
Paths to refactor: src and tests
-
Enabled set: deadCode
Itβs recommended to use prepared sets, especially if you're using popular frameworks and composer based method which will look in composer for needed framework version and predefined rules.
- Run Refactoring
vendor/bin/rector process
This will apply all rules defined in rector.php to the specified folders.
- (Optional) Preview Changes Without Modifying Code
vendor/bin/rector process --dry-run
The --dry-run flag displays changes without saving them. You can also customize the output:
--output-format=json|github|gitlab|console
`--no-progress-bar β hide progress bar (useful in CI)`
π§© Common CLI Options
- -n, --dry-run Only show diffs, do not save changes
- -a, --autoload-file=FILE Path to an additional autoload file
- --no-progress-bar Hide the progress bar (useful for CI)
- --no-diffs Hide diffs (useful for CI)
- --output-format=FORMAT Output format: console, json, github, gitlab, etc.
- --only=RULE Run only a specific rule (FQCN)
- --only-suffix=SUFFIX Apply only to files with specific suffix (e.g. "Controller")
- --debug Enable debug output (-vvv)
- --memory-limit=LIMIT Set memory limit
- --clear-cache Clear Rector cache
- --xdebug Enable xdebug
- -c, --config=FILE Path to config file (default: rector.php in root)
- -v|vv|vvv Increase verbosity
- -h, --help Show help
π Find All Rules