Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
Go to file
2020-05-05 22:33:00 +02:00
.docker/php Copy .git directory into docker image 2020-04-03 21:30:47 +02:00
.github ci: temporary disable broken commiting workflows 2020-05-04 22:27:26 +02:00
bin autoload reference file before creating container, so phpstan.neon extensions are loaded 2020-04-25 19:39:04 +02:00
ci Add changed files caching 2020-04-20 01:54:17 +02:00
compiler remove unused import 2020-05-05 16:56:04 +02:00
config [Order] Add OrderPropertyByComplexityRector 2020-05-04 02:48:48 +02:00
docs drop FixDataProviderAnnotationTypoRector, fixing typos in single docblock annotation is too narrow 2020-05-05 15:43:55 +02:00
helpers [PHPOffice] Add IncreaseColumnIndexRector 2020-04-16 10:14:21 +02:00
packages improve attribute print 2020-05-05 22:33:00 +02:00
rules improve attribute print 2020-05-05 22:33:00 +02:00
src improve NodeFactory adding @var type via phpstan type 2020-05-05 22:33:00 +02:00
stubs decopule AnnotationItemResolver 2020-04-30 16:53:03 +02:00
tests Fix multi-comment malforming 2020-05-05 00:19:45 +02:00
utils remove preslash custom type 2020-05-05 22:33:00 +02:00
.dockerignore Copy .git directory into docker image 2020-04-03 21:30:47 +02:00
.editorconfig cleanup 2020-04-03 13:24:44 +02:00
.gitattributes do not export compiler as part of package 2020-05-04 19:21:06 +02:00
.gitignore remove scan-fatal-errors, move to migrify 2020-05-03 01:06:31 +02:00
.kodiak.toml add merge section to kodiak 2020-04-29 23:42:29 +02:00
.phpstorm.meta.php add IS_REGULAR_PATTERN constant 2020-04-25 16:41:02 +02:00
.travis.yml build just once 2020-02-27 14:28:50 +01:00
BACKERS.md [SOLID] Add ChangeIfElseValueAssignToEarlyReturnRector 2020-01-06 01:42:15 +01:00
changelog-linker.yaml init CHANGELOG for 0.4.11 2019-04-13 15:16:49 +02:00
CHANGELOG.md update CHANGELOG 2020-04-26 23:32:34 +02:00
CODE_OF_CONDUCT.md Use HTTPS instead of HTTP 2018-02-14 07:23:09 -02:00
composer.json add console-color-diff 2020-05-05 16:49:29 +02:00
CONTRIBUTING.md Move contributing guide to its own file 2020-03-21 14:22:10 +01:00
Dockerfile Remove /dev/null from dockerfile for easier debugging 2020-04-20 10:34:51 +02:00
ecs-after-rector.yaml use explicit null in yaml 2020-01-05 20:00:32 +01:00
ecs.yaml add console-color-diff 2020-05-05 16:49:29 +02:00
LICENSE Update LICENSE year forever 2018-01-02 20:27:07 -02:00
phpstan.neon symplify NodeDumper to PHP code test 2020-05-05 21:38:44 +02:00
phpunit.xml improve PHPStan rules for Rector 2020-05-03 15:02:18 +02:00
README.md README: more clear coding standard section 2020-05-03 14:47:06 +02:00
rector-ci.yaml [cs] update variable names 2020-05-03 13:38:00 +02:00
rector.yaml cleanup rector configs 2020-05-03 12:53:25 +02:00
sonar-project.properties update sonar paths 2020-05-03 23:40:48 +02:00

Rector - Upgrade Your Legacy App to a Modern Codebase

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

Coverage Status Downloads SonarCube



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 donations are welcome through PayPal.

Thank you:


Open-Source First

Rector instantly upgrades and instantly refactors the PHP code of your application.

It supports all versions of PHP from 5.2 and many open-source projects:




What Can Rector Do for You?

How to Apply Coding Standards?

Rector uses nikic/php-parser, that build on technology called abstract syntax tree) technology* (AST). AST doesn't care about spaces and produces mall-formatted code. That's why your project needs to have coding standard tool and set of rules, so it can make refactored nice and shiny again.

Don't have any coding standard tool? Add EasyCodingStandard and use prepared ecs-after-rector.yaml set.

Install

composer require rector/rector --dev
  • Having conflicts during composer require? → Use the Rector Prefixed
  • Using a different PHP version than Rector supports? → Use the Docker image

Running Rector

A. Prepared Sets

Featured open-source projects have prepared sets. You can find them in /config/set or by running:

vendor/bin/rector sets

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

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

Rector will show you diff of files that it would change. To make the changes, run same command without --dry-run:

# apply upgrades to your code
vendor/bin/rector process src --set symfony40

Some sets, such as code-quality can be used on a regular basis. The best practise is to use config over CLI, here in sets parameter:

# rector.yaml
parameters:
    sets:
        - code-quality

B. Standalone Rules

In the end, it's best to combine few of basic sets and drop particular rules that you want to try:

# rector.yaml
parameters:
    sets:
        - code-quality

services:
    Rector\Php74\Rector\Property\TypedPropertyRector: null

Then let Rector refactor your code:

vendor/bin/rector process src

👍


Note: rector.yaml is loaded by default. For different location, use --config option.

Features

Extra Autoloading

Rector relies on whatever autoload setup the project it is fixing is using by using the Composer autoloader as default. To specify your own autoload file, use --autoload-file option:

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

Or use a rector.yaml configuration file:

# 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/*'

You can use a whole ruleset, except one rule:

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

How to Skip Specific Line + Specific Rule?

Use @noRector \FQN name annotation:

class SomeClass
{
    /**
     * @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector
     */
    public function foo()
    {
        /** @noRector \Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector */
        round(1 + 0);
    }
}

Provide PHP Version

By default Rector uses the language features matching your system version of PHP. You can configure it for a different PHP version:

# rector.yaml
parameters:
    php_version_features: '7.2' # your version is 7.3

Paths

If you're annoyed by repeating paths in arguments, you can move them to config instead:

# rector.yaml
parameters:
    paths:
        - 'src'
        - 'tests'

Import Use Statements

FQN classes are not imported by default. If you don't want to do it manually after every Rector run, enable it by:

# rector.yaml
parameters:
    auto_import_names: true

You can also fine-tune how these imports are processed:

# rector.yaml
parameters:
    # this will not import root namespace classes, like \DateTime or \Exception
    import_short_classes: false

    # this will not import classes used in PHP DocBlocks, like in /** @var \Some\Class */
    import_doc_blocks: false

Limit Execution to Changed Files

Execution can be limited to changed files using the process option --match-git-diff. This option will filter the files included by the configuration, creating an intersection with the files listed in git diff.

vendor/bin/rector process src --match-git-diff

This option is useful in CI with pull-requests that only change few files.

Symfony Container

To work with some Symfony rules, you now need to link your container XML file

# rector.yaml
parameters:
    # path to load services from
    symfony_container_xml_path: 'var/cache/dev/AppKernelDevDebugContainer.xml'

More Detailed Documentation

How to Contribute

See the contribution guide.

Run Rector in Docker

You can run Rector on your project using Docker:

docker run -v $(pwd):/project rector/rector:latest process /project/src --set symfony40 --dry-run

# Note that a volume is mounted from `pwd` (the current directory) 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? Add it here: