Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use PSR-2 with exceptions in PHP CodeSniffer

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.

like image 467
FighterHayabusa Avatar asked Jul 14 '14 13:07

FighterHayabusa


2 Answers

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"/>
like image 64
FighterHayabusa Avatar answered Oct 19 '22 19:10

FighterHayabusa


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

like image 45
Bruce Avatar answered Oct 19 '22 18:10

Bruce