Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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!

Add "show_panel_on_build": false to keep it from popping out the command panel.
via Sublime Text (3) for PHP Developers

Just FYI, show_panel_on_build: false goes in your user profile file.

hkchai commented Aug 22, 2015

I suggest to wrap $file in single quotes('') or empty spaces in path names can choke the command: "shell_cmd": "php-cs-fixer fix '$file' --level=psr2"

jegra commented Aug 22, 2015

Found I needed to remove the "path": "$HOME/.composer/vendor/bin" line from the script in order for this to work in windows.

Owner

mikefrancis commented Aug 27, 2015

@hkchai @jegra thanks for your input

iamfreee commented Sep 6, 2015

To work on my system (Windows 10) i used:

{
    "shell_cmd": "php-cs-fixer fix \"$file\" --level=psr2"
}

Thanks!

rweas commented Oct 3, 2015

So much easier than using a plugin! Thanks!

BTW if anyone didn't know you can hit cmd+B at any time to run the build command.

Ensure you can run PHP-CS-Fixer globally by running php-cs-fixer -v in a new terminal window.

I cannot get this to work on windows with cmd.

I've tried

$ php php-cs-fixer -v

-> Could not open input file: php-cs-fixer

$ php-cs-fixer -v

-> 'php-cs-fixer' is not recognized as an internal or external command, operable program or batch file.

I have used the windows installer and have checked that php and composer are both in the path

C:\PHP;C:\ProgramData\ComposerSetup\bin

Any ideas why it wont find/run php-cs-fixer

Owner

mikefrancis commented Oct 6, 2015

@dtjones

  • Do you have composer installed? (Can you run composer from anywhere in your system?)
  • Do you have the composer package fabpot/php-cs-fixer installed globally on your system?

Yes and I am still not able to run

keeZey commented Jan 6, 2016

@dtjones

I had the same fault and fixed it by using the full file path:
php C:\php\php-cs-fixer -V

My sublime build setting:
{
"shell_cmd": "php c:\php\php-cs-fixer fix "$file" --level=psr2"
}

I saved php-cs-fixer in my php folder, you will need to change the path to where you saved your file

Opening the Command Palette and selecting Build with: php. All your build commands are listed in there.
How to do it?

maisafie commented Mar 22, 2017

Well ... i know that a looooong way passed since last comment here but just as a fact, in order to "fix" @tarranjones problem. If package is installed using composer (which is the "right" way to do it) then the composer's global packages bin directory should be added to your path. Which in my case is "C:\Users\Chris\AppData\Roaming\Composer\vendor\bin". Then you can simply use : php-cs-fixer -V.

kevnk commented May 23, 2017

Package fabpot/php-cs-fixer is abandoned, you should avoid using it. Use friendsofphp/php-cs-fixer instead.

So after running installing the friendsofphp/php-cs-fixer with this command:

$ composer global require friendsofphp/php-cs-fixer

I was getting this message when running my build:

[Symfony\Component\Console\Exception\RuntimeException]  
  The "--level" option does not exist.

So instead of --level=psr2 I had to use --rules=@PSR2 as explained in the docs. So my PHP.sublime-build file (on Mac) is as follows:

{
    "shell_cmd": "php-cs-fixer fix \"$file\" --rules=@PSR2"
}
Owner

mikefrancis commented May 24, 2017

@kevnk Thanks for that 👌 I have updated the Gist for if anyone else has the same problems, seems like a lot has changed in 2 years!

It would be nice to add --using-cache=no to avoid littering .php_cs.cache everywhere.

markhowellsmead commented Jun 30, 2017

Thanks! Getting the error __init__() got an unexpected keyword argument 'show_panel_on_build' all the time, irrelevant of which file I run it on. (OSX, all installed today.)

@markhowellsmead show_panel_on_build option you must indicate it in User preferences file (cmd + ,)

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