Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
Go to file
2019-01-02 14:36:52 +01:00
bin
config/level [PHPUnit] Add UseSpecificWillMethodRector 2018-12-31 22:07:12 +01:00
docs Fix empty class in SimplifyDeMorganBinaryRector [closes #915] 2019-01-02 14:04:29 +01:00
examples
packages use new PHP CS Fixer sort style for function namespaces 2019-01-02 14:22:25 +01:00
src use new PHP CS Fixer sort style for function namespaces 2019-01-02 14:22:25 +01:00
tests use new PHP CS Fixer sort style for function namespaces 2019-01-02 14:22:25 +01:00
utils/phpstan
.coveralls.yml
.editorconfig
.gitattributes
.gitignore
.travis.yml
CODE_OF_CONDUCT.md
composer.json
create-rector.yml.dist
Dockerfile
ecs-after-rector.yml
ecs.yml
LICENSE
phpstan.neon
phpunit.xml
README.md [README] Add Docker link on install [skip ci] 2018-12-31 20:28:56 +01:00

Rector - Upgrade your Legacy App to Modern Codebase

Rector is a reconstructor tool - it does instant upgrades and instant refactoring of your code. I mean, why do it manually if 80 % can Rector handle for you?

Build Status Coverage Status Downloads

Rector-showcase

Rector instantly upgrades and instantly refactors PHP code of your application. It covers many open-source projects and PHP changes itself:



Rector can:

  • Rename classes, methods and properties
  • Rename partial namespace
  • Rename pseudo-namespace to namespace
  • Add, replace or remove arguments
  • Add arguments or return typehint
  • Change visibility of constant, property or method
  • And much more...

...look at overview of all available Rectors with before/after diffs and configuration examples. You can use them to build your own sets.

Install

composer require rector/rector --dev

Do you have conflicts on composer require or on run?

Extra Autoloading

Rector relies on project and autoloading of its classes. To specify own autoload file, use --autoload-file option:

vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php

Or make use of rector.yml config:

# rector.yml
parameters:
    autoload_paths:
        - 'vendor/squizlabs/php_codesniffer/autoload.php'
        - 'vendor/project-without-composer'

You can also exclude files or directories (with regex or fnmatch):

# rector.yml
parameters:
    exclude_paths:
        - '*/src/*/Tests/*'

Running Rector

A. Prepared Sets

Featured open-source projects have prepared sets. You'll find them in /config/level or by calling:

vendor/bin/rector levels

Let's say you pick symfony40 level and you want to upgrade your /src directory:

# show known changes in Symfony 4.0
vendor/bin/rector process src --level symfony40 --dry-run
# apply
vendor/bin/rector process src --level symfony40

Tip: To process just specific subdirectories, you can use fnmatch pattern:

vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run

B. Custom Sets

  1. Create rector.yml with desired Rectors:

    services:
        Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector:
            $annotation: "inject"
    
  2. Run on your /src directory:

    vendor/bin/rector process src --dry-run
    # apply
    vendor/bin/rector process src
    

How to Apply Coding Standards?

AST that Rector uses doesn't deal with coding standards very well, so it's better to let coding standard tools do that. Your project doesn't have one? Rector ships with EasyCodingStandard set that covers namespaces import, 1 empty line between class elements etc.

Just use --with-style option to handle these basic cases:

vendor/bin/rector process src --with-style

More Detailed Documentation

How to Contribute

Just follow 3 rules:

  • 1 feature per pull-request

  • New feature needs tests

  • Tests, coding standards and PHPStan checks must pass:

    composer complete-check
    

    Don you need to fix coding standards? Run:

    composer fix-cs
    

We would be happy to merge your feature then.

Run Rector in Docker

With this command, you can process your project with rector from docker:

docker run -v $(pwd):/project rector/rector:latest