Open a new terminal and install Composer:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Once that has finished installing and moved, install the PHP-CS-Fixer Composer package globally:
composer global require fabpot/php-cs-fixer
Ensure you can run PHP-CS-Fixer globally by running php-cs-fixer -v
in a new terminal window.
If you can't, make sure the composer bin directory is in your PATH:
export PATH="$PATH:$HOME/.composer/vendor/bin"
Thanks to @Migweld for the above.
Then open Sublime Text and go to Tools > Build System > New Build System...
- this will create a new untitled.sublime-build
file, in which you can drop this snippet in:
{
"path": "$HOME/.composer/vendor/bin",
"shell_cmd": "php-cs-fixer fix '$file' --level=psr2",
"show_panel_on_build": false
}
Hit 'Save', rename this file as php.sublime-build
and save it in the same directory. As @jegra suggested, you don't need the first line if you're on Windows.
Now every time you use the php
build system, your code will automatically be reformatted to conform with PSR-2. You can run a build system by:
- Opening the Command Palette and selecting
Build with: php
. All your build commands are listed in there. - Going to
Tools > Build System > php
(then running subsequent builds withTools > Build
). - Hitting
Cmd+B
(orCtrl+B
on Windows I think).
Finally, you can check Tools > Save All on Build
to also save whenever you build. Easy!