You can save yourself quite some time by running a simple command to automatically format your PHP/Laravel code to follow the PSR-1 and PSR-2 standards. Let me show you how you can set it all up to work with Sublime Text 3 on OS X.

Install php-cs-fixer

You must have at least PHP 5.3.6 on your system. Check with

php --version

Run the following commands to download php-cs-fixer:

cd ~/Downloads
wget http://get.sensiolabs.org/php-cs-fixer.phar -O php-cs-fixer

If you don't have wget installed, follow these instructions to install it. Inspect this page so you can get the latest version.

or use curl:

curl http://get.sensiolabs.org/php-cs-fixer.phar -o php-cs-fixer

If you prefer a different download method such as composer or homebrew, check out these instructions.

Now that you've downloaded php-cs-fixer, make it executable:

sudo chmod a+x php-cs-fixer

Move it to a global location:

sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer

Check that it works by running:

php-cs-fixer

If everything is fine, you should see the version and usage information.

Configure a Build Command in Sublime Text 3

  • Open Sublime Text
  • Got to Tools > Build System > New Build System...
  • Enter the following command and save:
{
    "shell_cmd": "php-cs-fixer fix $file --level=psr2"
}

That's it. When you run this command, php-cs-fixer will apply psr-2 standards to the current file you have open.

Run the command by pressing Command + B.

Cheers!