tied rope decorating light wall in room

Convert PHP arrays to short array syntax in seconds

Having just had to look this up, I thought I’d share how I did it. It’s really really easy.

1. Require code sniffer in your project (you are using composer, right?)

composer require --dev squizlabs/php_codesniffer

2. Run CBF using the DisallowLongArraySyntax sniff

vendor/bin/phpcbf . --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax

That will apply to everything in the current directory, so simply change the . to whichever specific files/folders you want to affect.

Perhaps you inherited a project full of old PHP code with long array syntax and you want to convert it all. Or maybe, like me, you have an unreasonable loathing of long array syntax but inherited some code full of it and can’t stand to mix short arrays with long arrays. Perhaps you’re just looking at some of your old code and fancy switching it up.







Leave a Reply

Your email address will not be published. Required fields are marked *