If you’re anything like me — that sucks.
No but really, like me — you might be struggling to get PHP formatting working in a way you’re satisfied with; one extension formats HTML but doesn’t format whole documents, and with others I don’t get any formatting at all.
Probably user error — true. But if you’re still struggling with getting PHP formatting working in VS Code here’s what I finally figured out.
Inside VS Code, open up the extension side-panel and search for
You’ll see a few with the same name so make sure you install the extension by the Junstyle.
Install The PHP Fixer File
In any folder use CURL to download the php fixer file to your working directory:
$ curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fixer
If for some reason CURL isn’t getting the file for you, go to the file’s Github Repo and download it however you want.
Make sure the file has the executable flag by running:
$ sudo chmod a+x php-cs-fixer
Make sure you can run it by running:
Move the file to a system directory that’s in the PATH:
$ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
Create a Config File
In your terminal — create and edit a config file in your user directory:
$ sublime ~/.php_cs
Use your favorite text editor.
Paste this config code in the editor and save:
Save and Exit.
Point VS Code To Your Config File
Open VS Code Preferences > Settings.
About 4 options down you’ll see a field for the config file path. Enter the absolute path like so:
Set Extension as Default Formatter
.php file in VS Code right click in the editor and click on
If that formatted your code then great! If you have other formatters associated with PHP then click on
Configurein the dialog in the bottom right and select
php cs fixer from the dropdown.
If all went well you should see your PHP cleaned up a bit.
You’ll probably wanna tweak that config file. The array syntax is set to short which bugs me and you’ll probably be bugged by a few things too.
Anyway, let me know if this didn’t work for you. Hopefully I didn’t miss anything.
Code on web assassins!