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

如何通过php-cs-fixer移除不必要的冗余赋值变量?

php-cs-fixer是否有规则可以移除赋值后直接返回的冗余变量?

有对应的配置规则,就是return_assignment。

这个规则专门处理你描述的场景:当代码中将某个表达式的结果赋值给变量,随后直接返回该变量时,它会自动消除这个冗余变量,直接返回原表达式。

配置示例

在你的php-cs-fixer配置文件(通常是.php-cs-fixer.dist.php)中启用该规则:

<?php

$config = new PhpCsFixer\Config();

return $config->setRules([
    'return_assignment' => true,
])->setFinder(
    PhpCsFixer\Finder::create()
        ->in(__DIR__ . '/src') // 替换为你的代码目录
);

代码转换效果

原代码:

public function example(): array
{
    $aExample = $this->oModel->example();

    return $aExample;
}

优化后代码:

public function example(): array
{
    return $this->oModel->example();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 12:28:17