PHP-CS Fixer With SublimeText

How to install PHP CS Fixer with Sublime Text on Windows

If you want to follow the PHP coding standards as defined in the PSR-1 and PSR-2 documents the easy way it is better to use a code improvement tool that will do ti for you. This is handy specially when you need to correct code formatting on big projects.

  1. Install PHP Code Sniffer from PEAR
    http://pear.php.net/package/PHP_CodeSniffer
  2. Install Fabpot PHP-CS fixer
    https://github.com/fabpot/PHP-CS-Fixer
  3. Install plugin with sublime text as usual Ctrl+Shift+P
    https://github.com/benmatselby/sublime-phpcs


Then you need to set the default setting with Sublime Text, just go to: Preferences > Package Settings > PHP Code Sniffer > Setting - Default

Here is my file for the Windows configuration:

{
// Plugin settings

// Turn the debug output on/off
"show_debug": true,

// Which file types (file extensions), do you want the plugin to
// execute for
"extensions_to_execute": ["php"],

// Do we need to blacklist any sub extensions from extensions_to_execute
// An example would be ["twig.php"]
"extensions_to_blacklist": [],

// Execute the sniffer on file save
"phpcs_execute_on_save": true,

// Show the error list after save.
"phpcs_show_errors_on_save": true,

// Show the errors in the gutter
"phpcs_show_gutter_marks": true,

// Show outline for errors
"phpcs_outline_for_errors": true,

// Show the errors in the status bar
"phpcs_show_errors_in_status": true,

// Show the errors in the quick panel so you can then goto line
"phpcs_show_quick_panel": true,

// The path to the php executable.
// Needed for windows, or anyone who doesn't/can't make phars
// executable. Avoid setting this if at all possible
// Path to php on windows installation
// This is needed as we cannot run phars on windows, so we run it through php
"phpcs_php_prefix_path": "C:\\xampp\\php\\php.exe",

// Options include:
// - Sniffer
// - Fixer
// - Mess Detector
//
// This will prepend the application with the path to php
// Needed for windows, or anyone who doesn't/can't make phars
// executable. Avoid setting this if at all possible
// We want the fixer to be run through the php application
"phpcs_commands_to_php_prefix": ["Fixer"],


// PHP_CodeSniffer settings

// Do you want to run the phpcs checker?
"phpcs_sniffer_run": true,

// Execute the sniffer on file save
"phpcs_command_on_save": false,

// It seems python/sublime cannot always find the phpcs application
// If empty, then use PATH version of phpcs, else use the set value
"phpcs_executable_path": "C:\\xampp\\php\\phpcs.bat",

// Additional arguments you can specify into the application
//
// Example:
// {
// "--standard": "PEAR",
// "-n"
// }
"phpcs_additional_args": {
"--standard": "PSR2",
"-n": ""
},


// PHP-CS-Fixer settings

// Fix the issues on save
"php_cs_fixer_on_save": false,

// Show the quick panel
"php_cs_fixer_show_quick_panel": true,

// Path to where you have the php-cs-fixer installed
// The fixer phar file is stored here:
"php_cs_fixer_executable_path": "C:\\xampp\\php\\php-cs-fixer.phar",

// Additional arguments you can specify into the application
//
// Example:
// {
// "--level": "all"
// }
"php_cs_fixer_additional_args": {
"--level": "all"
},

// PHP Linter settings

// Are we going to run php -l over the file?
"phpcs_linter_run": true,

// Execute the linter on file save
"phpcs_linter_command_on_save": true,

// It seems python/sublime cannot always find the php application
// If empty, then use PATH version of php, else use the set value
// Path to php
"phpcs_php_path": "C:\\xampp\\php\\php.exe",

// What is the regex for the linter? Has to provide a named match for 'message' and 'line'
"phpcs_linter_regex": "(?P<message>.*) on line (?P<line>\\d+)",



// PHP Mess Detector settings

// Execute phpmd
"phpmd_run": true,

// Execute the phpmd on file save
"phpmd_command_on_save": false,

// It seems python/sublime cannot always find the phpmd application
// If empty, then use PATH version of phpmd, else use the set value
"phpmd_executable_path": "C:\\xampp\\php\\phpmd.bat",

// Additional arguments you can specify into the application
//
// Example:
// {
// "codesize,unusedcode"
// }
//"phpmd_additional_args": {
// "codesize,unusedcode,naming": ""
//},
"phpmd_additional_args": {},

// PHP Scheck settings

// Execute scheck
"scheck_run": false,

// Execute the scheck on file save
"scheck_command_on_save": false,

// It seems python/sublime cannot always find the scheck application
// If empty, then use PATH version of scheck, else use the set value
"scheck_executable_path": "",

// Additional arguments you can specify into the application
//
//Example:
//{
// "-php_stdlib" : "/path/to/pfff",
// "-strict" : ""
//}
"scheck_additional_args": {
"-strict" : ""
}
}

Once is installed and cofigured, just right click on a php opended file with Sublime Text and you will see the PHP CS Fixer options