Hi,
sorry but I'm not able to find sniff, who check using of short array syntax (PHP >= 5.4). I've removed an old array syntax from my project with PHP-CS-Fixer and I would like to add rule into my standarts.xml.
I looked on Squiz.Arrays.ArrayDeclaration but it makes something different things what I need.
Thank you!
I think there are no sniff, that enforces PHP 5.4 short array syntax and would transform regular array syntax to short form. If you'd like to create such sniff, then please place it in https://github.com/squizlabs/PHP_CodeSniffer/tree/master/CodeSniffer/Standards/Generic/Sniffs/Formatting folder.
All existing array-related sniffs tolerate both regular and short array notations.
Thanks for reply! I'm a totally tokenizer noobie but I'll try it and maybe, I'll send PR :)
I recommend looking at the simplest sniff there can be: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php , which:
Your sniff would:
array word itself is T_ARRAY token so it can be dropped( (can be obtained via $tokens[$stackPtr]['parenthesis_opener']) with an [) (can be obtained via $tokens[$stackPtr]['parenthesis_closer']) with an [Then write a test case to confirm that behavior and a fixture + fixed file.
I guess you should also write the opposite sniff to convert short array to regular ones.
If you're totally stuck with implementation let me know and I see what I can do this week.
It could be easy. I'll try it.
If you get stuck, I'm happy to help as well, or accept a "nearly there" PR.
I think DisallowShortArraySyntax and DisallowLongArraySyntax sniffs would be a good addition.
@o5 maybe you like to consider https://github.com/Mayflower/mo4-coding-standard/blob/master/Sniffs/Formatting/UseArrayShortTagSniff.php
That looks like what @o5 is after.
I've added two sniffs into the Generic standard. The codes are Generic.Arrays.DisallowLongArraySyntax and Generic.Arrays.DisallowShortArraySyntax and both also fix the errors found.
If you prefer warnings, add this to your ruleset.xml file:
<rule ref="Generic.Arrays.DisallowLongArraySyntax.Found">
<type>warning</type>
</rule>
or
<rule ref="Generic.Arrays.DisallowShortArraySyntax.Found">
<type>warning</type>
</rule>
The sniffs are really simple and the fixing should be very easy to follow, so these are probably a good example of how to do basic stuff. The commit also contains the unit tests, and the optional .fixed files that confirm that the auto-fixes are working, so it's worth taking a look if you want to write other sniffs: https://github.com/squizlabs/PHP_CodeSniffer/commit/081f49aa967e2cd7c579442ffa05ef0193c6018a
Thank you @gsherwood
Most helpful comment
I've added two sniffs into the Generic standard. The codes are
Generic.Arrays.DisallowLongArraySyntaxandGeneric.Arrays.DisallowShortArraySyntaxand both also fix the errors found.If you prefer warnings, add this to your ruleset.xml file:
or
The sniffs are really simple and the fixing should be very easy to follow, so these are probably a good example of how to do basic stuff. The commit also contains the unit tests, and the optional
.fixedfiles that confirm that the auto-fixes are working, so it's worth taking a look if you want to write other sniffs: https://github.com/squizlabs/PHP_CodeSniffer/commit/081f49aa967e2cd7c579442ffa05ef0193c6018a