durumis AI 总结的文章
- 介绍了Rector这个迁移工具,用于解决PHP版本升级后不再支持的语法和版本问题。
- Rector可以检查和修改PHP 5.3~8.2的代码,使其符合所需的版本。
- 使用Rector需要安装Composer、编写rector.php配置文件、指定不想应用的规则和文件夹、然后运行等步骤。
韩国 php cms 的 GNU Board5 网站在升级 php 版本后,出现了对于停更了的语法和版本感到困难的文章。所以我开始搜索...
去 reddit 看了一下,他们说使用了一个名为 rector 的工具可以帮助代码迁移,所以我来介绍一下它的用法。
php 7.0 于 2015 年推出之后,出现了 php 迁移工具,例如 php cs-fixer 和 rector 等。
rector 看起来更容易一些。
rector (发音:雷克特)可以检查并修复代码中希望的 PHP 5.3 ~ 8.2 语法版本。
唯一令人遗憾的是 rector 是一个静态分析器,因此无法检测运行时发生的错误。
Rector 需要安装 php 包管理工具 composer。
1. 安装 composer。
2. 在当前项目文件夹中安装 rector。
```php composer require rector/rector --dev
3. 创建配置文件 rector.php 。
rector.php 配置
让我们设置一个场景以帮助理解。
目标源代码是 php5.3 代码,升级的目标版本是 php 7.4。
Rector 版本为 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', // 源代码路径 ]) ->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() 设置源代码文件夹和 PHP 版本。
withPhpSets 函数设置要升级的 PHP 函数。
如果当前运行 rector 的 php 版本是 php 8.0
```php ->withPhpSets(php74: true)
这样可以指定 php 版本:true 。
但是,如果运行的 PHP 版本低于 php 7.4 时
由于无法使用函数中的具名参数语法,因此必须在所需版本出现时将其设置为 true。
rector 支持 php 8.3,因此如果要指定 php 74,如下所示:
```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__
, // 是 rector.php 所在的目录。
])
```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/*',
])
指定不想应用的规则和文件夹。
文件夹是 '/文件夹名称/*'。
文件是 '文件名'。
规则为 “SensitiveConstantNameRector”,但是
rector 在 PHP 数组中存在 undefined index 时无法找到它,并且只将其识别为常量,并试图错误地更改它。因此,将其添加到排除规则中。
```php
AddLiteralSeparatorToNumberRector // 数组 -> []
SimplifyIfElseToTernaryRector
TernaryToElvisRector
ClassPropertyAssignToConstructorPromotionRector
由于这些规则是代码样式,因此将它们排除在外。
执行
有两个。
```bash vendor/bin/rector --dry-run --memory-limit=2G
此命令仅在控制台中输出需要更改的代码。
由于源代码可能很大,因此您需要使用 --memory-limit=2G 选项指定使用的内存,因为需要很多 RAM。
```bash vendor/bin/rector --memory-limit=2G
此命令会直接修改源代码。
您应该使用 git 进行检查,可能还有一些不需要的设置或错误。