Do you feel that your time indenting code lines could be used to something more useful? Do you hate saving files every 2 seconds because you missed a use declaration or forgot to put that infamous  ; in your code? Fear not, today I'll show you how you can easily overcome this by running some PHP code analysis every time you save a .php file on Sublime Text 3.

I'm assuming you have PHP installed, if you have not, you can follow this  guide. It's a LEMP stack guide, more than you need to just save PHP files and auto correct them, but having a basic stack is strongly recommended.

We're going to install phpcs (PHP Code Sniffer), phpcbf (PHP Code Beautifier) and php-cs-fixer (PHP Code Standards Fixer). These packages will execute upon saving on Sublime, they will tell any syntax errors and will format your code in the specified Coding Standard (the default is PSR-2, which is the one I use). No more discussions whether the code should be tabs, 4 spaces and where the if() brackets should go.

# Download .phar files
$ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
$ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
$ curl -L http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -o php-cs-fixer

# Moving to /bin
$ sudo mv phpcbf.phar /usr/local/bin/phpcbf
$ sudo mv phpcs.phar /usr/local/bin/phpcs
$ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer

# Giving permissions to execute
$ sudo chmod a+x /usr/local/bin/php-cs-fixer
$ sudo chmod a+x /usr/local/bin/phpcs
$ sudo chmod a+x /usr/local/bin/phpcbf

$ which php-cs-fixer
$ which phpcs
$ which phpcbf

The output should be

$ /usr/local/bin/php-cs-fixer
$ /usr/local/bin/phpcs
$ /usr/local/bin/phpcbf

Next, open Sublime Text, and via Package Control Ctrl+Shift+P, install the package Phpcs, it is deprecated but I think it's the only package that does the job well.

Open the package preferences under Preferences > Package Settings > Phpcs > Settings - User

You can tweak the settings any way you like, but I use them like this:

{
	//PHPCS
	"phpcs_execute_on_save": true,
	"phpcs_executable_path": "/usr/local/bin/phpcs",
	
	//PHPCSFIXER
	// "php_cs_fixer_on_save": true,
	// "php_cs_fixer_show_quick_panel": true,
	// "php_cs_fixer_executable_path": "/usr/local/bin/php-cs-fixer",

	//PHPCBF
	"phpcbf_on_save": true,
	"phpcbf_executable_path": "/usr/local/bin/phpcbf",

	//PHPMD
	"phpmd_run": false,	
	"phpmd_executable_path": "/usr/bin/phpmd",
}

Php-cs-fixer and phpcs essentially do the same things but have different criteria to analyze your code, try both and see which one you like best. By default your code errors will appear on the screen. If you want to disable it just put in the Package Settings.

{
    "phpcs_show_errors_on_save": true
}

Note: Both phpcs and php-cs-fixer are not recognizing Coalesce Null Operator expressions as valid syntax, they say that this have been fixed on their repo, but I couldn't get this to work properly. If someone knows how to fix this hit me up.

Congrats! Now test these changes creating a test.php file and purposely messing the indentation or syntax and saving it to see the packages doing their work.