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

如何让PHPCS忽略文档块/注释中的行尾空白检查?

解决PHPCS忽略文档块内行尾空白检查的问题

有几种办法可以让PHPCS跳过文档块或注释内的行尾空白检查:

  • 全局配置忽略注释行
    编辑你的PHPCS配置文件(phpcs.xml或phpcs.xml.dist),给Squiz.WhiteSpace.SuperfluousWhitespace.EndLine规则添加ignoreComments属性:

    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndLine">
        <properties>
            <property name="ignoreComments" value="true"/>
        </properties>
    </rule>
    

    这个属性会直接让规则忽略所有注释内的行尾空白,大部分Squiz规则集版本都支持。

  • 局部临时禁用规则
    如果只需要忽略个别文档块,在目标文档块前后添加规则开关:

    // phpcs:disable Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
    /**
     * This function does things. 
     * 
     * A long explanation. Nihil hic munitissimus habendi senatus locus, 
     * nihil horum? Gallia est omnis divisa in partes tres, quarum. 
     * 
     * @param something array  My explanation
     */
    // phpcs:enable Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
    
  • 自定义规则检查条件
    若ignoreComments属性不生效,可以重新定义规则,仅检查非注释行:

    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
        <exclude name="Squiz.WhiteSpace.SuperfluousWhitespace.EndLine"/>
    </rule>
    <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndLine">
        <conditions>
            <condition type="not" property="comment"/>
        </conditions>
    </rule>
    

    这段配置先移除原规则,再重新引入并添加条件,只对非注释代码行执行行尾空白检查。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 06:55:05