Skip to content

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

  1. Adds missing return types to code when possible
    • To ensure better type safety
  2. Adds missing typecasts
    • Also to ensure better type safety
  3. Makes classes final and readonly
    • To ensure the classes will not be changed by code
    • To ensure a good inheritance architecture
  4. Makes properties private and readonly when possible
    • To ensure the properties will not be accidently changed
  5. Ensures consistent naming for foreach loops
    • To ensure clarity for variable names
  6. 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

  1. Go into the backend directory so rector can find the correct config file rector.php:
    bash
    cd backend
  2. Run Rector on your codebase:
    bash
    vendor/bin/rector process
  3. 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.