Skip to content

Instantly share code, notes, and snippets.

@jarenal
Created July 27, 2017 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarenal/27d6c53e67a3de3907dd0a43e4732976 to your computer and use it in GitHub Desktop.
Save jarenal/27d6c53e67a3de3907dd0a43e4732976 to your computer and use it in GitHub Desktop.
PSR-2 Build System using Sublime Text

PSR-2 Build System using Sublime Text

Installation

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 friendsofphp/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"

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' --rules=@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.

Usage

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 with Tools > Build).
  • Hitting Cmd+B (or Ctrl+B on Windows I think).

Finally, you can check Tools > Save All on Build to also save whenever you build. Easy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment