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

如何在Neovim的null-ls中设置/修改PHP代码规范?

配置null-ls中phpcs/phpcbf的PSR-1代码规范

有两种方式可以将phpcs/phpcbf的代码规范从PEAR切换到PSR-1:

方式一:直接在null-ls配置中指定参数

在null-ls的setup配置里,给phpcs和phpcbf添加extra_args参数,明确指定使用PSR1标准:

local null_ls = require("null-ls")

null_ls.setup({
    sources = {
        -- 配置phpcs诊断使用PSR-1
        null_ls.builtins.diagnostics.phpcs.with({
            extra_args = { "--standard=PSR1" }
        }),
        -- 配置phpcbf格式化使用PSR-1
        null_ls.builtins.formatting.phpcbf.with({
            extra_args = { "--standard=PSR1" }
        })
    }
})

--standard=PSR1是phpcs/phpcbf原生的命令行参数,直接通过null-ls的extra_args传递给工具即可生效。

方式二:使用项目本地配置文件

如果需要团队统一规范,或者不想在neovim配置里硬编码标准,可以在项目根目录创建phpcs.xml(或.phpcs.xml.dist)文件,内容如下:

<?xml version="1.0"?>
<ruleset name="Custom Rules">
    <description>项目采用PSR-1代码规范</description>
    <rule ref="PSR1"/>
</ruleset>

phpcs和phpcbf会自动读取项目根目录的这个配置文件,优先于全局配置,null-ls调用工具时会自动遵循这个规范。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:20:51