Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
Go to file
Gabriel Caruso e5f6f1a871
Update LICENSE year forever
2018-01-02 20:27:07 -02:00
bin Add strict_types 2017-12-30 17:51:37 -02:00
docs How UseWithOldPhp.md to use dev-master 2018-01-02 00:21:12 -02:00
packages Clean up packages 2017-12-31 17:08:49 -02:00
src Use getPathname 2018-01-02 15:23:42 -02:00
tests Merge pull request #238 from carusogabriel/fix-misspeling 2017-12-27 12:32:30 +01:00
.coveralls.yml init meta files 2017-07-15 19:12:04 +02:00
.gitignore temp: add git keep 2017-09-29 12:25:56 +02:00
.travis.yml travis: split phpstan and cs 2017-12-26 23:40:42 +01:00
CODE_OF_CONDUCT.md init meta files 2017-07-15 19:12:04 +02:00
LICENSE Update LICENSE year forever 2018-01-02 20:27:07 -02:00
README.md Clean up documentation 2018-01-02 04:19:20 -02:00
composer.json Remove rector.php from composer bin section 2017-12-30 18:35:54 -02:00
easy-coding-standard.neon fix cs 2017-12-27 13:28:44 +01:00
ecs-after-rector.neon make tests pass 2017-11-09 03:33:17 +01:00
phpstan.neon phpstan fixes 2017-12-27 14:13:59 +01:00
phpunit.xml phpunit - narrow config 2017-12-22 10:41:40 +01:00

README.md

Rector - Upgrade your Legacy App to Modern Codebase

Build Status Coverage Status

Rector upgrades your application for you, with focus on open-source projects:

Rector can:

Install

composer require --dev rector/rector 'dev-master' nikic/php-parser '4.0.x-dev'

Do you have old PHP or dependencies in conflict? Ok, it is not a problem.

How to Reconstruct your Code

A. Prepared Sets

Featured open-source projects have prepared sets. You'll find them in /src/config/level.

Do you need to upgrade to Symfony 4.0, for example?

  1. Run rector on your /src directory:

    vendor/bin/rector process src --level symfony40
    

    Which is a shortcut for using complete path with --config option:

    vendor/bin/rector process src --config vendor/rector/rector/src/config/level/symfony/symfony40.yml
    

    You can also use your own config file:

    vendor/bin/rector process src --config your-own-config.yml
    
  2. Do you want to see the preview of changes first?

    Use the --dry-run option:

    vendor/bin/rector process src --level symfony33 --dry-run
    

B. Custom Sets

  1. Create rector.yml with desired Rectors:

    rectors:
        - Rector\Rector\Contrib\Nette\Application\InjectPropertyRector
    
  2. Try Rector on your /src directory:

    vendor/bin/rector process src --dry-run
    
  3. Apply the changes if you like them:

    vendor/bin/rector process src
    

Simple setup with Dynamic Rectors

You don't have to always write PHP code. Many projects change only classes or method names, so it would be too much work for a simple task.

Instead you can use prepared Dynamic Rectors directly in *.yml config:

You can:

Replace a class name

# phpunit60.yml
rectors:
    Rector\Rector\Dynamic\ClassReplacerRector:
        # old class: new class
        'PHPUnit_Framework_TestCase': 'PHPUnit\Framework\TestCase'

Replace some part of the namespace

# better-reflection20.yml
rectors:
    Rector\Rector\Dynamic\NamespaceReplacerRector:
        # old namespace: new namespace
        'BetterReflection': 'Roave\BetterReflection'

Change a method name

rectors:
    Rector\Rector\Dynamic\MethodNameReplacerRector:
        # class
        'Nette\Utils\Html':
            # old method: new method
            'add': 'addHtml'

        # or in case of static methods calls

        # class
        'Nette\Bridges\FormsLatte\FormMacros':
            # old method: [new class, new method]
            'renderFormBegin': ['Nette\Bridges\FormsLatte\Runtime', 'renderFormBegin']

Change a property name

rectors:
    Rector\Rector\Dynamic\PropertyNameReplacerRector:
        # class:
        'PhpParser\Node\Param':
            # old property: new property
            'name': 'var'

Change a class constant name

rectors:
    Rector\Rector\Dynamic\ClassConstantReplacerRector:
        # class
        'Symfony\Component\Form\FormEvents':
            # old constant: new constant
            'PRE_BIND': 'PRE_SUBMIT'
            'BIND': 'SUBMIT'
            'POST_BIND': 'POST_SUBMIT'

Change parameters type hinting according to the parent type

rectors:
    Rector\Rector\Dynamic\ParentTypehintedArgumentRector:
        # class
        'PhpParser\Parser':
            # method
            'parse':
                # parameter: typehint
                'code': 'string'

Change a argument value

rectors:
    Rector\Rector\Dynamic\ArgumentReplacerRector:
        # class
        'Symfony\Component\DependencyInjection\ContainerBuilder':
            # method
            'compile':
                # argument position
                0:
                    # added default value
                    '~': false
                    # or remove completely
                    '~': ~
                    # or replace by new value
                    'Symfony\Component\DependencyInjection\ContainerBuilder\ContainerBuilder::SCOPE_PROTOTYPE': false

Replace the underscore naming _ with namespaces \

rectors:
    Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector:
        # old namespace prefix
        - 'PHPUnit_'
        # exclude classes
        - '!PHPUnit_Framework_MockObject_MockObject'

Modify a property to method

rectors:
    Rector\Rector\Dynamic\PropertyToMethodRector:
        # type
        'Symfony\Component\Translation\Translator':
            # property to replace
            'locale':
                # (prepared key): get method name
                'get': 'getLocale'
                # (prepared key): set method name
                'set': 'setLocale'

Remove a value object and use simple type

rectors:
    Rector\Rector\Dynamic\ValueObjectRemoverRector:
        # type: new simple type
        'ValueObjects\Name': 'string'

For example:

- $value = new ValueObjects\Name('Tomas');
+ $value = 'Tomas';
/**
-* @var ValueObjects\Name
+* @var string
 */
private $name;
- public function someMethod(ValueObjects\Name $name) { ...
+ public function someMethod(string $name) { ...

Turn Magic to Methods

Replace get/set magic methods with real ones

rectors:
    Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
        # class
        'Nette\DI\Container':
            # magic method (prepared keys): new real method
            'get': 'getService'
            'set': 'addService'

For example:

- $result = $container['key'];
+ $result = $container->getService('key');
- $container['key'] = $value;
+ $container->addService('key', $value);

Replace isset/unset magic methods with real ones

rectors:
    Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
        # class
        'Nette\DI\Container':
            # magic method (prepared keys): new real method
            'isset': 'hasService'
            'unset': 'removeService'

For example:

- isset($container['key']);
+ $container->hasService('key');
- unset($container['key']);
+ $container->removeService('key');

Replace toString magic method with real one

rectors:
    Rector\Rector\MagicDisclosure\ToStringToMethodCallRector:
        # class
        'Symfony\Component\Config\ConfigCache':
            # magic method (prepared key): new real method
            'toString': 'getPath'

For example:

- $result = (string) $someValue;
+ $result = $someValue->someMethod();
- $result = $someValue->__toString();
+ $result = $someValue->someMethod();

Coding Standards are Outsourced

This package has no intention in formatting your code, as coding standard tools handle this much better.

We prefer EasyCodingStandard that is already available:

# check
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon

# fix
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon --fix

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.