How do I find the PHP CodeSniffer rules that I need?
For example I would like function arguments and arrays to not be spaced like this
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
Instead it should be like this:
function asd($var, $var2 = '') {
$array = [$arg, $arg2];
}
And I'd like phpcbf to be able to fix these, but I have no idea how to find what rules do that.
There is no documentation for PHPCS rules (I will get around to this one day) so the only way to figure out what rules you need is to run PHPCS over some sample code.
For this example, I've put your sample bad code into a temp.php file:
<?php
function asd( $var, $var2 = '' ) {
$array = [ $arg, $arg2 ];
}
Then I run PHPCS over it using the included standards. I use the -s
command line argument to make sure I can see the sniff codes as well. I get this output:
$ phpcs temp.php --standard=Generic,Squiz,PEAR,PSR2,Zend -s
FILE: temp.php
---------------------------------------------------------------------------------------------------------------------------------------------
FOUND 17 ERRORS AND 5 WARNINGS AFFECTING 4 LINES
---------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | [ ] The PHP open tag does not have a corresponding PHP close tag (Generic.PHP.ClosingPHPTag.NotFound)
1 | ERROR | [ ] Missing file doc comment (Squiz.Commenting.FileComment.Missing)
2 | WARNING | [ ] The method parameter $var is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
2 | WARNING | [ ] The method parameter $var2 is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
2 | WARNING | [ ] Consider putting global function "asd" in a static class (Squiz.Functions.GlobalFunction.Found)
2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing)
2 | ERROR | [ ] Missing function doc comment (Squiz.Commenting.FunctionComment.Missing)
2 | ERROR | [x] Expected 2 blank lines before function; 0 found (Squiz.WhiteSpace.FunctionSpacing.Before)
2 | ERROR | [ ] Missing function doc comment (PEAR.Commenting.FunctionComment.Missing)
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
2 | WARNING | [ ] Variable "var2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers)
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose)
2 | ERROR | [x] Opening brace should be on a new line (Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine)
2 | ERROR | [x] Opening brace should be on a new line (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine)
2 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine)
3 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
3 | ERROR | [x] Short array syntax is not allowed (Generic.Arrays.DisallowShortArraySyntax.Found)
3 | ERROR | [x] Array with multiple values cannot be declared on a single line (Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed)
3 | WARNING | [ ] Variable "arg2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers)
4 | ERROR | [x] Expected //end asd() (Squiz.Commenting.ClosingDeclarationComment.Missing)
4 | ERROR | [x] Expected 1 blank line before closing function brace; 0 found
| | (Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose)
4 | ERROR | [x] File must not end with a newline character (Generic.Files.EndFileNoNewline.Found)
---------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 12 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------------------------------------
Time: 84ms; Memory: 8Mb
Then I pick out the messages that you want to keep. Probably these two fixable errors:
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen)
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found
| | (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose)
So you might try including the whole Squiz.Functions.FunctionDeclarationArgumentSpacing
sniff in your custom standard and see if you like the results. If not, you can put just those two messages into your standard and that will ignore the rest of the sniff.
You will notice that no errors were reported for the spaces at the start and end of your short array. This tells me that there is no sniff included with PHPCS that checks for this, so you would have to write a custom sniff if you want to enforce it.
This isn't obviously a pretty way of getting this information and docs would be far better, but it's the way I figure out which sniffs can be used to check code (and if a sniff is available) so I thought I'd post it as an answer. Hopefully it helps a little.