1

我试图将 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>
4

1 回答 1

1

我采用了您上面的代码并根据提供的规则集运行它,并带有-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是有问题的嗅探。必须找到一种方法来排除那个!

于 2018-07-17T14:41:36.227 回答