Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
Go to file
2019-09-03 14:02:42 +02:00
.github [funding] remove open-collective 2019-07-09 21:46:22 +02:00
bin rename level to set to prevent confusion of duplicate 2019-08-29 22:50:37 +02:00
config [PHPUnit] Add AddSeeTestAnnotationRector 2019-09-03 14:02:42 +02:00
docs rename level to set to prevent confusion of duplicate 2019-08-29 22:50:37 +02:00
packages [PHPUnit] Add AddSeeTestAnnotationRector 2019-09-03 14:02:42 +02:00
src rename getNodeStaticType() to getStaticType() 2019-09-03 10:08:30 +02:00
stubs add Symfony Validator annotations 2019-08-30 23:08:03 +02:00
tests fix test classes names 2019-09-03 14:02:42 +02:00
utils [PHPUnit] Add AddSeeTestAnnotationRector 2019-09-03 14:02:42 +02:00
.coveralls.yml
.dockerignore
.editorconfig
.gitattributes Colorify neon files 2019-07-14 12:07:34 +02:00
.gitignore report old and new table 2019-08-31 12:49:10 +02:00
.phpstorm.meta.php remove getDoctrine*() methods from PhpDocInfo, use getByType() instead 2019-08-31 13:46:48 +02:00
.travis.yml rename level to set to prevent confusion of duplicate 2019-08-29 22:50:37 +02:00
BACKERS.md update CHANGELOG for v0.5.8 2019-07-21 10:46:36 +02:00
changelog-linker.yaml
CHANGELOG.md improve changelog 2019-08-25 13:10:47 +02:00
CODE_OF_CONDUCT.md
composer.json composer: downgrade annotation 1.6 for PHP 7.1 2019-09-02 16:17:13 +02:00
create-rector.yaml.dist rename level to set to prevent confusion of duplicate 2019-08-29 22:50:37 +02:00
Dockerfile
ecs-after-rector.yaml
ecs.yaml add Sensio TemplateTagValueNode 2019-08-30 09:56:56 +02:00
LICENSE
phpstan.neon add AbstarctPriorityAwareTypeInferer 2019-09-02 10:47:09 +02:00
phpunit.xml
README.md rename levels command to sets 2019-08-25 12:19:03 +02:00
rector.yaml decouple EntityWithMissingUuidProvider class 2019-08-31 12:47:41 +02:00

Rector - Upgrade your Legacy App to Modern Codebase

Rector is a reconstructor tool - it does instant upgrades and instant refactoring of your code. Why doing it manually if 80% Rector can handle for you?

Build Status Coverage Status Downloads

Rector-showcase


Sponsors

Rector grows faster with your help, the more you help the more work it saves you. Check out Rector's Patreon. One-time donation is welcomed trough PayPal.

Thank you:


Open-Source First

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



What can Rector do for You?

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

How to Apply Coding Standards?

AST libraries that Rector use, doesn't work well with coding standards, so it's better to let coding standard tools do that.

Your project doesn't have one? Consider adding EasyCodingStandard, PHP CS Fixer or PHP_CodeSniffer.

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.yaml config:

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

Exclude Paths and Rectors

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

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

Do you want to use whole set, except that one rule? Exclude it:

# rector.yaml
parameters:
    exclude_rectors:
        - 'Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector'

By default Rector uses language features of your PHP version. If you you want to use different PHP version than your system, put it in config:

parameters:
    php_version_features: '7.2' # your version 7.3

Running Rector

A. Prepared Sets

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

vendor/bin/rector sets

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

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

B. Custom Sets

  1. Create rector.yaml 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
    

3 Steps to Create Own Rector

First, make sure it's not covered by any existing Rectors yet.

Let's say we want to change method calls from set* to change*.

 $user = new User();
-$user->setPassword('123456');
+$user->changePassword('123456');

1. Create New Rector and Implement Methods

Create class that extends Rector\Rector\AbstractRector. It has useful methods like checking node type and name. Just run $this-> and let PHPStorm show you all possible methods.

<?php declare(strict_types=1);

namespace App\Rector;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Expr\MethodCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class MyFirstRector extends AbstractRector
{
    public function getDefinition(): RectorDefinition
    {
        // what does this do?
        // minimalistic before/after sample - to explain in code
        return new RectorDefinition('Change method calls from set* to change*.', [
            new CodeSample('$user->setPassword("123456");', '$user->changePassword("123456");')
        ]);
    }

    /**
     * @return string[]
     */
    public function getNodeTypes(): array
    {
        // what node types we look for?
        // pick any node from https://github.com/rectorphp/rector/blob/master/docs/NodesOverview.md
        return [MethodCall::class];
    }

    /**
     * @param MethodCall $node - we can add "MethodCall" type here, because only this node is in "getNodeTypes()"
     */
    public function refactor(Node $node): ?Node
    {
        // we only care about "set*" method names
        if (! $this->isName($node, 'set*')) {
            // return null to skip it
            return null;
        }

        $methodCallName = $this->getName($node);
        $newMethodCallName = Strings::replace($methodCallName, '#^set#', 'change');

        $node->name = new Identifier($newMethodCallName);

        // return $node if you modified it
        return $node;
    }
}

2. Register it

# rector.yaml
services:
    App\Rector\MyFirstRector: ~

3. Let Rector Refactor Your Code

# see the diff first
vendor/bin/rector process src --dry-run

# if it's ok, apply
vendor/bin/rector process src

That's it!

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 process /project/src --set symfony40 --dry-run

# Note that a volume is mounted from `pwd` into `/project` which can be accessed later.

Using rector.yaml:

docker run -v $(pwd):/project rector/rector:latest process /project/app --config /project/rector.yaml --autoload-file /project/vendor/autoload.php --dry-run

Community Packages

Do you use Rector to upgrade your code? Share it here: