Automatically upgrade your PHP code from 5.3 to PHP 7.4 with Rector

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

Installation per Composer:

composer require rector/rector --dev

For example, to upgrade the contents of the ./src folder:

vendor/bin/rector process src --set php74

The cool thing is that Rector uses sets, of which it has many. Upgrading from SwiftMailer 5 to 6 or from Twig 1 to Twig 2.4.0 for example? There are sets available for those.

Configuration through a rector.yaml is also possible.

A Docker image to run Rector is also available:

docker pull rector/rector:latest

docker run -v $(pwd):/project rector/rector:latest process /project/src --set phpunit8
docker run -v $(pwd):/project rector/rector:latest process /project/src --config="/project/rector.yaml" --autoload-file /project/vendor/autoload.php

Rector →
rectorphp/rector on GitHub →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.