phpcs: How can I modify PSR2 to check that the brace is on the same line as the method?
Asked Answered
B

2

18

I've spent now over 2h on trying to figure out how to require the { in the same line as the method declaration instead of the default requirement being the next line. How can I get this done? I've copied the PSR2 standard to a new folder named PSR2 to be ably to modify it to my liking. So the base I'm working on is basically the PSR2 standard which I would like to modify.

I've tried the ruleset.xml and I've tried to modify it in the code directly without success.

<rule ref="PEAR.Classes.ClassDeclaration">
    <properties>
        <property name="eolChar" value="{"/>
    </properties>
</rule>
<rule ref="PSR2R.Classes.ClassDeclaration">
    <properties>
        <property name="eolChar" value="{"/>
    </properties>
</rule>

I've already figured out that this is wrong. The EOL is set by phpcs. But I can't figure out if there is at all a value I can configure via a rule.

This works fine for me so far (screw the silly spaces!!!):

<?xml version="1.0"?>
<ruleset name="PSR2R">
    <description>PSR2 with tabs instead of spaces.</description>
    <arg name="tab-width" value="4"/>
    <rule ref="PSR2">
        <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
    </rule>
    <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="4"/>
            <property name="tabIndent" value="true"/>
        </properties>
    </rule>
</ruleset>

But I would like to add the rule above.

Bord answered 15/9, 2015 at 12:18 Comment(1)
This was also asked here: github.com/squizlabs/PHP_CodeSniffer/issues/703Grantee
G
18

Put this code in your ruleset.xml file:

<rule ref="PSR2">
    <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
</rule>
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />

That will include the PSR2 standard, but exclude the specific message about the brace needing to be on the same line. Then, it includes the Generic sniff that forces method and function braces to be on the following line.

With that change, this code:

<?php
namespace Test;

class Foo
{
    public function bar() {
    }
}

Will produce no errors, but running PSR2 directly over it produces one error:

FILE: temp.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 6 | ERROR | [x] Opening brace should be on a new line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Grantee answered 15/9, 2015 at 23:40 Comment(4)
Thank you! I could not figure out which rule I had to exclude! How do I archive the same for the class declaration? Do you know the rules that needs changed for this as well? It's sad that there is no proper documentation for all the rules - or I haven't found them.Bord
There is actually no included sniff to enforce a class brace on the same line, and nobody has ever requested one either. So for this, you'd need to write a custom sniff, or submit a feature request to have something added to the core. And yes, I know there is a lack of docs on this and hopefully I can change that one day. I'm only a team of one person though, and I have a very full-time job and a family, so my time has always been pretty limited. Thankfully a lot of good people have submitted code to the project. Just not many docs :)Grantee
The old version of the CakePHP framework must have a sniff for that, I'll see if I can identify it and will do a PR to phpcs if I manage to get it done. I haven't realized you're the lead developer on this project. Thanks for that awesome tool! Any way I can donate a few bucks? :)Bord
Absolutely no donations required, but thanks. The best help is to test, report bugs, and submit PRs if you are able. It makes a huge difference.Grantee
L
9

In addition to the answer from Greg, if you are using PHPStorm, go to Settings -> Editor -> Inspections -> PHP -> Code Sniffer and you will see an option Show sniff name.

enter image description here

This will give you the name of the offending rule (first, configure the PHP Code Sniffer executable path in Settings -> Languages and frameworks -> PHP -> Code sniffer). Then on the warning tooltip in your source code file, carefully move the cursor, select the text and without releasing the button, press Control C to copy it.

Then you paste it in the rules:

<?xml version="1.0"?>
<ruleset name="PSR2R">
    <rule ref="PSR2">
        <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
        <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
    </rule>
    <rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />
</ruleset>

I have added here PSR2.Classes.ClassDeclaration.OpenBraceNewLine to the excluded rules.

Limitative answered 6/3, 2018 at 18:11 Comment(1)
Although this will prevent it from flagging a brace on the same line for a class declaration, since there is no replacement rule for the one you are excluding it won't force the brace on the same line like it does with a function.Andrade

© 2022 - 2024 — McMap. All rights reserved.