PHP 用于多行函数声明的 CodeSniffer 自定义规则集

PHP CodeSniffer custom ruleset for multiline function declaration

我试图将 phpcs 配置为在所有地方使用两个空格缩进而不是 4 个,但我被困在一个地方,我无法覆盖多行函数声明的规则

我的密码是

if (!function_exists('errorlog')) {
  function errorlog(
    Exception $e,
    array $data = []
  ) {
}

尽管这段代码给我的错误是

多行函数声明缩进不正确;预期 6 个空格但发现 4

我的自定义规则集

<?xml version="1.0"?>
  <ruleset name="Amit">
  <description>My Custom on top of PSR2</description>

  <rule ref="PSR2">
    <exclude name="PSR2.Classes.ClassDeclaration"/>
    <exclude name="PSR2.ControlStructures.SwitchDeclaration"/>
   <exclude name="PSR2.Methods.FunctionCallSignature"/>
 </rule>

 <rule ref="Generic.Arrays.ArrayIndent">
   <properties>
     <property name="indent" value="2"/>
   </properties>
 </rule>

<rule ref="Generic.WhiteSpace.ScopeIndent">
  <properties>
    <property name="indent" value="2"/>
  </properties>
</rule>

<rule ref="PSR2.Classes.ClassDeclaration">
  <properties>
    <property name="indent" value="2"/>
  </properties>
</rule>

<rule ref="PSR2.ControlStructures.SwitchDeclaration">
  <properties>
    <property name="indent" value="2"/>
  </properties>
</rule>

<rule ref="PSR2.Methods.FunctionCallSignature">
  <properties>
    <property name="indent" value="2"/>
    <property name="requiredSpacesAfterOpen" value="1"/>
    <property name="requiredSpacesBeforeClose" value="1"/>
  </properties>
</rule>


<rule ref="PEAR.ControlStructures.MultiLineCondition">
  <properties>
    <property name="indent" value="2"/>
  </properties>
</rule>


<rule ref="PEAR.Formatting.MultiLineAssignment">
  <properties>
    <property name="indent" value="2"/>
  </properties>
</rule>

<rule ref="PEAR.Functions.FunctionDeclaration">
  <properties>
    <property name="indent" value="2"/>
  </properties>
</rule>
</ruleset>

我使用了上面的代码,运行 它根据提供的规则集,使用 -s 标志来显示 Sniff 报告错误。

<?php
// test.php

if (!function_exists('errorlog')) {
  function errorlog(
    Exception $e,
    array $data = []
  ) {
  }
}

$ ~/.composer/vendor/bin/phpcs --standard=./rules.xml -s test.php

这是我的输出:

FILE: test.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------------
 5 | ERROR | [x] Multi-line function declaration not indented correctly; expected 6 spaces but found 4 (Squiz.Functions.MultiLineFunctionDeclaration.Indent)
 6 | ERROR | [x] Multi-line function declaration not indented correctly; expected 6 spaces but found 4 (Squiz.Functions.MultiLineFunctionDeclaration.Indent)
-------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 90ms; Memory: 6Mb

Squiz.Functions.MultiLineFunctionDeclaration.Indent 是有问题的嗅探。得想办法排除那个!