Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reformat php's old "array()" to the newer/neater "[]" with Visual Studio Code?

Is there a neat search and replace model for reformatting php's old array style to the newer/cleaner json style - without reformatting the entire document with a formatter extension*

from

$flibble = array('foo' => 'bar');

$wibble = array(
    'linefeeds' => true,
);

to

$flibble = ['foo' => 'bar'];


$wibble = [
    'linefeeds' => true,
];

* Aside; I can't actually find a PHP Formatter for VSC which does this either...

like image 930
Jim Morrison Avatar asked Sep 14 '25 23:09

Jim Morrison


2 Answers

No idea how to do it with visual studio code itself but here are some solutions:


Using PHP Codesniffer:

https://github.com/squizlabs/PHP_CodeSniffer

phpcbf src/ --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax

or

https://github.com/thomasbachem/php-short-array-syntax-converter

like image 91
Xatenev Avatar answered Sep 17 '25 02:09

Xatenev


There is now a vscode plugin:

https://marketplace.visualstudio.com/items?itemName=Yannick.php-convert-array

I haven't tried it.

like image 32
Gregory Cosmo Haun Avatar answered Sep 17 '25 02:09

Gregory Cosmo Haun