Summarized by durumis AI
- I introduced the Rector migration tool to solve the syntax and version problems that arise after upgrading the PHP version.
- Rector checks and fixes the syntax of PHP 5.3~8.2 code to match the desired version.
- To use Rector, you need to install Composer, write a rector.php configuration file, specify folders and rules to exclude, and then execute it.
Rector is a PHP tool that helps automate the migration of PHP code from older versions to newer ones.
After PHP 7.0 was released in 2015, a number of PHP migration tools emerged, including PHP CS-Fixer and Rector.
Rector is generally considered easier to use.
Rector (pronounced "rector") checks the syntax of code from PHP 5.3 to 8.2 and corrects it to match the desired version.
One drawback is that Rector is a static analyzer, so it cannot detect errors that occur at runtime.
Rector requires the PHP package management tool Composer to be installed.
1. Install Composer.
2. Install Rector in your current project folder.
```php composer require rector/rector --dev
3. Create a configuration file named rector.php.
Rector.php Configuration
Let's set up a scenario to help us understand.
Assume that the target source code is PHP 5.3 code and the upgrade target version is PHP 7.4.
The Rector version is 1.0.3.
```php <?php
use Rector\Config\RectorConfig; use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector; use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
return RectorConfig::configure() ->withPhpSets(php74: true) ->withParallel(1200, 8, 8) ->withPaths([ DIR . '/src', // Source code path ]) ->withSkip([ Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector::class, Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector::class, Rector\CodeQuality\Rector\If\SimplifyIfElseToTernaryRector::class, Rector\Php53\Rector\Ternary\TernaryToElvisRector::class, Rector\Php80\Rector\Class\ClassPropertyAssignToConstructorPromotionRector::class, Rector\Php80\Rector\FunctionLike\MixedTypeRector::class, Rector\CodeQuality\Rector\If\SimplifyIfNullableReturnRector::class, Rector\Php80\Rector\Switch\ChangeSwitchToMatchRector::class, Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector::class, Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector::class, ]);
RectorConfig::config() sets the source code folder and PHP version.
The withPhpSets function sets the PHP functions to be upgraded.
If the PHP version running Rector is PHP 8.0
```php ->withPhpSets(php74: true)
It can be set to php version : true like this.
However, if the running PHP version is PHP 7.4 or lower
Since the named parameter syntax cannot be used in the function, you must set true when the desired version is released.
Rector supports PHP 8.3, so to specify PHP 74, do the following:
```php ->withPhpSets( false, // php 8.3 false, // php 8.2 false, // php 8.1 false, // php 8.0 false, // php 8.1 true // php 7.4 )
->withPaths([
__
DIR__
, // This is the directory where rector.php is located.
])
```php
->withSkip([
Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector::class,
Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector::class,
Rector\CodeQuality\Rector\If\SimplifyIfElseToTernaryRector::class,
Rector\Php53\Rector\Ternary\TernaryToElvisRector::class,
Rector\Php80\Rector\Class\ClassPropertyAssignToConstructorPromotionRector::class,
'/data/*',
])
Specifies the rules and folders that you do not want to apply.
The folder is '/folder_name/*'
The file is 'filename'.
I added the rule "SensitiveConstantNameRector"
Rector cannot find this when there is an undefined index in a PHP array and recognizes it as a constant only, so it tries to change it incorrectly. That's why it is added to the exception rule.
```php
AddLiteralSeparatorToNumberRector //array -> []
SimplifyIfElseToTernaryRector
TernaryToElvisRector
ClassPropertyAssignToConstructorPromotionRector
I exclude these rules because they are code styles.
Execution
There are two.
```bash vendor/bin/rector --dry-run --memory-limit=2G
This command only prints the code that needs to be changed to the console.
Since the more source code there is, the more RAM is required, you must specify the RAM usage with the option --memory-limit=2G.
```bash vendor/bin/rector --memory-limit=2G
This command modifies the source code directly.
You should check with git as there may be unwanted options or errors.