Php_codesniffer: Short array syntax sniffer

Created on 9 Feb 2015  路  11Comments  路  Source: squizlabs/PHP_CodeSniffer

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!

Enhancement

Most helpful comment

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

All 11 comments

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:

  1. finds a cast token
  2. checks for next token after it to be a single space
  3. if it's not reports that
  4. in fixing mode adds missing space

Your sniff would:

  1. listen for T_ARRAY tokens
  2. report them as an error
  3. when fixing:

    • the array word itself is T_ARRAY token so it can be dropped

    • replace the opening parenthesis ( (can be obtained via $tokens[$stackPtr]['parenthesis_opener']) with an [

    • replace the closing parenthesis ) (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.

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

Was this page helpful?
0 / 5 - 0 ratings