Create a gist now

Instantly share code, notes, and snippets.

Embed
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!

@deadlyhifi

This comment has been minimized.

Show comment
Hide comment
@deadlyhifi

deadlyhifi Jul 5, 2015

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

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

@kjunggithub

This comment has been minimized.

Show comment
Hide comment
@kjunggithub

kjunggithub Jul 12, 2015

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

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

@hkchai

This comment has been minimized.

Show comment
Hide comment
@hkchai

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

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

This comment has been minimized.

Show comment
Hide comment
@jegra

jegra 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.

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.

@mikefrancis

This comment has been minimized.

Show comment
Hide comment
@mikefrancis

mikefrancis Aug 27, 2015

@hkchai @jegra thanks for your input

Owner

mikefrancis commented Aug 27, 2015

@hkchai @jegra thanks for your input

@carlosflorencio

This comment has been minimized.

Show comment
Hide comment
@carlosflorencio

carlosflorencio Sep 6, 2015

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

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

Thanks!

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

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

Thanks!

@rweas

This comment has been minimized.

Show comment
Hide comment
@rweas

rweas 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.

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.

@tarranjones

This comment has been minimized.

Show comment
Hide comment
@tarranjones

tarranjones Oct 6, 2015

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

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

@mikefrancis

This comment has been minimized.

Show comment
Hide comment
@mikefrancis

mikefrancis 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?
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?
@Nutriwel

This comment has been minimized.

Show comment
Hide comment
@Nutriwel

Nutriwel Dec 17, 2015

Yes and I am still not able to run

Yes and I am still not able to run

@keeZey

This comment has been minimized.

Show comment
Hide comment
@keeZey

keeZey 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

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

@sunshowerykk

This comment has been minimized.

Show comment
Hide comment
@sunshowerykk

sunshowerykk Oct 17, 2016

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

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

@maisafie

This comment has been minimized.

Show comment
Hide comment
@maisafie

maisafie 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.

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

This comment has been minimized.

Show comment
Hide comment
@kevnk

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

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

This comment has been minimized.

Show comment
Hide comment
@mikefrancis

mikefrancis 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!

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!

@jimmerioles

This comment has been minimized.

Show comment
Hide comment
@jimmerioles

jimmerioles Jun 6, 2017

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

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

@markhowellsmead

This comment has been minimized.

Show comment
Hide comment
@markhowellsmead

markhowellsmead 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 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.)

@raulqtime

This comment has been minimized.

Show comment
Hide comment
@raulqtime

raulqtime Sep 11, 2017

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

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

@zmonteca

This comment has been minimized.

Show comment
Hide comment
@zmonteca

zmonteca Jan 27, 2018

To get rid of that show_panel_on_build init error, on my MacOS 10.13, Sublime 3, I used the following:

{
    "path": "$HOME/.composer/vendor/bin",
    "shell_cmd": "php-cs-fixer fix '$file' --using-cache=no",
    "cmd": {
        "show_panel_on_build": false
    }
}

To get rid of that show_panel_on_build init error, on my MacOS 10.13, Sublime 3, I used the following:

{
    "path": "$HOME/.composer/vendor/bin",
    "shell_cmd": "php-cs-fixer fix '$file' --using-cache=no",
    "cmd": {
        "show_panel_on_build": false
    }
}
@allyssonharry

This comment has been minimized.

Show comment
Hide comment
@allyssonharry

allyssonharry May 29, 2018

{
"path": "$HOME/.composer/vendor/bin", required only if php-cs-fixer is not registered in the system.
"shell_cmd": "php-cs-fixer fix '$file' --rules=@PSR2",
"show_panel_on_build": false
}

{
"path": "$HOME/.composer/vendor/bin", required only if php-cs-fixer is not registered in the system.
"shell_cmd": "php-cs-fixer fix '$file' --rules=@PSR2",
"show_panel_on_build": false
}

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