A fast, AST-based PHP code refactoring CLI tool written in Rust. Automatically applies transformations like adding final keywords, enforcing code standards, and more — with zero manual review needed.
- AST-powered: Parses PHP into an abstract syntax tree for safe, accurate transformations (not regex-based)
- Fast: Single-threaded, compiled Rust binary — processes files in milliseconds
- Extensible: Add new rules as pure functions; all rules are automatically tested against fixtures
- Zero false positives: Each rule includes fixtures for what it transforms and what it skips
- Composable: Rules chain together; you can apply multiple transformations in one pass
git clone https://github.com/yoannblot/php-refactor.git
cd php-refactor
cargo build --releaseBinary will be at target/release/php-refactor.
docker build -t php-refactor .
docker run --rm -v $(pwd):/workspace php-refactor \
/app/target/release/php-refactor /workspace/MyClass.phpThe tool accepts a single argument: a file path, directory path, or config file path.
Process one PHP file:
php-refactor MyClass.phpExample:
$ cat MyClass.php
<?php
class MyClass {
public function hello() {}
}
$ php-refactor MyClass.php
$ cat MyClass.php
<?php
final class MyClass {
public function hello() {}
}Recursively process all PHP files in a directory:
php-refactor src/
php-refactor tests/Process multiple directories defined in a TOML config file:
php-refactor config.tomlconfig.toml:
[source]
paths = ["src", "tests", "app"]Paths can be:
- Relative to the project root:
"src","./app" - Absolute:
"/home/user/project/src" - Nested:
"src/components/php"
The tool will recursively walk each path and apply all rules to every .php file found.
The tool modifies files in-place. If no rules apply, files are left unchanged.
The tool prints a summary:
[INFO] Refactoring took 12.34ms (peak memory: 2.1MB) | quality/add_final_keyword: 5.67ms
- Timing: Wall-clock time for the entire operation and per-rule
- Memory: Peak RSS memory usage (macOS/Linux only; 0 on other platforms)
| Rule | What It Does |
|---|---|
quality/add_final_keyword |
Adds final keyword to concrete classes |
quality/add_readonly_keyword |
Adds readonly keyword to bare concrete classes |
quality/remove_redundant_readonly_keyword |
Removes redundant readonly from properties and promoted params inside readonly classes |
See docs/rules.md for detailed rule documentation.
- Fork the repository
- Create a feature branch
- Add rules or fix bugs (see docs/rules.md for the implementation guide)
- Run
just quality-checkto ensure all checks pass - Open a pull request
See LICENSE file in the repository.