Php convert long array syntax to short

Php introduced new short array syntax ‘[]’ back with PHP version 5.4. Practically we may still work on some older projects where we may encounter those long formatted array() syntax. And also – there is nothing wrong with using a long syntax, it’s not like it’s deprecated or anything. Some of us do however strongly prefer short syntax .

Here’s how to convert your (perhaps older) PHP projects to short array syntax fast:

Add PHP code sniffer to your project:

{
    "require-dev": {
        "squizlabs/php_codesniffer": "^3.5"
    }
}

Update composer dependencies:

project$ composer update

Run this command:

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

Substitute src with directory you wish to correct array syntax in.

You should see output similar to this:

PHPCBF RESULT SUMMARY
-----------------------------------------------------------------------------------------------------------------------------
FILE                                                                                                         FIXED  REMAINING
-----------------------------------------------------------------------------------------------------------------------------
/project/src/bundle/AuthFrontend/src/Controller/ApiController.php                   3      0
/project/src/bundle/CategoriesManager/config/module.config.php                       33     0
/project/src/bundle/CategoriesManager/src/Controller/ViewController.php              2      0
/project/src/bundle/CategoriesManager/src/Controller/ApiController.php              24     0
/project/src/bundle/ProductsManager/config/module.config.php                         33     0
/project/src/bundle/ProductsManager/src/Controller/ViewController.php                2      0
/project/src/bundle/ProductsManager/src/Controller/ApiController.php                24     0
/project/src/bundle/TagsManager/config/module.config.php                             33     0
/project/src/bundle/TagsManager/src/Controller/ViewController.php                    2      0
/project/src/bundle/TagsManager/src/Controller/ApiController.php                    24     0
/project/src/bundle/UsersManager/src/Module.php                                      2      0
/project/src/bundle/UsersManager/src/Controller/ViewController.php                   9      0
/project/src/bundle/UsersManager/src/Controller/ApiController.php                   11     0
/project/src/bundle/Application/config/module.config.php                             9      0
/project/src/bundle/Application/src/Module.php                                       9      0
/project/src/bundle/Application/src/Controller/ApiController.php                    3      0
/project/src/bundle/ArticlesManager/config/module.config.php                         33     0
/project/src/bundle/ArticlesManager/src/Controller/ViewController.php                2      0
/project/src/bundle/ArticlesManager/src/Controller/ApiController.php                24     0
/project/src/bundle/AdScansManager/config/module.config.php                          33     0
/project/src/bundle/AdScansManager/src/Controller/ViewController.php                 2      0
/project/src/bundle/AdScansManager/src/Controller/ApiController.php                 24     0
-----------------------------------------------------------------------------------------------------------------------------
A TOTAL OF 341 ERRORS WERE FIXED IN 22 FILES
-----------------------------------------------------------------------------------------------------------------------------

Time: 1.05 secs; Memory: 14MB

There we go – quick and easy.

Leave a Comment