Amit Bisht
Amit Bisht

Reputation: 339

PHP CodeSniffer custom ruleset for multiline function declaration

I was trying to configure phpcs to use two spaces indentation everywhere instead of 4 but i am stuck at one place, i cannot override rule for multiline function declartion

my code is

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

although this code is giving me error that

Multi-line function declaration not indented correctly; expected 6 spaces but found 4

My Custom Ruleset

<?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>

Upvotes: 1

Views: 1962

Answers (1)

Cameron Hurd
Cameron Hurd

Reputation: 5031

I took the code you had above and ran it against the ruleset provided, with the -s flag to show the Sniff reporting the error.

<?php
// test.php

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

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

Here was my output:

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 is the sniff in question. Gotta find a way to exclude that one!

Upvotes: 1

Related Questions