6

I'm attempting to set up a ruleset for PHP CodeSniffer to enforce code style among a group of developers but I've run into some trouble.

We'd like to adhere to PSR-2 except for regarding two things. We want class declarations to have the open brace on the same line and the same for functions. The first I've managed to fix but the error for open brace on the same line for functions just will not go away.

I've traced it to the sniff Generic.Functions.OpeningFunctionBrace.BsdAllman and the error BraceOnSameLine but adding this exclude to my ruleset does nothing.

My ruleset looks like this:

<?xml version="1.0"?>
<ruleset name="OrgXYZ">
    <description>The coding standard for Organization XYZ.</description>
    <rule ref="PSR2">
        <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/>
        <exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine"/>
    </rule>
</ruleset>

And the message I'm trying to remove from the report is this:

15 | ERROR | Opening brace should be on a new line

This is my first attempt at a ruleset of my own and I'm really at a loss here. I've googled, searched and tried everything it seems.

FighterHayabusa
  • 321
  • 1
  • 3
  • 12

2 Answers2

6

Found the problem. I'd gotten lost in what's included in the PSR2 ruleset and excluded the wrong things. Adding this solved it:

<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
FighterHayabusa
  • 321
  • 1
  • 3
  • 12
  • It's important to note that excluding this sniff only suppresses the error for placing the opening brace on the same line. It does **not** automatically guarantee that placing the opening brace on a _new_ line is flagged as an error instead. I have yet to find a sniff that does that... – beporter Jul 23 '15 at 12:32
  • 1
    Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine will flag the new line opening brace as an error. – cmerriman May 10 '16 at 14:30
2

The way to debug codesniffer, is to use the -s flag, revealing the sniffs invoked. For example

phpcs -s ugly.php

Add the results to exclude tags in your project root phpcs.xml, such as your demo

Bruce
  • 199
  • 2
  • 2