I have enabled PEAR.Functions.FunctionCallSignature because I want to check that the spacing after the opening parathesis and the spacing before the close parathesis is just one space, like this:
<?php
test( $example );
For some reason, though, this sniff actually checks more than that, and there is no way to disable the additional functionality. Take this example code:
<?php
test( [
'foo' => 'bar',
] );
Here is what PHPCS says:
FILE: example.php
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 | ERROR | [x] Opening parenthesis of a multi-line function call must be the last content on the line (PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket)
5 | ERROR | [x] Closing parenthesis of a multi-line function call must be on a line by itself (PEAR.Functions.FunctionCallSignature.CloseBracketLine)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Fixing it automatically leads to this, which I consider really ugly:
<?php
test(
[
'foo' => 'bar',
]
);
Is there anyway to disable this unwanted multi-line checking?
Edit:
This is what my phpcs.xml file contains:
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description />
<file>.</file>
<arg name="extensions" value="php,inc,lib"/>
<arg value="n"/>
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1"/>
<property name="requiredSpacesBeforeClose" value="1"/>
</properties>
</rule>
</ruleset>
To be clear, I still want PEAR.Functions.FunctionCallSignature to run and to check whether spaces have been used as expected, I just don't want this weird multiline behaviour described earlier.
For this file:
<?php
test($example);
I still want to see this output:
$ phpcs -s
FILE: example-no-spaces.php
----------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------
3 | ERROR | [x] Expected 1 spaces after opening bracket; 0 found (PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket)
3 | ERROR | [x] Expected 1 spaces before closing bracket; 0 found (PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket)
----------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------------------------------
You just want these two sniff codes:
Hopefully you're using version 3, so you can just do this in your ruleset:
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket" />
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket" />
If you are still using version 2, you'd need to include the main sniff and then exclude the sniff codes you don't want. This is a good start, but you may need to exclude more:
<rule ref="PEAR.Functions.FunctionCallSignature" />
<rule ref="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket">
<severity>0</severity>
</rule>
<rule ref="PEAR.Functions.FunctionCallSignature.CloseBracketLine">
<severity>0</severity>
</rule>
You can find more info about rulesets in the wiki: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
Does that help?
@gsherwood Thanks you for your useful reply. I tried the two sniff rules you suggested:
Unfortunately, it seems like these rules require that there be no space after opening parenthesis of a function call, and they require that there be no spaces before the closing bracket. There doesn't seem to be a way to configure them to require a space. I want to require spaces, but without getting the multi-line errors that I explained in the original post.
This is what I'm getting with the two rules suggested:
$ phpcs -s
FILE: example.php
--------------------------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINES
--------------------------------------------------------------------------------------------------------------------------------------------
10 | ERROR | [x] Space after opening parenthesis of function call prohibited (PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket)
10 | ERROR | [x] Expected 0 spaces before closing bracket; 1 found (PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket)
Try setting the number of spaces you want, i.e.:
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1" />
<property name="requiredSpacesBeforeClose" value="1" />
</properties>
</rule>
@jrfnl I already tried that, and I ran across the issue I described in the first post.
@Flimm Are you by chance also using the Squiz.Arrays.ArrayDeclaration sniff ?
@jrfnl I am not using Squiz.Arrays.ArrayDeclaration.
Ok, just checking cause I know there are potential fixer conflicts between PEAR.Functions.FunctionCallSignature and Squiz.Arrays.ArrayDeclaration when you disable parts of either sniff.
I already tried that, and I ran across the issue I described in the first post.
Have you tried excluding the messages you don't want by setting their severity to 0?
I think what might be happening is that you are including the entire sniff because you are setting properties, so you are getting all these messages you don't want.
If you can't get it working and you are willing to post your ruleset, please do and I may be able to suggest a change. But I'd start by just excluding the sniff codes you don't want.
@gsherwood How do I do that?
This phpcs.xml does not do what I want it to do:
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description />
<file>.</file>
<arg name="extensions" value="php,inc,lib"/>
<arg value="n"/>
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1"/>
<property name="requiredSpacesBeforeClose" value="1"/>
</properties>
<severity>0</severity>
</rule>
</ruleset>
This completely disables PEAR.Functions.FunctionCallSignature! This is not what I want. I want to continue to use PEAR.Functions.FunctionCallSignature, to catch when spaces are missing in function calls like test($example); instead of test( $example );, but not to have the weird additional errors about multi-line, as described in the original post. I've edited the original post to be more clear and to include the ruleset as you suggested.
Try:
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1"/>
<property name="requiredSpacesBeforeClose" value="1"/>
</properties>
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
</rule>
If you only want those two error messages for spaces around function calls, then do this (see comments for an explanation):
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description />
<file>.</file>
<arg name="extensions" value="php,inc,lib"/>
<arg value="n"/>
<!-- Include the FunctionCallSignature sniff so we can set some properties
to enforce a single space after the opening parenthesis and before the
closing parenthesis, like this: test( $var );
But we only want two of the error codes from this sniff, no matter how
it changes over time, so set the severity of the entire sniff to 0
as this will exclude all message codes it produces. -->
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1"/>
<property name="requiredSpacesBeforeClose" value="1"/>
</properties>
<severity>0</severity>
</rule>
<!-- Now that we've excluded all message codes from FunctionCallSignature,
re-enable the two codes that we actually want. This will change the severity
of these codes from 0 (set by the above rule) to the default value (5). -->
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket" />
<rule ref="PEAR.Functions.FunctionCallSignature.SpaceBeforeCloseBracket" />
</ruleset>
@jrfnl @gsherwood Thank you so much, both of your solutions worked! Thanks for being patient with me, in hindsight, I should have posted my phpcs.xml and example files with expected output from the get-go.
Glad it's working. Thanks for being patient with me - you did post this 15 days ago :)
Most helpful comment
@jrfnl @gsherwood Thank you so much, both of your solutions worked! Thanks for being patient with me, in hindsight, I should have posted my
phpcs.xmland example files with expected output from the get-go.