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

如何配置Vim避免PHP代码switch case格式化时缩进错误?

解决Vim中PHP switch语句case缩进异常问题

直接把下面的配置加到你的Vim配置文件(~/.vimrc 或 ~/.vim/vimrc)里:

autocmd FileType php setlocal cinoptions+=l1

说明

  • autocmd FileType php 确保这个配置只对PHP文件生效,不会干扰其他语言的缩进规则
  • cinoptions+=l1 表示让switch语句里的case/default标签相对于switch块缩进1个层级,正好匹配PHP代码规范要求

代码格式对比

正确格式(符合规范):

public function getSeverity($sanitizedResult, array $statuses, $format)
{             
    switch ($format) {
        case 'namedInteger':
            return $statuses[$sanitizedResult]['result'];
        case 'namedString':
            return $statuses['currentVersion'] == $sanitizedResult ? 0 : 1;
        default:
            throw new \Exception("Unknown result format given " . $format);
    }
}

错误格式(格式化后出现的问题):

public function getSeverity($sanitizedResult, array $statuses, $format)
{             
    switch ($format) {
    case 'namedInteger':
        return $statuses[$sanitizedResult]['result'];
    case 'namedString':
        return $statuses['currentVersion'] == $sanitizedResult ? 0 : 1;
    default:
        throw new \Exception("Unknown result format given " . $format);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 18:52:50