Skip to content

About

Rector in Rust - fixes PHP code, built on mago-syntax

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Rector in Rust

Rector in Rust fixes PHP code. It is a Rust port of reco, built on the mago-syntax PHP parser.

rector run --rule finalize-classes-without-children,add-private-method-void-return-type src
-class Logger
+final class Logger
 {
-    private function record($message)
+    private function record($message): void
     {
         $this->messages[] = $message;
     }
 }

Install

cargo install --git https://github.com/rectorphp/rector-in-rust

Usage

rector run [--rule <name>[,<name>...]] [--dry-run] [--php-version 8.1] <path> ...
rector list-rules
  • With no --rule, every rule runs.
  • The PHP version is read from composer.json in the working directory (require.php, falling back to config.platform.php), unless --php-version is given. A rule needing a newer PHP version does nothing.
  • vendor/, node_modules/, var/, .git/ and .idea/ are skipped. A file that does not parse is reported and left untouched.

How it works

Every file is parsed with mago-syntax. Rules walk the tree and return byte-range edits instead of reprinting it, so whitespace, comments and formatting survive. Files are processed in parallel, in two passes: the first collects project-wide facts (which classes are extended or mocked), the second applies the rules.

Rules

Rule Change
array-long-to-short array(1, 2) => [1, 2]
finalize-classes-without-children marks a class final when nothing in the scanned paths extends or mocks it, and it is not a Doctrine entity
add-private-method-void-return-type adds : void to a private method that never returns a value or yields, and does not always throw (PHP 7.1+)
add-protected-method-void-return-type the same for a protected method, unless a subclass overrides it with one returning a value, it is an empty hook in a non-final class, or it sits in or next to a trait
add-public-method-void-return-type the same for a public method

Development

cargo test
cargo clippy --all-targets -- -D warnings
cargo fmt

Rector parity

Every rector-src test fixture of the Rector rules listed in parity/ported.txt runs through the rules ported from them, on every PR. Each fixture's outcome - match, skip, format, declined, diverge, overreach or error - is checked against the parity/rector-fixtures.txt snapshot, at the rector-src commit pinned in parity/rector-src-commit.txt.

RECTOR_SRC=../rector-src cargo test --test parity -- --nocapture
RECTOR_SRC=../rector-src UPDATE_SNAPSHOT=1 cargo test --test parity
RECTOR_SRC=../rector-src RECO_SNAPSHOT=../reco/parity/rector-fixtures.txt cargo test --test parity -- --nocapture

The last one lists the fixtures where reco does better, the next rules to port.

Each rule has fixtures in tests/fixtures/<rule-name>/*.php.inc: the input, then ----- and the expected output. A fixture with no ----- must stay unchanged.

About

Rector in Rust - fixes PHP code, built on mago-syntax

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages