Rector PHP
Rector is a tool for automated refactoring of PHP code. It allows us to apply a set of predefined rules to our codebase to improve its quality and apply specific coding standards.
Examples
Some examples of what rector will do to our codebase
- Adds missing return types to code when possible
- To ensure better type safety
- Adds missing typecasts
- Also to ensure better type safety
- Makes classes final and readonly
- To ensure the classes will not be changed by code
- To ensure a good inheritance architecture
- Makes properties private and readonly when possible
- To ensure the properties will not be accidently changed
- Ensures consistent naming for foreach loops
- To ensure clarity for variable names
- It will apply property promotion to class constructors when possible
- To ensure more readable and managable code
Key Features
- Automated refactoring: Rector automatically refactors our code to improve its quality and maintainability.
- Integration: Rector is integrated into our CI/CD pipeline to ensure consistent code quality.
Usage
- Go into the backend directory so rector can find the correct config file
rector.php:bashcd backend - Run Rector on your codebase:bash
vendor/bin/rector process - If you want a test run you can use:bash
vendor/bin/rector process --dry-run
On auto-save
Rector will run on auto save through the run on save extension. The command is defined in .vscode/settings.json
For more information, visit the official documentation.