Josselin Dionisi - Freelance developer

Refactoring PHP code with Rector

ionicons-v5-k Josselin Dionisi Mar 4, 2024
156 reads Level: intermediate

If you've been following this blog for a while, you'll remember that I talked about the importance of updates in a previous post (if you haven't read it, I invite you to go there, but you're welcome to do as you please 😛).

When you've been working on a project for a long time, even if it's in production, you may need to make updates as the resources you're using evolve. In general, we use the LTS of these resources, which stands for "Long Term Service".

For example, if you're using Symfony, the latest LTS version is 6.4, since the recent release of version 7 of the framework.

For PHP, the LTS is 8.2 (8.1 will expire at the end of the year, including security support).

Adapting your code to updates

"That's all great, but why are you listing the versions of these software packages?"

Already because it's important to keep up to date with this kind of thing 😛

But also because reading about it isn't enough. Sometimes you also need to take action to make sure your sites and applications are globally up to date to avoid security loopholes.

Imagine (and this is really the case) that security flaws are discovered in PHP 7 and you still have sites in 7.1. Since updates are no longer guaranteed on this version, your code contains exploitable flaws.

"Oh yes, definitely. What should we do then?"

Sometimes a simple command line is enough, often for what we call minor updates. Symfony, for example, handles this case very well and I've never encountered any problems with this type of update.

But sometimes you have to get your hands into the code and readapt certain things, what we call breaking changes during major updates.

This is precisely what happened to me recently, when I wanted to upgrade Neartrip to Symfony LTS 6.4, if you've been following along 😛

The problem is that between 6.1 and 6.4, PHP annotations are no longer supported, in favor of the recent PHP attributes introduced in PHP 8.

In concrete terms, this meant that I had to go back over each source code file to convert the annotations into attributes. It's a long and tedious job, so I had to find a tool capable of doing this 🙂

Rector, a tool for automating code rewriting

This is how I discovered Rector, a small open source package available on Github. The principle is simple: you give it a set of rules to follow in a rector.php configuration file at the root of your project. You then run a classic command and it will take care of everything.

vendor/bin/rector

Indeed, in the case of Symfony annotations, for example, you may have @Rest/Get or @Route, etc...

You therefore need to list all these different annotations in the rector script as follows:

Rector declaration in php

Once the command has been run and the script completed, the code is automatically transformed 😀

Old PHP attributes

Before

New PHP attributes

After

Practical, isn't it? 🙂

A set of rules available and applicable by Rector are available in the doc if you need them for other uses.

Conclusion

Rector is one of those tools that saves an incredible amount of time on often tedious tasks. Keeping your code up to date is important, but it's not the most fun part of your job either. So finding this kind of little everyday facilitator is really handy, and we can thank the community behind it 😀