phpstorm replace array() notation to [ ] short syntax

RegexReplacePhpstorm

Regex Problem Overview


I would like to transform all "array(...)" notation in "[...]" in my project but I don't find a regex that work for all cases.

Do you have something that work for all cases (nested array, array on multiple lines, nested array on multiple lines)

Regex Solutions


Solution 1 - Regex

  1. Code | Inspect Code... --- run it on your files. One of the inspections called "Traditional syntax array literal detected".

    Alternatively just run that specific inspection only via Code | Run Inspection by Name...

  2. Once done, you will see a list with results. Find that particular inspection in results (in case if 1st option was used (all inspections)).

  3. From there you can apply "Fix it" action on all (or individual) results of that inspection.

P.S. This inspection/fix available since PhpStorm v7.1.

enter image description here

Solution 2 - Regex

Open File->Settings... Click Editor->Code Style->PHP->Code Conversion

Now you can find block "Array declaration style" at the bottom.

If you check checkbox "Force short declaration style" your "array(...)" became "[...]" when you run Reformat code (Ctrl+Shift+L).

Solution 3 - Regex

As of now (PHPStorm 2017.3.2) it is here: Settings/Editor/Code Style/PHP/Code Conversion/Array Declaration style

enter image description here

Solution 4 - Regex

If you only want to update a single array/line, you can place the cursor on the array and hit Alt + Enter to invoke the intention actions dialogue then select the Convert array to short syntax option. 1

animated gif from phpstorm tips

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3060886View Question on Stackoverflow
Solution 1 - RegexLazyOneView Answer on Stackoverflow
Solution 2 - RegexStepanView Answer on Stackoverflow
Solution 3 - RegexDmitriy LezhnevView Answer on Stackoverflow
Solution 4 - RegexSᴀᴍ OnᴇᴌᴀView Answer on Stackoverflow