Skip to content

Instantly share code, notes, and snippets.

@Migweld
Last active December 3, 2015 12:39
Show Gist options
  • Save Migweld/01dec97faf0dc145a433 to your computer and use it in GitHub Desktop.
Save Migweld/01dec97faf0dc145a433 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 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"

PHP-CS-Fixer is also available as part of the homebrew-php package:

brew install homebrew/php/php-cs-fixer

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"
}

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