Php_codesniffer: which sniff can use [] replace array()

Created on 8 Dec 2017  路  4Comments  路  Source: squizlabs/PHP_CodeSniffer

Question

Most helpful comment

If you want to convert long array syntax to short array syntax, you'll want to use the Generic.Arrays.DisallowLongArraySyntax sniff:

$ cat temp.php
<?php
$foo = array();

$ phpcs temp.php --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax --report=diff
--- temp.php
+++ PHP_CodeSniffer
@@ -1,2 +1,2 @@
 <?php
-$foo = array();
+$foo = [];

If you just want to replace all long arrays with short arrays over your codebase, you can do this: phpcbf --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax /path/to/project

But make sure you add that sniff to your coding standard so new code is being checked and replaced as well.

Is that what you were after?

All 4 comments

If you want to convert long array syntax to short array syntax, you'll want to use the Generic.Arrays.DisallowLongArraySyntax sniff:

$ cat temp.php
<?php
$foo = array();

$ phpcs temp.php --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax --report=diff
--- temp.php
+++ PHP_CodeSniffer
@@ -1,2 +1,2 @@
 <?php
-$foo = array();
+$foo = [];

If you just want to replace all long arrays with short arrays over your codebase, you can do this: phpcbf --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax /path/to/project

But make sure you add that sniff to your coding standard so new code is being checked and replaced as well.

Is that what you were after?

thank you, but i can't find DisallowLongArraySyntax in wiki, and i found the sniffs of document is less than the actual

where can we get the newest document ?

There are no docs for sniffs that don't have config options.

@tim0991 The only sniffs which are documented in the wiki are the ones which have custom properties you can set.
To find out which sniffs are available, you can use the -e (=explain) option, i.e.:
phpcs -e --standard=Generic
will show you all sniffs which are available in Generic or for other rulesets: all sniffs which are included by the ruleset.

Was this page helpful?
0 / 5 - 0 ratings