After installing as an extension with Visual Studio Code, this extension automatically formats your PHP code, in accordance with PSR-0, PSR-1, PSR-2 or Symfony style conventions.
Features
- Format current selection only, or the whole file.
- Trigger formatting with custom keybindings or actions.
- Supports formatting on save.
- Supports adjustable level [i.e. PSR2] and fixers.
- Can be configured to support other file extensions than PHP as well, i.e. ".inc" files.
- Supports different PHP-CS-Fixer installation methods, i.e. Composer vs manual installation.
Setup
Refer to our wiki for a quick and easy guide on how to install the extension and its dependencies.
Configuration
Want custom keybindings? Need specific levels and fixers? Check out the wiki
Troubleshooting
Set up the extension, but can't get it to work? Check the Troubleshooting section.
Contribute
Want to add a feature or fix an error? Follow the Contribute section on the wiki to get started.
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.
Install Extension
Inside VS Code, open up the extension side-panel and search for php-cs-fixer.
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 //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:
$ ./php-cs-fixer
You should get the program name and version number.
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: