- GitHub - rectorphp/rector: Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
- Instant Upgrades and Automated Refactoring of any PHP 5.3+ code - rectorphp/rector
This post has been translated by AI.
Post summarized by durumis AI
- This article demonstrates how to use the PHP version upgrade tool, Rector, to migrate PHP 5.3 code to PHP 7.4.
- After installing Composer, install Rector in your project and specify the target code and PHP version in the rector.php configuration file.
- By setting up and running the modification rules, you can automatically modify the code and manage the changes using Git for a safe upgrade process.
While browsing a Gnuboard 5 site, a Korean PHP CMS, I came across several posts expressing difficulties related to deprecated syntax and versions after a PHP version upgrade.
So, I started looking around...
Upon visiting Reddit, I discovered that a tool called Rector can assist with migration, and I'd like to introduce its usage.
Since the release of PHP 7.0 in 2015, PHP migration tools have emerged, including php-cs-fixer and Rector.
I find Rector to be more user-friendly.
Rector (pronounced: Rector) inspects and modifies PHP code from versions 5.3 to 8.2, adapting it to the desired version's syntax.
One drawback of Rector is that, being a static analyzer, it cannot detect runtime errors.
Rector requires Composer, the PHP package manager, to be installed.
1. Install Composer.
2. Install Rector in your current project folder.
3. Create the configuration file, rector.php.
rector.php Configuration
To illustrate, let's define a scenario.
We'll assume the target source code is written in PHP 5.3, and the upgrade target version is PHP 7.4. We'll proceed based on this assumption.
The Rector version used is 1.0.3.
RectorConfig::config() sets the source code folder and PHP version.
The withPhpSets function configures the PHP version you intend to upgrade to.
If the PHP version you're currently running Rector with is PHP 8.0,
You can set the PHP version like this: `php_version: true`.
However, if the running PHP version is PHP 7.4 or lower
You cannot use the named parameter syntax for functions, so you need to set `true` for the desired version when it becomes available.
Rector supports PHP 8.3, so to specify PHP 7.4, you would do it as follows:
->withPaths([
__DIR__ , // This is the directory where rector.php is located.
])
Specifies rules and folders that you don't want to apply.
Folders are specified as '/folder_name/*'.
Files are specified as 'file_name'.
I've added the "SensitiveConstantNameRector" rule.
Rector fails to detect undefined indices within PHP arrays and incorrectly treats them as constants, leading to unintended modifications. Therefore, I've added it to the exclusion rules.
These rules are related to code style, so I've excluded them.
Execution
There are two ways to execute it.
This command only outputs the code that needs to be changed to the console.
Since more RAM is required as the amount of source code increases, you should specify the amount of RAM to use with the `--memory-limit=2G` option.
This command directly modifies the source code.
Since there are so many options, unintended options may be applied, so you should verify using Git.