You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Rector批量替换变量名?对应的规则名称是什么?

使用Rector批量替换变量名的方法

你可以用Rector的RenameVariableRule规则实现全局变量名替换,具体操作如下:

  • 配置规则:在项目的rector.php配置文件中,添加该规则并指定变量名的映射关系:

    use Rector\Renaming\Rector\Variable\RenameVariableRule;
    use Rector\Config\RectorConfig;
    
    return RectorConfig::configure()
        ->withRules([
            RenameVariableRule::class => [
                '$connection' => '$link',
            ],
        ]);
    
  • 执行替换:运行Rector命令处理目标代码目录(示例中为src目录):

    vendor/bin/rector process src
    

该规则会遍历指定目录下的所有PHP文件,将所有名为$connection的变量统一替换为$link。如果需要排除特定文件或目录,可在配置中通过withSkip或withPaths参数调整处理范围。

内容的提问来源于stack exchange,提问作者realtebo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 05:20:23