rector/rector

Upgrade your application with CLI without any deprecations knowledge.

v0.3.8 2018-09-16 17:30 UTC

README

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 Downloads

Rector-showcase

Rector instantly upgrades PHP & YAML code of your application, with focus on open-source projects:

symfony.png space.png sylius.png space.png phpunit.jpg space.png twig.png

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...

...just look at overview of all available Rectors with before/after diffs and configuration examples. It's all you really need to use build your own sets.

Install

composer require rector/rector:@dev --dev

Do you have conflicts on composer require?

Install prefixed version with isolated dependencies.

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_files:
        - '%kernel.project_dir%/vendor/squizlabs/php_codesniffer/autoload.php'

    autoload_directories:
        - '%kernel.project_dir%/vendor/project-without-composer'

You can also exclude files or directories - use regex or fnmatch:

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

How to Reconstruct your Code

A. Prepared Sets

Featured open-source projects have prepared sets. You'll find them in /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
  3. To process just specific subdirectories, you can use fnmatch pattern with *:

    vendor/bin/rector process "src/Symfony/Component/*/Tests" --level phpunit60 --dry-run
  4. What levels are on the board?

    vendor/bin/rector levels

B. Custom Sets

  1. Create rector.yml with desired Rectors:

    services:
        Rector\Nette\Rector\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

How to Apply Coding Standards?

Rector uses EasyCodingStandard to improve the code style of changed files, like import namespaces, make 1 empty line between class elements etc.

It's always better to use own project's prepared set, but if you don't have the option yet, 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.